2011-07-18 113 views
0

在Spring 3 MVC中,我有一個控制器,我稱之爲RolesController,它具有用於顯示角色列表(saveRole()和deleteRole())的諸如displayRoles()等方法。在Spring MVC中,我怎樣才能讓@RequestMapping工作?

目前,我成功使用@RequestMapping註釋來路由/ settings/roles /以調用displayRoles()方法,/ settings/roles/save /來調用saveRole()方法等等。

我的代碼如下,它的工作原理。

@Controller 
public class RolesController { 

    @Transactional 
    @RequestMapping(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 
    } 

} 

有2個問題,我一直沒能解決,但:

  1. 我一直無法映射父目錄/settings/調用displayRoles()方法。如果我將displayRoles方法的映射更改爲@RequestMapping(value = {"settings","settings/","settings/roles"}, method = RequestMethod.GET),然後重新生成並瀏覽到URL中的/設置,則會出現404錯誤。爲什麼我無法映射到「設置」父文件夾?
  2. URL中的尾部斜槓似乎是必需的。例如,轉到設置/角色/作品,但訪問設置/角色會導致404頁面未找到錯誤。

我在做什麼錯?謝謝! -Ryan


P.S.下面是一些額外的信息...的情況下,這會有所幫助:

在我的applicationContext-mvc.xml文件,我有(其中包括代碼):

<!-- 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> 

+0

你可以發佈網頁嗎?xml內容的servlet映射 – kalyan

回答

0

嘗試以下...

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

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

好吧,我剛剛嘗試過,並沒有解決任何問題。我希望/ settings /和/ settings/roles /都調用displayRoles()方法,如果不需要尾部斜槓,我也會喜歡。 – Ryan

1

對於尾部斜線問題,這可能是一個版本細節:有一些尾隨s (見http://jira.springframework.org/browse/SPR-7064https://issues.springsource.org/browse/SPR-5636),它們在Spring 3.0.3和更高版本中被標記爲固定的。

對於/settings映射問題,你可能需要做一些像@Bill那樣:

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

我試過你的想法/設置,我仍然得到404錯誤。我不確定我運行的是Spring 3的哪個版本,或者如何弄清楚/升級。 – Ryan

+0

只需從http://www.springsource.org/download重新下載3.05,你就可以確定你擁有什麼。 – Femi

1

如上所述,可能已被錯誤使用舊版本,但因爲沒有答案似乎被接受,這是什麼適用於我...

它幫助我創建一個indexAction()與可選的斜槓作爲給定的控制器的默認操作。在這種情況下,displayRoles()可以被用作默認:

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

如果/設置和/設置/角色應該指的是相同的方法,那麼這樣的事情解決了這個問題:

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

適用於Spring 4.0.7。

相關問題