1。乱码的解决通过过滤器来解决乱码,Spring MVC中提供CharacterEncodingFilter来解决post乱码
配置过滤器:
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决get乱码的方式:
第一种修改tomcat的配置文件:server.xml
<Connector connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443" <span style="color:#FF6666;">URIEncoding="UTF-8"</span>/>
第二种自定义乱码解决的过滤器。
2.restful风格的url
优点:轻量级、安全、效率高
//http://localhost:8088/SpringMvcData/999/delete.do
@RequestMapping("/{id}/delete")
public String delete(@PathVariable int id){
System.out.println(id);
return "index.jsp";
}
3.同一个controller通过参数调用不同的方法:
访问url:http://localhost:8088/SpringMvcData/delete.do?method=add
代码:(其中还可以指定get方式或者是post方式请求,不写默认都可以)
@RequestMapping(params="method=add", method=RequestMethod.GET)
public String add(){
System.out.println("add");
return "index.jsp";
}