木木甫 2017-01-22 14:37 采纳率: 35.7%
浏览 4604
已采纳

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条回答 默认 最新

查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?