Spring MVC is very confused about controller mappings

Use annotation-based controller mappings.

@Controller public class AlertsController { @RequestMapping(value="create", method=RequestMethod.GET) public void create(HttpServletRequest request, Model model) { } } 

When accessing alerts/create I get the message Does your handler implement a supported interface like Controller? . This seems strange and contradicts what the documentation says.

So, I add the RequestMapping class to the class:

 @Controller @RequestMapping("/alerts") public class AlertsController { @RequestMapping(value="create", method=RequestMethod.GET) public void create(HttpServletRequest request, Model model) { } } 

This therefore works. I don't need @RequestMapping , but I do. Now everything is strange. I really wanted to display this in `/ profile / alerts', so I change it to the following:

 @Controller @RequestMapping("/profile/alerts") public class AlertsController { @RequestMapping(value="create", method=RequestMethod.GET) public void create(HttpServletRequest request, Model model) { } } 

I get 404 when switching to profile/alerts/create , but for some reason it still maps to /alerts/create ?!?!?!

I change it to:

 @Controller @RequestMapping("foobar") public class AlertsController { @RequestMapping(value="create", method=RequestMethod.GET) public void create(HttpServletRequest request, Model model) { } } 

This is very strange and incredibly inconvenient. Does anyone have a way to fix this or even debug what happens?

+4
source share
2 answers

In your first fragment, you missed the lead / . It should be something like @RequestMapping(value="/create", method=RequestMethod.GET)

Now you need to change your third fragment,

 @Controller public class AlertsController { @RequestMapping(value="/profile/alerts/create", method=RequestMethod.GET) public void create(HttpServletRequest request, Model model) { } } 

Also, since you are creating your void method, which expects the DispatcherServlet to revert to the default view name "profile / alerts / create / create". And then it is combined with a suitable resolution. For instance,

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> 

And there you have 404, maybe.

+4
source

You can perform url matching both in class annotation and in more subtle form by methods. Class level annotations are added to method level annotations

 @Controller @RequestMapping(value = "/admin") public class AdminController { @RequestMapping(value = "/users", method = RequestMethod.GET) /* matches on /admin/users */ public string users() { ... } } 

This is very close to your original third snippet, except that you forgot the lead /.

0
source

All Articles