大家好,又见面了,我是你们的朋友全栈君。
最近开始自学springMVC框架,遇到中文乱码这一经典问题,记录下解决过程,以便后续忘记
web.xml 里过滤器配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <!-- 解决中文编码问题 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <servlet> <servlet-name>example</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- init-param 节点必须在 load-on-startup 节点以上 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
spring配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.guo.controller"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" > <!-- 如果使用 jstl 的话,配置下面的属性 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> --> </bean> </beans>
jsp代码如下:
<%@ 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> 欢迎您,${requestScope.username } </body> </html>
java的controller代码如下:
/* * 文 件 名: UserController.java * 版 权: Ltd. Copyright YYYY-YYYY, All rights reserved * 描 述: <描述> * 修 改 人: Administrator * 修改时间: 2016年3月17日 */ package com.guo.controller; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.guo.po.User; /** * <一句话功能简述> * <功能详细描述> * * @author Administrator 2016年3月17日 */ @Controller @RequestMapping("/user") public class UserController { // Controller 单例 // http://localhost:8080/springmvc/user/add.do @RequestMapping("/add") public void add(User user) { System.out.println(user); } @RequestMapping("/login") public String login(@RequestParam("n")String userName, @RequestParam("p")String password, ModelMap map) { //userName = new String(userName.getBytes("ISO-8859-1"), "UTF-8"); System.out.println("userName is " + userName + " and password is " + password); // session.setAttribute // request.setAttribute map.put("username", userName); // return "forward:/welcome.jsp"; return "admin/adminwelcome"; } @RequestMapping("/login2") public ModelAndView login2(@RequestParam("n")String userName, @RequestParam("p")String password) { System.out.println("userName is " + userName + " and password is " + password); ModelAndView mv = new ModelAndView(); mv.setViewName("admin/adminwelcome"); mv.getModelMap().put("username", userName); return mv; } @RequestMapping("/baidu") public String baidu() { return "redirect:http://www.baidu.com"; } @RequestMapping("{id}/delete") public void delete(@PathVariable("id")Integer id) { System.out.println("delete id is " + id); } }
启动tomcat,浏览器访问 http://localhost:8080/springmvc/user/login.do?n=小郭&p=123
发现页面展示仍然是乱码,debug了下CharacterEncodingFilter里的代码,发现还是能拦截到请求的,反复检查 web.xml里的配置,没有任何问题,无奈在
login方法里加入userName = new String(userName.getBytes(“ISO-8859-1”), “UTF-8”);就能正常显示了
但还是不能解答我的疑惑,按理说是应该能直接展示中文的
后来经朋友提示,是因为tomcat配有配置编码格式,server.xml里如果不配置编码格式,则默认是iso-8859-1,于是加上即可
<Connector connectionTimeout=”20000″ port=”8080″ protocol=”HTTP/1.1″ redirectPort=”8443″ URIEncoding=”UTF-8″/>
仅作记录,后续复习,如有说错,请指出。
转载于:https://www.cnblogs.com/countguo/p/5303805.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159316.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...