springmvc(6)乱码及restful风格

1.乱码的解决:通过过滤器解决乱码:springmvc 提供 CharacterEncodingFilter解决post乱码:

    <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>

 

如果get方式乱码:

a)修改tomcat的配置解决

在tomcat的conf文件夹中找server.xml:

改之前:

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

 

 

改之后:

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
      URIEncoding="UTF-8"
               redirectPort="8443" />

 

请求:http://localhost:8080/hello?name=李四,参数可以通过任何方式获取:

如:

@RequestMapping("/hello")
    public String hello(String name,ModelMap modelMap){
        System.out.println(name);
        //相当于request.setAttribute("msg","modelMap");
        modelMap.addAttribute("msg",name);
        return "index.jsp";
    }

 

b)自定义过滤器解决

public class EncodingFilter implements Filter {
    private String encoding = "";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 拦截所有的请求,解决全站中文乱码,指定request和response的编码
        request.setCharacterEncoding(encoding);   // 只对消息体有效  也就是只对post有效,get的参数是放在地址栏里的
        servletResponse.setContentType("text/html;charset=utf-8");
        // 对 request 进行包装
        CharacterRequest characterRequest = new CharacterRequest(request);
        filterChain.doFilter(characterRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

public class CharacterRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request;

    public CharacterRequest(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    // 子类继承父类一定会覆写一些方法,此处用于重写getParameter()方法
    public String getParameter(String name) {
        // 调用被包装对象getParameter()方法,获得请求参数
        String value = super.getParameter(name);
        if (value == null) {
            return null;
        }
        String method = super.getMethod(); // 判断请求方式
        if ("get".equalsIgnoreCase(method)) {
            try {
                value = new String(value.getBytes("iso-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return value; // 解决乱码后返回结果
    }
}

@RequestMapping("/hello")
public String hello(HttpServletRequest request,ModelMap modelMap){
String name = request.getParameter("name");
System.out.println(name);
//相当于request.setAttribute("msg","modelMap");
modelMap.addAttribute("msg",name);
return "index.jsp";
}
 

 

用了这种方式,get请求时:http://localhost:8080/hello?name=李四,得到参数的方式一定需要通过request.getParameter(),不能通过其他方式,因为我们只是修改了getParameter方式获取参数,别的方式没有修改,所以别的方式无法使用。

2.restful风格的url:

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

正常连接:http://localhost:8080/delete?id=123

restful风格:http://localhost:8080/delete/123

代码:

 @RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        System.out.println(id);
        return "/index.jsp";
    }

 

这个也可以写在前面::http://localhost:8080/123/delete

 @RequestMapping("/{id}/delete")
    public String delete(@PathVariable int id){
        System.out.println(id);
        return "/index.jsp";
    }

 

可以传递多个值:http://localhost:8080/aa/123/delete

   @RequestMapping("/{uuid}/{id}/delete")
    public String delete(@PathVariable int id,@PathVariable String uuid){
        System.out.println(id);
        System.out.println(uuid);
        return "/index.jsp";
    }

 

值会根据变量名称进行一一对应,不会因为位置的不同而传错数值,也可以直接指定名称如:

 @RequestMapping("/{uuid}/{id}/delete")
    public String delete(@PathVariable("uuid") int id,@PathVariable("id") String uuid){
        System.out.println(id);
        System.out.println(uuid);
        return "/index.jsp";
    }

 

比如此时将uuid的值赋给id,id的值赋给uuid

3.一个controller通过参数来到达不同的处理方法:

提交的url:http://localhost:8080/hello2?method

@Controller
@RequestMapping("hello2")
public class Hello2Controller {

    @RequestMapping(params = "method")
    public String hello(String name,ModelMap modelMap){
        //相当于request.setAttribute("msg","modelMap");
        System.out.println("method");
        return "index.jsp";
    }
}

 

posted @ 2019-06-16 21:38  yuby  阅读(303)  评论(0编辑  收藏  举报