第五讲 乱码及RESTful风格

本文介绍如何在SpringMVC中解决GET和POST请求的乱码问题,并提供自定义过滤器的实现方法。同时,文章还探讨了RESTful风格URL的设计及其优势,并展示了如何在同一Controller内根据不同参数调用不同方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、乱码的解决-通过过滤器来解决乱码

  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 int uid)


三、同一个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";
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值