有三个JSP,其中一个JSP中文显示正常,其它两个显示的是乱码,搞到我晕呼呼
望高人帮忙解决,我用的是struts2.2.1+tomcat-6.0.26+jdk1.6.0_20,见下图:index.jsp
login.jsp
welcome.jsp
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>login_struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<!--
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml<struts>
<package name="login_struts2" extends="struts-default">
<action name="Login" class="y2ssh.login.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
struts.propertiesstruts.locale=zh_CN
struts.i18n.encoding=UTF-8
LoginAction.javapackage y2ssh.login;public class LoginAction {
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String execute() throws Exception{
if(username.equals("scott") && password.equals("tiger")) {
return "success";
} else {
return "error";
}
}
}
index.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
</head>
<body>
欢迎
</body>
</html>
login.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="Login.action" method="post">
<table align="center" border="1">
<caption><h3>用户登录</h3></caption>
<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 align="center">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Struts2</title>
</head>
<body>
您已经登录
</body>
</html>
望高人帮忙解决,我用的是struts2.2.1+tomcat-6.0.26+jdk1.6.0_20,见下图:index.jsp
login.jsp
welcome.jsp
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>login_struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<!--
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml<struts>
<package name="login_struts2" extends="struts-default">
<action name="Login" class="y2ssh.login.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
struts.propertiesstruts.locale=zh_CN
struts.i18n.encoding=UTF-8
LoginAction.javapackage y2ssh.login;public class LoginAction {
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String execute() throws Exception{
if(username.equals("scott") && password.equals("tiger")) {
return "success";
} else {
return "error";
}
}
}
index.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎</title>
</head>
<body>
欢迎
</body>
</html>
login.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="Login.action" method="post">
<table align="center" border="1">
<caption><h3>用户登录</h3></caption>
<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 align="center">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Struts2</title>
</head>
<body>
您已经登录
</body>
</html>
图看不到,index.jsp和welcome.jsp乱码,login.jsp正常
简单的说就是,三个JSP页面,login.jsp中文显示正常,index.jsp和welcome.jsp显示乱码
三个JSP的charset和pageEncoding都设置成UTF-8
struts.xml加了org.apache.struts2.dispatcher.ActionContextCleanUp filter
struts.properties也设置了struts.i18n.encoding=UTF-8
<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>
</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>
</filter>
问题已经解决了,原因是我用记事本写的,保存时默认是用ANSI编码,我用UTF-8编码另存后显示正常了
谢谢你的关注,我什么都没改,用记事本UTF-8编码另存后问题已经解决了