lyg0 2020-06-04
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <!-- 提交路径 --> <form action="${pageContext.request.contextPath}/controller2/form" method="post" accept-charset="UTF-8"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="userPwd"></td> </tr> <tr> <td><input type="submit" value="登陆"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> form page is success </body> </html>
package com.springmvc.tranfercontroller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 基于注解带参数传输案例 * * @author Administrator * */ @Controller @RequestMapping(value = "/controller2") public class TranferController2 { // 成功提交跳转对应的方法 @RequestMapping(value = "/form") public String form(String userName, Integer userPwd) { System.out.println("用户名:" + userName + "用户密码:" + userPwd); System.out.println("println login of controller"); return "success"; } // 访问页面路径对应的方法 @RequestMapping(value = "/form2") public String form() { return "form"; } }
@Controller @RequestMapping("/controller2") public class TranferController2 { @RequestMapping("/form") public String form(HttpServletRequest request) { String userName=request.getParameter("userName"); String userPwd=request.getParameter("userPwd"); System.out.println("用户名:" + userName + "用户密码:" + userPwd); System.out.println("println login of controller"); return "success"; } }
1 | < meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> |
1 | < form method="post" action="#" accept-charset="UTF-8"> |
以上两种方式都试了一下,问题还是不能解决。
在web.xml文件中增加如下代码:
<filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>