我在我的项目中使用spring mvc3,我对请求映射模式(url中的最后一个斜杠)感到困惑
以下面的控制器方法为例:
@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
public String edit(@PathVariable int id, Model model) {
model.addAttribute(postDao.query(id));
return "posts/edit";
}
获取url“ http://localhsot/app/posts/3/edit
”时有效,但获取url“”时无法使用该方法http://localhsot/app/posts/3/edit/
。
我知道我可以像这样设置请求映射值:
@RequestMapping(value = {"/{id}/edit","/{id}/edit/"})
但我想知道是否有其他解决方案?Rails 似乎会忽略 url 中的最后一个斜杠。
更新:
servlet-context.xml:
<mvc:annotation-driven />
<mvc:view-controller path="/" view-name="home" />
<context:component-scan base-package="com.king.controller" />
<mvc:resources mapping="/res/**" location="/res/" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
web.xml
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>modelServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>modelServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>modelServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>