gpt4 book ai didi

spring - spring mvc3中的请求映射url模式

转载 作者:行者123 更新时间:2023-12-04 18:13:22 24 4
gpt4 key购买 nike

我在我的项目中使用spring mvc3,我对请求映射模式(url中的最后一个斜杠)感到困惑

以下面的 Controller 方法为例:

@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
public String edit(@PathVariable int id, Model model) {
model.addAttribute(postDao.query(id));
return "posts/edit";
}

获取网址“ http://localhsot/app/posts/3/edit”时有效,但获取网址“ 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>

最佳答案

我认为您正在尝试解决错误的问题。

如果你匹配一个带有和不带斜杠的 URL,你会因为 duplicate content 而从搜索引擎中得到一个不好的评价。 .

我要做的是添加一个过滤器,它使用状态代码 HttpServletResponse.SC_MOVED_PERMANENTLY 发送所有请求而不使用尾部斜杠 带有尾部斜杠的重定向(反之亦然)

这是这种过滤器的最小实现:

public class CanonicalUrlFilter implements Filter {
@Override
public void init(final FilterConfig filterConfig) throws ServletException { }

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest hsr = (HttpServletRequest) servletRequest;
if (hsr.getMethod().equals("GET") && !hsr.getRequestURI().endsWith("/") && (hsr.getQueryString() == null)) {
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.sendRedirect(hsr.getRequestURI() + "/");
return;
}
}

filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() { }
}

关于spring - spring mvc3中的请求映射url模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12173854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com