一:中文编码设置:
在tomcat9版本中,如果提交方式为post方式,则会出现乱码问题,此时在web.xml文件中配置中文编码过滤器即可
<filter>
<filter-name>character-filter</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>
</filter>
<filter-mapping>
<filter-name>character-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二:springmvc四种跳转方式:
<a href="${pageContext.request.contextPath}/a.do">1.请求转发页面</a>
<a href="${pageContext.request.contextPath}/b.do">2.请求转发action</a>
<a href="${pageContext.request.contextPath}/c.do">3.重定向</a>
<a href="${pageContext.request.contextPath}/d.do">4.重定向action</a>
@RequestMapping("/a")
public String a() {
return "main";
}
@RequestMapping("/b")
public String b() {
return "forward:/other.do";
} @RequestMapping("/c")
public String c() {
//会自动屏蔽视图解析器
return "redirect:/main.jsp";
} @RequestMapping("/d")
public String d() {
return "redirect:/other.do";
}
注意:放在web-inf目录下的动态资源,不能直接访问,只能通过请求转发的方式进行访问,当然重定向也不可以访问
三:springmvc默认的参数类型:
不需要去创建,直接拿来用即可:
1:HttpServletRuquest
2:HttpSetvletResponse
3:HttpSession
4:Model
5:Map
6:ModelMap
注意:map,model,modelmap和request一样,都是请求作用域进行数据传递,所以服务器的跳转必须的请求转发
@RequestMapping("/data")
public String data(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
HttpSession httpSession,
Model model,
Map map,
ModelMap modelMap) {
User user=new User("张三","20");
httpServletRequest.setAttribute("requset",user);
httpSession.setAttribute("session",user);
model.addAttribute("model",user);
map.put("mao",user);
modelMap.addAttribute("modelmap",user);
return "main";
}
request:${requset}<br>
session:${session}<br>
model:${model}<br>
map:${mao}<br>
modelmap:${modelmap}
从页面中带来的数据:${param.name} 对应下面这句:
<a href="${pageContext.request.contextPath}/data.do?name=zhang">携带数据</a>
注意:如果不是请求转发,如果是重定向,则只有session才有数据标签:return,String,springmvc,简单,user,使用,model,public,RequestMapping From: https://www.cnblogs.com/zhangtaibing/p/16600467.html