I am creating a Spring MVC webapp and I like to use automatic discovery of components such as controllers.
In my application-context file I put the following tag
<context:component-scan base-package="com.example" />
There is a controller present in com.example.springmvc.controller
@Controller
public class AccountController {
@RequestMapping("/account.html")
public ModelMap showAccount() throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("someText", "This is an account");
return modelMap;
}
@RequestMapping("/account.html")
public ModelMap showAccount(@RequestParam("accountId") int accountId) throws Exception {
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("someText", String.format("This is an account with id %d", accountId));
return modelMap;
}
}
When I test by going to localhost:8080/account.html I get a 404, so I must be forgetting something.
When I create a custom mapping in my MVC config file, like below, than it works. At least, then I get an error about ambigious methods in the controller, but that is another problem. At least then it finds the controller.
<bean name="/account.html" class="com.example.springmvc.controller.AccountController"/>
I don’t want to create my mappings in XML, I want to use annotated URL mappings. Could you tell me what I am forgetting here?
To enable annotation-based configuration scanning, add the following to your Spring configuration: