In Spring MVC, how can I map sub-URLs like / settings /, / settings / users / and / settings / users / delete?

In Spring 3 MVC, I have a controller that I call SettingsController, and it has methods like displayUsers () to display a list of users, saveUser () and deleteUser (). SettingsContoller also controls roles and other things.

I would like to use URL routing so that / settings / users call displayUsers (), / settings / users / save, call saveUser (), and / settings / users / delete call deleteUser ().

My code is below and I get an error message that follows the code. What am I doing wrong? Thanks!

@Controller @RequestMapping("/settings") public class SettingsController { @Transactional @RequestMapping(value = {"/users/save"}, method = {RequestMethod.POST}) public ModelAndView saveUser(details removed){ //details removed } @RequestMapping(value = {"/users/delete"}, method = {RequestMethod.POST}) public ModelAndView deleteUser(details removed){ //details removed } @RequestMapping(value = {"/users"}, method = RequestMethod.GET) public ModelAndView settingsUsers(details removed){ //details removed } } 

Error:

 HTTP ERROR: 500 Could not resolve view with name 'settings/users/delete' in servlet with name 'spring' RequestURI=/das-portal/srv/settings/users/delete Caused by: javax.servlet.ServletException: Could not resolve view with name 'settings/users/delete' in servlet with name 'spring' at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029) ... 
+4
source share
2 answers

It seems to me that you have correctly configured your controller. As you noted, the problem may be how Spring parses annotations at startup.

How did you configure Sprint to parse annotations like @Controller ? Have you explicitly configured any type of HandlerMapping ? If you use <context:component-scan> , then the DefaultAnnotationHandlerMapping file is registered for you .

The good news is that you can combine multiple handler mapping classes . DispatcherServlet will check each of them in the order that you specify using the beans handler's display order property (in other words, use the order property to indicate the priority of your handlers).

So, throw <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> into your configuration and, if necessary, set its order property.

+2
source

How to use one method verification mode?

 @RequestMapping(value = "/users/{action}", method = RequestMethod.POST) public String userAction(@PathVariable String action, ...) { if (mode.equals("save")) { //your save code here } } 
+1
source

Source: https://habr.com/ru/post/1201033/


All Articles