ssm controller层获取的数据是乱码

传递表单如下

 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/log.do">
    <table>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="userName"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td><input type="radio" name="sex" value="男" ></td>
            <td>男</td>
            <td><input type="radio" name="sex" value="女"></td>
            <td>女</td>
        </tr>
        <td><input type="submit" value="提交"></td>
    </table>
</form>
</body>

web.xml如下

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
    <!--spring 字符过滤器-->
    <filter>
        <filter-name>filterEncoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>filterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
     <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

controller层如下

 @Controller
public class LogController {
    @Resource
    private LoggerService loggerService;

    @RequestMapping(value = "/log",produces = "text/plain;charset=UTF-8")
    public String logJudge(@RequestParam String userName, @RequestParam String password, @RequestParam String sex,HttpServletRequest request,HttpServletResponse response)
    {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (userName!=null&&password!=null&&sex!=null)
        {
                Logger logger=new Logger();
                logger.setUsername(userName);
                logger.setPassword(password);
                logger.setSex(sex);
            System.out.println(userName);
            System.out.println(sex);
               loggerService.insertLogUser(logger);
                return "success";
        }
         return "404";
    }


}

在controller层中我尝试输出获取输出的数据,结果

图片说明
结果在控制台输出的是这个

 ???è??
???

求大神解答

2个回答

m0_37385244
木木甫 感谢感谢,乱码解决了。我想追问下我之前设置了编码为什么没用啊
大约 3 年之前 回复

你设的那个编码 只有form是post方式时才会有用,如果是get,设置request.set编码是没用的,只能用一种不正确的方式Sting的构造器来转换。
http://blog.csdn.net/zzhao114/article/details/54646945

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问