21处理器方法特殊参数与请求参数中文乱码问题解决

87 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第21天,点击查看活动详情

哈喽,大家好!我是Why,一名在读学生,目前刚刚开始进入自己的编程学习生涯。虽然学习起步较晚,但我坚信做了才有0或1的可能。学了一段时间以后也是选择在掘金上分享自己的日常笔记,也希望能够在众多道友的大家庭中打成一片。 本文主要讲解处理器方法特殊参数与请求参数中文乱码问题解决,如果大家读后觉得有用的话,还请大家多多支持博主:欢迎 ❤️点赞👍、收藏⭐、留言💬 ✨✨✨个人主页:JinHuan

8、获取集合类参数
获取普通类集合对象
 <h3>8、获取集合类参数</h3>
 <form action="/param/test08" method="post">
     球队1:<input type="text" name="teamName"><br/>
     球队2:<input type="text" name="teamName"><br/>
     球队3:<input type="text" name="teamName"><br/>
     球队4:<input type="text" name="teamName"><br/>
     <button type="submit">提交</button>
 </form>
 /**
  * @param teams:
  * @return org.springframework.web.servlet.ModelAndView
  * decription: 获取集合类型的参数,需要注解@RequestParam("teamName")来帮助
  * 第一种:获取简单类型的集合参数:可以通过@RequestParam
  * 第二种:获取对象类型的集合数:必须封装到一个类中作为一个【属性】操作【曲线救国】
  * */
 @RequestMapping("test08")
 public ModelAndView test08(@RequestParam("teamName") List<String> teams){
     System.out.println("test08");
     for (String team : teams) {
         System.out.println(team);
     }
     return new ModelAndView("ok");
 }

image-20220718011331740

获取对象类型集合

SpringMVC不支持直接从参数中获取对象集合类型,需要将对象集合封装到实体类中,作为类的属性来使用。见下:

 public class QueryVO {
     private List<Team> teamList;
 ​
     public List<Team> getTeamList() {
         return teamList;
     }
 ​
     public void setTeamList(List<Team> teamList) {
         this.teamList = teamList;
     }
 }
 //获取对象类集合
 @RequestMapping("test09")
 public ModelAndView test09(QueryVO vo){
     System.out.println("test08");
     for (Team team : vo.getTeamList()) {
         System.out.println(team);
     }
     return new ModelAndView("ok");
 }
 <form action="/param/test09" method="post">
     球队名称1:<input type="text" name="teamList[0].teamName"><br/>
     球队位置1:<input type="text" name="teamList[0].teamLocation"><br/>
     球队名称2:<input type="text" name="teamList[1].teamName"><br/>
     球队位置2:<input type="text" name="teamList[1].teamLocation"><br/>
     球队名称3:<input type="text" name="teamList[2].teamName"><br/>
     球队位置3:<input type="text" name="teamList[2].teamLocation"><br/>
     <button type="submit">提交</button>
 </form>

image-20220718011638288

请求参数中文乱码

对于前面所接受的请求参数,若含有中文,则会出现中文乱码的情况。Spring对于请求参数的中文乱码问题进行了处理,给出了专门的字符集过滤器CharacterEncodingFilter类,如下:

image-20220724095132040

乱码解决方法

在web.xml中注册字符集过滤器,推荐将该过滤器注册在其他过滤器之前,因为过滤器的执行顺序是按照注册顺序执行的。

在web.xml中配置文件直接注册字符集

 <!--配置中文乱码过滤器Filter-->
 <filter>
     <filter-name>characterEncodingFilter</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>
     <!--强制request使用字符集encoding-->
     <init-param>
         <param-name>forceRequestEncoding</param-name>
         <param-value>true</param-value>
     </init-param>
     <!--强制response使用字符集encoding-->
     <init-param>
         <param-name>forceResponseEncoding</param-name>
         <param-value>true</param-value>
     </init-param>
 </filter>
 <filter-mapping>
     <filter-name>characterEncodingFilter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
乱码解决原理
 public class CharacterEncodingFilter extends OncePerRequestFilter {
 ​
    @Nullable
    private String encoding; //设置的编码格式
 ​
     //是否强制设置请求编码格式为设置的编码格式
    private boolean forceRequestEncoding = false; 
 ​
     //是否强制设置返回的编码格式为设置的编码格式
    private boolean forceResponseEncoding = false;
 ​
 ​
    @Override
    protected void doFilterInternal(
          HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
          throws ServletException, IOException {
 ​
       String encoding = getEncoding();
       if (encoding != null) {
           //核心
          if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
             request.setCharacterEncoding(encoding);
          }
          if (isForceResponseEncoding()) {
             response.setCharacterEncoding(encoding);
          }
       }
       filterChain.doFilter(request, response);
    }
 ​
 }