ssm中使用jsp递交表单时,出现乱码解决方法

    xiaoxiao2022-07-02  131

    页面出现乱码,主要时编码格式不对导致的。

    1.检查jsp文件中的编码格式是否为"UTF-8"

    如果不是,改为"UTF-8",

    2.添加过滤器

    package com.yage.myoa.global; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class EncodingFilter implements Filter { private String encoding = "utf-8"; @Override public void init(FilterConfig filterConfig) throws ServletException { if(filterConfig.getInitParameter("encoding")!=null){ encoding = filterConfig.getInitParameter("encoding"); } } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); filterChain.doFilter(request,response); } @Override public void destroy() { } }

    在web.xml中添加配置

    <filter> <filter-name>encoding</filter-name> <filter-class>添加class所在的包</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>

    持续更新

    最新回复(0)