Spring mvc &引用;HTTP状态404“;还有一个额外的“;“错误的道路”;在请求路径中
这是我的实习春季项目。我无法在SpringMVC拦截器中重定向到正确的页面 spring-mvc.xmlSpring mvc &引用;HTTP状态404“;还有一个额外的“;“错误的道路”;在请求路径中,spring-mvc,http-status-code-404,Spring Mvc,Http Status Code 404,这是我的实习春季项目。我无法在SpringMVC拦截器中重定向到正确的页面 spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.spr
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<description>Spring MVC Configuration</description>
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
<context:component-scan base-package="com.huahua.my.shop" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${web.view.prefix}"/>
<property name="suffix" value="${web.view.suffix}"/>
</bean>
<mvc:resources mapping="/**/static/**" location="/static/" cache-period="31536000"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/static/**"/>
<mvc:exclude-mapping path="/loginOut"/>
<bean class="com.huahua.my.shop.web.admin.web.intercepter.LoginIntercepter" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/login"/>
<bean class="com.huahua.my.shop.web.admin.web.intercepter.PermissionIntercepter" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
这是我的拦截器
public class LoginIntercepter implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o ) throws Exception {
TbUser tbUser =(TbUser) httpServletRequest.getSession().getAttribute(ConstantUtils.SESSION_USER); //SESSION_USER = user
System.out.println(httpServletRequest.getRequestURL());
if (tbUser == null){
httpServletResponse.sendRedirect("login");
}
return true;
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}
这是LoginController
@Controller
public class LoginController {
@Autowired
private TbUserService tbuserService ;
@RequestMapping(value = {"","login"},method = RequestMethod.GET)
public String login(){
return "login" ;
}
@RequestMapping(value = "login",method = RequestMethod.POST)
public String login(@RequestParam(required = true) String email ,
@RequestParam(required = true ) String password ,
@RequestParam(required = false) String isRemember,
HttpServletRequest request,
Model model){
TbUser user = tbuserService.loginRight(email, password);
isRemember = isRemember == "on" ? "checked" : null ;
if (user != null ){
request.getSession().setAttribute(ConstantUtils.SESSION_USER,user);
request.getSession().setAttribute("remember",isRemember);
return "redirect:/main" ;
}
else {
model.addAttribute("message","username or password is wrong");
return "login" ;
}
}
http://localhost:8080/user/list
,我成功地输入了user\u list.jsp
http://localhost:8080/login
但是我得到了这个路径http://localhost:8080/user/login
/user
和UserController中的@RequestMapping(value=“/user”)
之间有什么关系正如API文档对HttpServletResponse#sendRedirect的说明: 使用指定的 重定向位置URL并清除缓冲区。缓冲区将是 用此方法的数据集替换。调用此方法集 SC_的状态代码已找到302(已找到)。这种方法可以接受 相对网址;servlet容器必须将相对URL转换为 将响应发送到客户端之前的绝对URL如果 位置是相对的,没有前导“/”,容器解释它 相对于当前请求URI。如果位置是相对的 使用前导“/”时,容器将其解释为相对于 servlet容器根 因此,由于您指定了相对URL,因此相对于当前请求URL进行解析
/users/list
因此,您需要将其更改为:
httpServletResponse.sendRedirect("/login");
httpServletResponse.sendRedirect("/login");