在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個問題,我一直沒能解決,但:
- 我一直無法映射父目錄
/settings/
調用displayRoles()方法。如果我將displayRoles方法的映射更改爲@RequestMapping(value = {"settings","settings/","settings/roles"}, method = RequestMethod.GET)
,然後重新生成並瀏覽到URL中的/設置,則會出現404錯誤。爲什麼我無法映射到「設置」父文件夾? - 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>
你可以發佈網頁嗎?xml內容的servlet映射 – kalyan