SpringMVC重定向和转发
九. 重定向和转发
默认return 是转发 自动拼接前缀和后缀
//转发 @RequestMapping("/testForword") public String testForword() { return "success"; }
需要手动重定向
return “redirect:完整路径”
@RequestMapping("/testRedirect") public String testRedirect() { //重定向 只能访问webapp 下的页面 不能访问 web-inf下面的页面 //return "redirect:/WEB-INF/success.jsp"; //重定向 需要加redirect: 已经完整路径(前缀和后缀只是给转发用的) return "redirect:/reg.jsp"; }
十.解决中文乱码
1.在 web.xml文件中添加,在DispatherServlet前
<!-- 过滤器 设置编码 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.遇到ajax请求时,还要添加:
@RequestMapping(value="/getMajorList2",produces = "text/html;charset=utf-8")
3.post请求时出乱码,在server.xml文件中添加
URIEncoding="utf-8" <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="utf-8" /> 设置禁止缓存: //设置页面不缓存 response.setContentType("application/json"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache");