首页 > 编程语言 > 详细

tomcat class nofound,spring mvc的get请求乱码处理

时间:2018-05-02 16:54:42      阅读:174      评论:0      收藏:0      [点我收藏+]
一、maven中classnotfound
1.其实是jar包问题,maven默认会把jar包编译到target下面,而tomcat需要在WEB-INF目录下使用它,所以发现没有的问题
需要在pom.xml中配置
<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.6</source>

<target>1.6</target>

<encoding>UTF-8</encoding>

<compilerArguments>

  <extdirs>src\main\webapp\WEB-INF\lib</extdirs>

</compilerArguments>

</configuration>

</plugin>

二、解决springmvc的get乱码问题
1.post处理乱码是这样的
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>

    2.get乱码处理
    public class EncodingFilter extends OncePerRequestFilter {  
private String encoding;  

public void setEncoding(String encoding) {  
    this.encoding = encoding;  
}  

@Override  
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)  
        throws ServletException, IOException {  
    // 设置请求响应字符编码  
    request.setCharacterEncoding(encoding);  
    response.setCharacterEncoding(encoding);  

    // 传递给目标servlet或jsp的实际上是动态代理的对象,而不是原始的HttpServletRequest对象  
    request = (HttpServletRequest) Proxy.newProxyInstance(request.getClass().getClassLoader(), request.getClass().getInterfaces(), new MyInvacationHandler(request));  
    chain.doFilter(request, response);  
}  

class MyInvacationHandler implements InvocationHandler{  
    private HttpServletRequest request;  
    MyInvacationHandler(HttpServletRequest request){  
        this.request=request;  
    }  

    @Override  
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
        switch (method.getName()) {  
        case "getParameter":  
            String value = request.getParameter((String)args[0]);  
            try {  
                if(value != null){  
                    value=new String(value.getBytes("ISO-8859-1"),encoding);  
                }  
            } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
            }  
            return value;  
        case "getParameterValues":  
            String[] values = request.getParameterValues((String)args[0]);  
            if (values != null) {  
                for (int i = 0; i < values.length; i++) {  
                    try {  
                        values[i] = new String(values[i].getBytes("ISO-8859-1"),encoding);  
                    } catch (UnsupportedEncodingException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
            return values;  
        default:  
            return method.invoke(request, args);  
        }  
    }  

}  

}

web.xml配置
<filter>
<filter-name>encodingFilter12</filter-name>
<filter-class>com.wjw.controller.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter12</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

tomcat class nofound,spring mvc的get请求乱码处理

原文:http://blog.51cto.com/10913595/2110718

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!