第五讲 乱码及RESTful风格

来源:互联网 发布:生化危机6 for mac 编辑:程序博客网 时间:2024/06/14 01:33
一、乱码的解决-通过过滤器来解决乱码

  1. Spring MVC中提供了org.springframework.web.filter.CharacterEncodingFilter来解决POST乱码

  <filter>
    <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

     @RequestMapping(value="/hello")
     public String user(@RequestParam("uname") Stringuname, ModelMap model)
                throws UnsupportedEncodingException {

           model.addAttribute("name",uname);
           return "index.jsp";

     }

   model.addAttribute("name", uname);直接使用即可。

  1. 如果是GET方式乱码

          
 修改Tomcat的 server.xml配置文件解决












 自定义乱码解决的过滤器
注意:GET请求的乱码解决是通过
value = new String(value.getBytes("iso-8859-1"), encoding);
POST请求则是通过
// 设置request编码
httpRequest.setCharacterEncoding(encoding);
// 设置response编码
httpResponse.setContentType("text/html;charset=" + encoding);



     @RequestMapping(value="/hello")
     public String user(@RequestParam("uname") String uname, ModelMap model,
                HttpServletRequest request)
                throws UnsupportedEncodingException {

           model.addAttribute("name", request.getAttribute("uname"));
           return "index.jsp";

     }

  <filter>
    <filter-name>CharacterEncoding</filter-name>
    <filter-class>com.liujie.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncoding</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>
注意:<url-pattern>*.do</url-pattern>,不能够拦截/*


二、RESTful风格的URL

      优点:轻量级、安全、效率高

     //http://localhost:8080/springmvc/1/123/hello.do
     @RequestMapping(value="/{id}/{uid}/hello")
     public String user(@PathVariable("id") int id, @PathVariable("uid") int uid) {

           System.out.println(id);
           System.out.println(uid);
           return "/index.jsp";

     }

     @RequestMapping(value="/{id}/{uid}/hello")
     public String user(@PathVariable int id, @PathVariable intuid)


三、同一个Controller通过参数来到达不同的处理方法-不重要

提交URL:
处理代码:
@Controller
@RequestMapping("/hello")
public class HelloController {

     //http://localhost:8080/springmvc/hello.do?method=add
     @RequestMapping(params="method=add", method=RequestMethod.GET)
     public String add() {
           System.out.println("add");
           return "redirect:/index.jsp";
     }

     @RequestMapping(params="method=update", method=RequestMethod.GET)
     public String update() {
           System.out.println("update");
           return "redirect:/index.jsp";
     }

     @RequestMapping(params="method=delete", method=RequestMethod.GET)
     public String delete() {
           System.out.println("delete");
           return "redirect:/index.jsp";
     }
}
原创粉丝点击