在Spring MVC中,如何映射嵌套URL,例如/ settings /,/ settings / users /和/ settings / users / delete? [英] In Spring MVC, how can I map nested URLs such as /settings/, /settings/users/, and /settings/users/delete?

查看:136
本文介绍了在Spring MVC中,如何映射嵌套URL,例如/ settings /,/ settings / users /和/ settings / users / delete?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring 3 MVC中,我有一个控制器,我调用了SettingsController,它有一些方法,如displayUsers(),用于显示用户列表,saveUser()和deleteUser()。 SettingsContoller还控制角色和其他东西。

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(). SettingsContoller also controls roles and other things.

我希望能够使用URL路由,这样/ settings / users会调用displayUsers(),/ settings / users / save会调用saveUser(),而/ settings / users / delete会调用deleteUser()。

我的代码如下,我收到代码后面的错误消息。我究竟做错了什么?谢谢!

@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
    }

}

错误:

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)
...


推荐答案

在我看来你已经正确设置了你的控制器。正如您所指出的,问题可能在于Spring如何在启动时解析注释。

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.

您是如何配置Sprint来解析注释的,例如 @控制器?你明确设置了任何种类的 HandlerMapping 吗?如果您使用< context:component-scan> ,那么为您注册DefaultAnnotationHandlerMapping

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.

好消息是您可以将多个处理程序映射类链接在一起 DispatcherServlet 将通过处理程序映射bean的order属性(换句话说,使用order属性指示处理程序的优先级) 。

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).

因此,抛出< bean class =org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping/> 进入您的配置并根据需要设置其订单属性。

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

这篇关于在Spring MVC中,如何映射嵌套URL,例如/ settings /,/ settings / users /和/ settings / users / delete?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆