今天小弟尝试搭建struts2环境,但是碰见了悲催的不算是乱码的乱码问题。
我只引入了struts2的相关包,struts2的配置文件struts.xml也设置了统一编码方式:
<constant name="struts.i18n.encoding" value="GBK" />
页面编码方式是GBK,java,jsp文件属性都是GBK。启动tomcat没报错,访问的时候页面也能显示中文,悲催的事情发生了,右键查看源文件出现这样的字符“中文”,ognl加上escape="false"属性后页面源文件显示正常,求高手解答这是为什么啊????难道是jar版本的问题?以前木有遇到过~~~
代码:
import com.opensymphony.xwork2.ActionSupport;
public class test extends ActionSupport {
private String fffd;
public String test(){
fffd = "中文";
return "success";
}
public String getFffd() {
return fffd;
}
public void setFffd(String fffd) {
this.fffd = fffd;
}
}
页面如下jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="fffd"/>
</body>
</html>
struts配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="GBK" />
<package name="protal" namespace="" extends="struts-default">
<action name="test" class="test">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
------最佳解决方案--------------------------------------------------------
你已经在struts.xml文件里面配置了还是乱码,你可以去tomcat conf目录下的service.xml文件有个 <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/> --有就改成UTF-8 没有的话就加一个!
还是不行的话就写一个过滤器! 一切OK!
------其他解决方案--------------------------------------------------------
建议楼主所有的编码格式都设置成:“utf-8”,我看楼主的代码里面有两种编码:GBK和utf-8两种方式,用两种编码格式不容易查找具体是哪里出现了问题。
------其他解决方案--------------------------------------------------------
进入Window/Preferences进行配置
General/Content Types
点开右边有个Text进行配置
首先把CSS,DTD,Html,Java Source File,JavaScript Source File都设为UTF-8
然后点开JSP
把JSP,CSS JSP,JSP Fragment,Jsp Tag Definitoion设为UTF-8