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. 单机 “5千万以上“ 工业级 LRU cache 实现
  2. 3dsmax 长动画导入 three.js 转变成 多个动画
  3. 用NanoPi neo制作网络音箱了解一下?
  4. HTML高亮标签<mark></mark>
  5. 【Qt】Qt中QJsonDocument 类
  6. well 这是第一次记录
  7. mysql被除数为0不报错_SQLServer中进行sql除法运算结果为小数时显示0的解决方案...
  8. html5与css3基础教程课件,揭秘HTML5和CSS3教学幻灯片.ppt
  9. java web插件下载_javaweb开发1.环境配置(javaweb插件下载及tomact在eclips中配置)
  10. 手机访问电脑虚拟服务器,User Agent Switcher插件使用教程【电脑模拟手机访问网站】...
  11. 视频会议十大开源项目排行
  12. h5案例分享:王中军个人画展:藝術東西,玄如藝術
  13. clientX,clientY,screenX,screenY,offsetX,offsetY 区别测试
  14. TESS NG微观交通仿真软件二次开发接口如何规划
  15. 解决 Macbook 连接蓝牙鼠标卡顿、飘的现象
  16. 数据分析师的日常工作是什么?
  17. python基础学习与应用
  18. c语言怎么编写数控g指令,数控车床编程--G 代码 M代码命令
  19. Flutter中获取监听屏幕方向、锁定屏幕方向
  20. CDC::CreateCompatibleDC 的整理(转)

热门文章

  1. 【TensorFlow】TensorFlow从浅入深系列之七 -- 教你使用验证数据集判断模型效果
  2. 收藏 | 人工智能与模式识别会议集锦
  3. AI算法工程师手册!
  4. 土是独体字结构吗_毛笔楷书基础练习独体字部首的写法(左部)4
  5. 我们一起爬爬爬之HTTP原理
  6. k-means算法原理及实战
  7. 栈的输出_算法:栈和队列题目集合(一)
  8. centos 配置mysql环境变量_Centos7.1部署mysql-5.6.34(笔记)
  9. mysql 从 a表updateb表_mysql A表自动更新和插入B表的数据
  10. Java 面试之语言基础