我参照了MAX的struts2教程,但是按照MAX大师的做法,碰到了不少问题,通过google问题以及自己修改页面实验,最终克服了“下拉列表中选择什么,语言都不会改变”、乱码等问题。
练习工具:Myeclipse7
各文件分布图:
HelloWorld.jsp
---------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=GB18030">
<title>Hello World</title>
</head>
<body>
<s:include value="/LangSelector.jsp"/>
<h2><s:text name="HelloWorld"/></h2>
<h2><s:property value="%{getText('HelloWorld')}"/></h2>
</body>
</html>
LangSelector.jsp
--------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function langSelector_onChanged() {
document.langForm.submit();
}
</script>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<s:bean id="locales" name="tutorial.Locales"/>
<form action="<s:url includeParams="get" encode="true"/>" name="langForm"
style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;">
Language: <s:select label="Language"
list="#locales.locales" listKey="value" listValue="key"
value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
name="request_locale" id="langSelector"
οnchange="langSelector_onChanged()" theme="simple"/>
</form>
</body>
</html>
Locales.java
----------------
package tutorial;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
public class Locales {
public Map<String,Locale> getLocales() {
Map<String,Locale> locales = new Hashtable<String,Locale>(2);
locales.put("American English", Locale.US);
locales.put("Simplified Chinese", Locale.CHINA);
return locales;
}
}
web.xml
---------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
<package name="tutorial" namespace="/" extends="struts-default">
<action name="*">
<result>/{1}.jsp</result>
</action>
</package>
</struts>
globalMessages_en_US.properties
----------------------------------------------
HelloWorld=Hello World iasdfa sdf/!
globalMessages_zh_CN.properties
--------------------------------------------
HelloWorld=/u4F60/u597D/!
运行结果如下:
一定要通过Action来访问页面, 如果直接使用JSP访问页面,Struts 2.0在web.xml的配置的过滤器(Filter)就不会工作,所以拦截器链也不会工作。