• SpringMVC_视图解析器与中文乱码问题


    return "/pages/front/success.jsp";

    return "/pages/front/fail.jsp";

    这两句的结构是: 前缀(prefix)+变化值+ 后缀(suffix)

    前缀是/pages/front/。后缀是.jsp

    在springmvc.xml配置文件里用视图解析器配置前缀后缀,使return里只有变化值: return "success";

    视图解析器只对转发有效,对重定向无效。

    springmvc.xml:

    按住ctrl shift h,搜索internalResourceViewResolver,然后右键copy qualified name。粘贴到class中。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
        
        <!-- 配置包扫描 -->        
        <context:component-scan base-package="cn.java.controller"></context:component-scan>
            
        <!-- 加入springmvc特有的注解驱动 -->    
        <mvc:annotation-driven></mvc:annotation-driven>    
        
        <!-- 视图解析器 -->
        <!-- prefix:前缀-->
        <!-- suffix:后缀-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/pages/front/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
    </beans>

    java:

        @RequestMapping(value="test1")
        public String test1(String username,String password) {
            
            if("admin".equals(username)&&"123".equals(password)) { //登录成功
                return "success";
            }
            else { //登录失败
                System.out.println(username);
                return "fail";
            }
            
        }  

    这里实际return的就是/pages/front/success.jsp

    解决在jsp页面输入中文传入后台就乱码问题:

    过去做法就是:创建一个包:cn.java.Filters,创建EncodingFilter.java。这是一个过滤器,先把乱码转成utf-8再放行。同时也要在web.xml配置过滤器。

    过去servlet处理乱码方法:

    EncodingFilter.java

    package cn.java.filters;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class EncodingFilter implements Filter {
    
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            chain.doFilter(request, response); //放行
            
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
            
        }
        
    }

    web.xml

      <!-- 配置过滤器,要放在servlet前面,因为是先拦截 -->
      <filter>
          <filter-name>encodingFilter</filter-name>
          <filter-class>cn.java.filters.EncodingFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>encodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

    而springmvc简化了的方法:

    不用再写EncodingFilter.java了,springmvc有写好的方法:CharacterEncodingFilter

     ctrl+shift+h搜索CharacterEncodingFilter,右键copy quality name,粘贴到filter-class中

    web.xml

      <filter>
          <filter-name>encodingFilter</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>encodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
  • 相关阅读:
    Linux系统负载
    full nat
    close wait 状态的随想
    记录一下 性能分析问题
    golang 反射
    socket里面那个又爱又恨的锁
    Difference between skbuff frags and frag_list
    浅析TCP协议---转载
    http 怎样关闭
    http 响应 ngx_http_send_header ngx_http_output_filter
  • 原文地址:https://www.cnblogs.com/lonske/p/9099767.html
Copyright © 2020-2023  润新知