1.Spring mvc乱码解决
乱码一般通过过滤器来解决:springmvc中提供CharacterEncodingFilter解决post乱码问题
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果是get方式乱码
a)修改tomcat的配置解决
b)自定义乱码解决的过滤器
2. restful风格的url
优点:轻量级,安全,效率高
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping("/{id}/{uid}/delete")
public String hello(HttpServletRequest req,HttpServletResponse res,
@PathVariable String id,@PathVariable String uid) throws IOException, ServletException {
System.out.println("id=" + id + "uid=" + uid);
return "/index.jsp";
}
}
通过同一个controller,通过参数来到达不同的处理方法(不建议使用)
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping(params="method=add")
public String add(HttpServletRequest req,HttpServletResponse res,
@RequestParam("name") String name) throws IOException, ServletException {
System.out.println(name);
return "redirect:/index.jsp";
}
@RequestMapping(params="method=update")
public String update(HttpServletRequest req,HttpServletResponse res,
@RequestParam("name") String name) throws IOException, ServletException {
System.out.println(name);
return "redirect:/index.jsp";
}
@RequestMapping(params="method=delete")
public String delete(HttpServletRequest req,HttpServletResponse res,
@RequestParam("name") String name) throws IOException, ServletException {
System.out.println(name);
return "redirect:/index.jsp";
}