struts 2的国际化错误
我测试struts 2的国际化时错误,错误如下:严重: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', id 'langSelecter', name 'request_locale': The requested list key '#localeList.locales' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:231)
at org.apache.struts2.components.Component.findValue(Component.java:293)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:79)
at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:99)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:780)
at org.apache.struts2.components.UIBean.end(UIBean.java:481)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:43)
at org.apache.jsp.choose_jsp._jspx_meth_s_005fselect_005f0(choose_jsp.java:559)
at org.apache.jsp.choose_jsp._jspService(choose_jsp.java:174)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
代码如下:ChooseAction.java
package com;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ChooseAction extends ActionSupport{
private Locale current;
public void setCurrent(Locale cur){
this.current = cur;
}
public Map<String, Locale> getLocales(){
Map<String, Locale> locales = new Hashtable<String, Locale>(2);
ResourceBundle bundle = ResourceBundle.getBundle("globalMessages",current);
locales.put(bundle.getString("huizhi.en"), Locale.US);
locales.put(bundle.getString("huizhi.cn"), Locale.CHINA);
return locales;
}
public String execute(){
return SUCCESS;
}
}
choose.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<s:text name="huizhi.title"/>
</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function changeLocale(){
document.langForm.submit();
}
</script>
</head>
<body>
<!--topMain start -->
<div id="topMain">
<!--top start -->
<div id="top"><a href="index.html">
<img src="images/logo.gif" alt="charged" border="0" class="logo"/>
</a>
<s:form name="serch" action="#" method="post" theme="易做图">
<s:textfield name="serch" cssClass="txtBox1"/>
<s:submit name="go" key="huizhi.go"/>
</s:form>
</div>
<!--top end -->
</div>
<!-- topMain end -->
<!--navMain start -->
<div id="navMain">
<!--nav start -->
<div id="nav">
<ul>
<li><a href="#" class="hover"><s:text name="huizhi.home"/></a></li>
<li><a href="#"><s:text name="huizhi.about"/></a></li>
<li><a href="#"><s:text name="huizhi.forum"/></a></li>
<li><a href="#"><s:text name="huizhi.class"/></a></li>
<li><a href="#"><s:text name="huizhi.blog"/></a></li>
<li><a href="#"><s:text name="huizhi.support"/></a></li>
<li>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<s:bean id="localeList" name="com.ChooseAction">
<s:param name="current" value="#SESSION_LOCALE==null ? locale : #SESSION_LOCALE"/>
</s:bean>
<form action='<s:url/>' name="langForm">
<s:select cssStyle="width:130px"
list="#localeList.locales" listKey="value" listValue="key"
value="#SESSION_LOCALE==null ? locale : #SESSION_LOCALE"
name="request_locale" id="langSelecter"
onchange="changeLocale()" theme="易做图"/>
</form>
</li>
</ul>
</div>
<!--nav end -->
</div>
<!--navMain end -->
<!--blackMain start -->
<s:text name="huizhi.welcome"></s:text>
</body>
</html><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
<s:text name="huizhi.title"/>
</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function changeLocale(){
document.langForm.submit();
}
</script>
</head>
<body>
<!--topMain start -->
<div id="topMain">
<!--top start -->
<div id="top"><a href="index.html">
<img src="images/logo.gif" alt="charged" border="0" class="logo"/>
</a>
<s:form name="serch" action="#" method="post" theme="易做图">
<s:textfield name="serch" cssClass="txtBox1"/>
<s:submit name="go" key="huizhi.go"/>
</s:form>
</div>
<!--top end -->
</div>
<!-- topMain end -->
<!--navMain start -->
<div id="navMain">
<!--nav start -->
<div id="nav">
<ul>
<li><a href="#" class="hover"><s:text name="huizhi.home"/></a></li>
<li><a href="#"><s:text name="huizhi.about"/></a></li>
<li><a href="#"><s:text name="huizhi.forum"/></a></li>
<li><a href="#"><s:text name="huizhi.class"/></a></li>
<li><a href="#"><s:text name="huizhi.blog"/></a></li>
<li><a href="#"><s:text name="huizhi.support"/></a></li>
<li>
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']"/>
<s:bean id="localeList" name="com.ChooseAction">
<s:param name="current" value="#SESSION_LOCALE==null ? locale : #SESSION_LOCALE"/>
</s:bean>
<form action='<s:url/>' name="langForm">
<s:select cssStyle="width:130px"
list="#localeList.locales" listKey="value" listValue="key"
value="#SESSION_LOCALE==null ? locale : #SESSION_LOCALE"
name="request_locale" id="langSelecter"
onchange="changeLocale()" theme="易做图"/>
</form>
</li>
</ul>
</div>
<!--nav end -->
</div>
<!--navMain end -->
<!--blackMain start -->
<s:text name="huizhi.welcome"></s:text>
</body>
</html>
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>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="utf-8"/>
<package name="Struts2_Login" extends="struts-default">
<!-- <action name="validate" class="tmp.action.ValidateAction" method="login">
<result name="input">/validate.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="requiredstring" class="tmp.action.Requiredstring">
<result name="input">/requiredstring.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="required" class="tmp.action.Required">
<result name="input">/required.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="int" class="tmp.action.IntAction">
<result name="input">/int.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="date" class="tmp.action.DateAction">
<result name="input">/date.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="user" class="tmp.action.UserAction">
<result name="input">/user.jsp</result>
<result name="success">/success.jsp</result>
</action> -->
<action name="choose" class="com.ChooseAction">
<result name="success">/choose.jsp</result>
</action>
<action name="checkLogin" class="com.LoginAction" method="checkLogin">
<!-- <result name="input"></result> -->
<result name="success">/index.jsp</result>
</action>
</package>
</struts> --------------------编程问答-------------------- 你那个localeList.locales里的locales是一个局部变量,你直接这样localeList.locales当然是错误的,。因为根本访问不到locales这个属性,所以报了如上所说的异常。异常的意思就是说你的localeList.locales这个不能解析为一个集合,因为<s:select>的list要传一个集合,也就是说属性找不到或者为空,所以你把你的action类中的locales属性设置为全局变量,要么直接(localeList.方法),取方法的返回值, --------------------编程问答-------------------- 我是按照教程写的!具体应该怎么改啊! --------------------编程问答--------------------
你照着教程写最起码你得知道是什么意思阿、我说的已经够清晰的了。 --------------------编程问答-------------------- 把locales设置成action的属性必须是list类型
List<Map<String, String>> locales; get().. set()..
赋值
locales = new ArrayList<Map<String, String>>();
Map<String,String> m1 = new HashMap<String,String>();
m1.put("key",bundle.getString("huizhi.en"));
m1.put("value",Locale.US);
Map<String,String> m2 = new HashMap<String,String>();
m2.put("key",bundle.getString("huizhi.cn"));
m2.put("value",Locale.CHINA);
locals.add(m1);
locals.add(m2); [/code]
把页面上的select标签改成
<s:select cssStyle="width:130px"
list="locales" listKey="key" listValue="value"
补充:Java , Java EE