一.字符编码过滤器
在web.xml中配置Filter。
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二.传参
把内容写到方法(HandlerMethod)参数中,SpringMVC 只要有这个内容,注入内容。
基本数据类型参数,默认保证参数名称和请求中传递的参数名相同,如果请求参数名和方法参数名不对应使用@RequestParam(value="newname")赋值,如果方法参数是基本数据类型(不是封装类)可以通过@RequestParam(defaultValue="abc")设置默认值,防止没有参数时出现500的错误,如果强制某个参数必须有可以用@RequestParam(required=true)。
HandlerMethod中参数是对象类型,请求参数名和对象中属性名对应(get/set方法)。
请求参数中包含多个同名参数的获取方式,复选框传递的参数是多个同名参数,可以使用List集合获取。
请求参数中是对象.属性格式的,需要新建一个类,然后在控制器中形参用新建类。
<input type="text" name="peo.name"/>
<input type="text" name="peo.age"/>
public class Demo {private People peo;}
@RequestMapping("demo")
public String demo(Demo demo){return "main.jsp";}
请求参数中传递集合对象类型的参数,也是需要新建一个类,然后在控制器中形参用新建类。
<input type="text" name="peo[0].name"/>
<input type="text" name="peo[0].age"/>
<input type="text" name="peo[1].name"/>
<input type="text" name="peo[1].age"/>
public class Demo {private List<People> peo;}
@RequestMapping("demo")
public String demo(Demo demo){return "main.jsp";}
restful传值方式,可以简化jsp中参数编写格式,在jsp中设定特定的格式 。在控制器中,@RequestMapping中一定要和请求格式对应,{名称}中名称自定义名称,@PathVariable 获取,@RequestMapping 中内容,默认按照方法参数名称去寻找。
<a href="demo/123/abc">跳转</a>
@RequestMapping("demo/{id}/{name}")
public String demo(@PathVariable String name,@PathVariable("id") int age){return "main.jsp";}
三.跳转方式
默认跳转方式是请求转发。
设置返回值字符串内容,添加redirect:资源路径是重定向,添加forward:资源路径或省略forward是转发。
四.视图解析器
SpringMVC会提供默认视图解析器。
程序员自定义视图解析器。
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
如果希望不执行自定义视图解析器,在方法返回值前面添加forward:或redirect:。
五.@ResponseBody
在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转。
在方法上再添加一个@ResponseBody(恒不跳转)。如果返回值满足 key-value 形式(对象或 map),把响应头设置为 application/json;charset=utf-8,把转换后的内容输出流的形式响应给客户端;如果返回值不满足 key-value,例如返回值为 String,把相应头设置为 text/html,把方法返回值以流的形式直接输出,如果返回值包含中文,出现中文乱码,produces 表示响应头中 Content-Type 取值。
@RequestMapping(value="demo",produces="text/html;charset=utf-8")
@ResponseBody
public String demo() throws IOException{
People p = new People();
p.setAge(12);
p.setName("张三");
return p;
}
底层使用 Jackson 进行 json 转换,在项目中一定要导入 jackson 的 jar 3.1,spring4.1.6 对 jackson 不支持较高版本,jackson 2.7 无效。
六.JSP九大内置对象和四大作用域复习
JSP九大内置对象
四大作用域
page:在当前页面不会重新实例化。
request:在一次请求中同一个对象,下次请求重新实例化一个request对象。
session:一次会话,只要客户端Cookie中传递的Jsessionid不变,session不会重新实例化(不超过默认时间),实际有效时间是当浏览器关闭Cookie失效和在默认时间或者自定义时间范围内无任何交互,可以在tomcat的web.xml中配置。
<session-config>
<session-timeout>30</session-timeout>
</session-config>
application:只有在tomcat启动项目时才实例化,关闭tomcat时销毁application。
七.SpringMVC作用域传值的几种方式
使用原生的Servlet,在HandlerMethod参数中添加作用域对象。
@RequestMapping("demo1")
public String demo1(HttpServletRequest abc,HttpSession sessionParam){
//request 作用域
abc.setAttribute("req", "req 的值");
//session 作用域
HttpSession session = abc.getSession();
session.setAttribute("session", "session 的值");
sessionParam.setAttribute("sessionParam", "sessionParam 的值");
//appliaction 作用域
ServletContext application = abc.getServletContext();
application.setAttribute("application", "application 的值");
return "/index.jsp";
}
使用Map集合,把map中内容放在request作用域中,spring会对map集合通过BindingAwareModelMap进行实例化。
@RequestMapping("demo2")
public String demo2(Map<String,Object> map){
System.out.println(map.getClass());
map.put("map","map 的值");
return "/index.jsp";
}
使用SpringMVC中Model接口,把内容最终放入到request作用域中。
@RequestMapping("demo3")
public String demo3(Model model){
model.addAttribute("model", "model 的值");
return "/index.jsp";
}
使用SpringMVC中ModelAndView类。
@RequestMapping("demo4")
public ModelAndView demo4(){
//参数,跳转视图
ModelAndView mav = new ModelAndView("/index.jsp");
mav.addObject("mav", "mav 的值");
return mav;
}