木木甫 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条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python