SpringMVC重定向和转发

九. 重定向和转发
​ 默认return 是转发 自动拼接前缀和后缀

1
2
3
4
5
6
//转发
@RequestMapping("/testForword")
public String testForword()
{
    return "success";
}

  

需要手动重定向

return “redirect:完整路径”

1
2
3
4
5
6
7
8
9
@RequestMapping("/testRedirect")
public String testRedirect()
{
     //重定向  只能访问webapp 下的页面 不能访问 web-inf下面的页面
    //return "redirect:/WEB-INF/success.jsp";
     
    //重定向 需要加redirect: 已经完整路径(前缀和后缀只是给转发用的)
    return "redirect:/reg.jsp";
}

  

十.解决中文乱码
1.在 web.xml文件中添加,在DispatherServlet前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- 过滤器  设置编码 -->
<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请求时,还要添加:

1
@RequestMapping(value="/getMajorList2",produces = "text/html;charset=utf-8")

  

3.post请求时出乱码,在server.xml文件中添加

1
2
3
4
5
6
7
8
9
​ 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");

  

posted @   呆萌老师  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示