读书人

国际化校验struts的资源包验证信息

发布时间: 2013-07-08 14:13:00 作者: rapoo

国际化校验——struts的资源包验证信息不能取出问题
今天遇到一个问题,struts中的资源包验证信息取不出来,不知道怎么回事,还请大家帮帮忙

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<body>
<s:text name="infoText"></s:text>
<s:property value="% { getText('infoText')}"/>
<br />
<s:text name="infoTextParam">
<s:param>我是参数1</s:param>
<s:param>我是参数2</s:param>
</s:text>
<br />
<s:form action="register" method="post">
账号:<s:textfield name="username"></s:textfield>${errors.username[0]}
<br/>
密码:<s:textfield name="password"></s:textfield>${errors.password[0]}
<br/>
年龄:<s:textfield name="age"></s:textfield>${errors.age[0]}
<br/>
身高:<s:textfield name="height"></s:textfield>${errors.height[0]}
<br/>
测试:${errors.infoTextParamValueFromAction[0]}
<br/>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>


Register.java代码

package controller;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;



public class Register extends ActionSupport{

private String username;

private String password;

private Integer age;

private Double height;


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 Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Double getHeight() {
return height;
}

public void setHeight(Double height) {
this.height = height;
}


@Override
public void validate(){



List paramText = new ArrayList();

if("".equals(username)){
this.addFieldError("username", this.getText("username_null"));
}

if("".equals(password)){
this.addFieldError("password", this.getText("password_null"));
}

if(age==null || age>=100 || age<0){
this.addFieldError("age", this.getText("age_wrong"));
}

if(height==null || height<0 || height>3){
this.addFieldError("height", this.getText("height_wrong"));
}
System.out.println("username_null:"+this.getText("username_null"));
paramText.add("第一个参数");
paramText.add("第二个参数");

this.addFieldError("infoTextParamValueFromAction", this.getText(
"infoTextParamValueFromAction",paramText));


}


public String execute(){

System.out.println("执行execute()方法");

return "register";
}
}








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>

<package name="ghyStruts1" extends="struts-default">
<action name="register" class="controller.Register">
<result name="register">showregister.jsp</result>
<result name="input">register.jsp</result>
</action>

</package>

<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.custom.i18n.resources" value="info"></constant>
</struts>


info_en_US.properties

username_null=username is null ,please input!
password_null=password is null,please input!
age_wrong=age wrong,must 0-100
height_wrong=height wrong,must 0-3
infoText=info_en_US.properties Text!
infoTextParam=info_en_US.properties Text has Paramete,param 0 value:{0},param 1 value: {1}
infoTextParamValueFromAction=infoTextParamValueFromAction param 0 value :{0},param 1 value\:{1}


错误信息:


2013-7-1 3:31:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: The first TextProvider in the ValueStack (controller.Register) could not locate the message resource with key 'infoText'


2013-7-1 3:31:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: The default value expression 'infoText' was evaluated and did not match a property. The literal value 'infoText' will be used.
2013-7-1 3:31:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: The first TextProvider in the ValueStack (controller.Register) could not locate the message resource with key 'infoTextParam'
2013-7-1 3:31:23 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: The default value expression 'infoTextParam' was evaluated and did not match a property. The literal value 'infoTextParam' will be used.

执行结果:
国际化校验——struts的资源包验证信息不能取出有关问题

username、password、age、height都显示的是资源包中的key,而没有显示value
Struts Java 国际化校验 javaweb
[解决办法]
貌似应该写成<constant name="struts.custom.i18n.resources" value="properties.info"></constant>
乱码文章推荐:http://blog.sina.com.cn/s/blog_6608320c0100j3v8.html

读书人网 >Java Web开发

热点推荐