开发者

In Spring MVC, how can I map nested URLs such as /settings/, /settings/users/, and /settings/users/delete?

开发者 https://www.devze.com 2023-03-16 04:56 出处:网络
In Spring 3 MVC, I have a controller that I call SettingsController, and it has methods such as displayUsers() for displaying a list of users, saveUser(), and deleteUser().SettingsCo开发者_如何学Gonto

In Spring 3 MVC, I have a controller that I call SettingsController, and it has methods such as displayUsers() for displaying a list of users, saveUser(), and deleteUser(). SettingsCo开发者_如何学Gontoller also controls roles and other things.

I'd love to be able to use URL routing such that /settings/users would call displayUsers(), /settings/users/save would call saveUser(), and /settings/users/delete would call deleteUser().

My code is below, and I'm getting the 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)
...


It looks to me like you've set up your controller correctly. As you pointed out, the problem might be in how Spring parses annotations upon start up.

How did you configure Sprint to parse annotations such as @Controller? Do you explicitly set up any sort of HandlerMapping? If you use <context:component-scan>, then it registers a DefaultAnnotationHandlerMapping for you.

The good news is that you can chain multiple handler mapping classes together. The DispatcherServlet will check each one in the order that you specify via the order property of the handler mapping beans (in other words, use the order property to indicate the precedence of your handlers).

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


What about using just one method checking mode?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号