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 = "";@Overridepublic void init(FilterConfig filterConfig) throws ServletException {encoding = filterConfig.getInitParameter("encoding");}@Overridepublic 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);}@Overridepublic 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";}
}

转载于:https://www.cnblogs.com/yuby/p/11033075.html

springmvc(6)乱码及restful风格相关推荐

  1. springMVC_07乱码及restful风格

    乱码的解决 通过过滤器解决乱码问题:CharacterEncodingFilter 配置web.xml文件 <filter><filter-name>encoding</ ...

  2. SpringMVC(三)Restful风格及实例、参数的转换

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Restful风格 1.Restful风格的介绍 Restful 一种软件架构风格.设计风格,而不是 ...

  3. SpringMVC基础学习之Restful风格的简单使用

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 这个SpringM ...

  4. SpringMVC+Json构建基于Restful风格的应用

    为什么80%的码农都做不了架构师?>>>    https://git.oschina.net/zjg23/SpringMVCRestful.git 转载于:https://my.o ...

  5. SpringMVC响应Restful风格请求404

    一.问题 在学习Springmvc时,使用Restful风格的url,页面提示404错误.为找到原因,编写一个简单的Restful测试用例如下: jsp页面: <a href="use ...

  6. SpringMVC3----@Controller注解、RestFul风格的讲解和应用、SpringMVC的接受请求参数、网页跳转方式和数据回显、乱码问题

    目录 7 Controller类的写法 7.1 继承Controller接口 7.2 一个简单通过@Controller注解实现的程序. 7.3 @RequestMapping 8 RestFul风格 ...

  7. Day73.SpringMVC案例:影院系统、使用Restful风格重构

    目录 springMVC:影院系统 一.准备SpringMVC环境 二.首页显示所有电影 三.影院案例-删除指定电影信息 四.影院案例-添加新电影 五.影院案例-修改指定电影信息 六.总结 七.Res ...

  8. SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful

    资源:尚学堂 邹波 springmvc框架视频 一.提交数据的处理 1.提交的域名称和处理方法的参数一致 (1)提交的数据:http://localhost:8080/data/hello.do?na ...

  9. 迟到的总结(三)--springmvc的系统学习之数据的处理,乱码及restful

    前序:本篇主要是讲后台处理前台页面提交过来的数据的几种方式,后台传递数据到页面的方式.以及乱码的处理和restful. 资源:尚学堂 邹波 springmvc框架视频 一.提交数据的处理 1.提交的域 ...

最新文章

  1. spring-amqp整合rabbitmq消费者配置和代码
  2. nginx常用功能全揭秘(内附福利!!!)
  3. 初识 ::after
  4. 如何将cocos2d-x项目打包成一个.exe
  5. tomcat,zookeeper,activeMQ,Kafka设置jvm参数
  6. postman使用记录,带cookie的get请求和传json对象的post请求示范
  7. 群晖DS220+ 应用小笔记
  8. Mysql调优大全梳理(涵盖90%需要调优的场景)
  9. 常用元器件使用方法4:一种Micro-SIM卡连接器的使用方法
  10. 利用jspx解决jsp后缀被限制拿shell
  11. “某某某”was not declared in this scope?报错原因。
  12. NO.4 项目无法一键打包?自己写个shell脚本吧
  13. 云计算的认识和看法_对云计算的看法. 我对云计算的认识
  14. 微信支付-vue 实现微信支付-前端篇
  15. 三、uboot简单介绍
  16. 怎么把pdf转为html?PDF转HTML转换器推荐
  17. Python OpenCV 实现魔方识别+复原
  18. 跨界转型 打造大数据旗舰
  19. 国内外做视频会议比较牛的公司有哪些?
  20. 块级元素 div水平居中 垂直居中

热门文章

  1. 【OpenCV】OpenCV函数精讲之 -- Mat和IplImage之间的相互装换(OpenCV2.0和OpenCV3.0)
  2. 深度学习实战 | 使用Kera预测人物年龄
  3. 南京的学员看过来 | NVIDIA DLI深度学习入门培训
  4. DWA泊车算法的实现
  5. labview串口数据采集并显示_一种NB-IoT冶金节点温度采集与远程监测系统的设计...
  6. LL1分析构造法_16条数学得分法,想提分快来看!
  7. 将之前写完的猜数字游戏改为通过javabean_42个宝宝益智游戏良心整理!让宝宝快人一步更聪明...
  8. excel和mysql php_php将mysql数据库和Excel相互导入和导出的方法
  9. html页面怎么引用通用的头部,html 如何引入一个公共的头部和底部
  10. k8s边缘节点_KubeEdge v0.2发布,全球首个K8S原生的边缘计算平台开放云端代码