开发者

In Spring MVC, how can I get @RequestMapping to work?

In Spring 3 MVC, I have a controller that I call RolesController, and it has methods such as displayRoles() for displaying a list of roles, saveRole(), and deleteRole().

Currently, I'm successfully using @RequestMapping annotations to route /settings/roles/ to call the displayRoles() method, /settings/roles/save/ to call the saveRole() method, and so on.

My code is below, and it works.

@Controller
public class RolesController {

    @Transactional
    @Reques开发者_如何学编程tMapping(value = {"settings/roles/save"}, method = {RequestMethod.POST})
    public ModelAndView saveRole(details removed){
        //details removed
    }

    @RequestMapping(value = {"settings/roles/delete"}, method = {RequestMethod.POST})
    public ModelAndView deleteRole(details removed){
       //details removed
    }

    @RequestMapping(value = {"settings/roles"}, method = RequestMethod.GET)
    public ModelAndView displayRoles(details removed){
      //details removed
    }

}

There are 2 problems that I haven't been able to fix, though:

  1. I've been unable to map the parent directory /settings/ to call the displayRoles() method. If I change the mapping of the displayRoles method to @RequestMapping(value = {"settings","settings/","settings/roles"}, method = RequestMethod.GET) and then rebuild and browse to /settings in the URL, I get a 404 error. Why can't I map to the "settings" parent folder?
  2. Trailing slashes in the URL seem to be required. E.g., going to settings/roles/ works, but visiting settings/roles results in a 404 Page Not Found error.

What am I doing wrong? Thanks! -Ryan


P.S. Here is some extra info... in case this helps:

In my applicationContext-mvc.xml file, I have (among other code):

<!-- Maps request paths to @Controller classes; e.g. a path of /login looks for a controller named LoginController -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
    <property name="order" value="1" />
</bean>      
<!-- If no @Controller match, look for @RequestMapping match-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="order" value="2" />
    <property name="defaultHandler">
        <!-- If no @RequestMapping match, map path to a view to render; e.g. the "/intro" path would map to the view named "intro" -->
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
</bean>


For the trailing slash issue, this may be a version detail: there are a number of trailing slash bugs (see http://jira.springframework.org/browse/SPR-7064 and https://issues.springsource.org/browse/SPR-5636) that are marked as fixed in Spring 3.0.3 and higher.

For the /settings mapping issue, you may need to do something like @Bill did:

@Controller
@RequestMapping(value = {"/settings"})
public class RolesController {

    @Transactional
    @RequestMapping(value = {"/roles/save"}, method = {RequestMethod.POST})
    public ModelAndView saveRole(details removed){
        //details removed
    }

    @RequestMapping(value = {"/roles/delete"}, method = {RequestMethod.POST})
    public ModelAndView deleteRole(details removed){
       //details removed
    }

    @RequestMapping(value = {"/","/roles"}, method = RequestMethod.GET)
    public ModelAndView displayRoles(details removed){
      //details removed
    }
}


As mentioned above, might have been bug with older versions, but since no answer seems to be accepted, this is what works for me...

It helped me to create an indexAction() with optional slash that serves as the default action for given Controller. In this case, displayRoles() can be used as the default:

@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public ModelAndView displayRoles(details removed) {
  //details removed
}

If /settings and /settings/roles should refer to the same method, then something like this solves the problem:

@Controller
@RequestMapping(value = "/settings")
public class RolesController {

    @Transactional
    @RequestMapping(value = {"/roles/save"}, method = {RequestMethod.POST})
    public ModelAndView saveRole(details removed) {
        //details removed
    }

    @RequestMapping(value = {"/roles/delete"}, method = {RequestMethod.POST})
    public ModelAndView deleteRole(details removed) {
       //details removed
    }

    @RequestMapping(value = {"", "/", "/roles", "/roles/"}, method = RequestMethod.GET)
    public ModelAndView displayRoles(details removed) {
      //details removed
    }
}

Works fine with Spring 4.0.7.


Try the following...

    @Controller
    @RequestMapping("/settings/roles")
    public class RolesController {

        @Transactional
        @RequestMapping(value = "/save", method = RequestMethod.POST)
        public ModelAndView saveRole(details removed){
            //details removed
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜