传递表单如下
<%@ 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层中我尝试输出获取输出的数据,结果
???è??
???
求大神解答