国际化校验——struts的资源包验证信息不能取出问题

本文描述了在使用Struts进行国际化校验时遇到资源包验证信息无法取出的问题,通过分析代码和错误日志,指出问题可能出在获取message资源上,并展示了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天遇到一个问题,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.

执行结果:


username、password、age、height都显示的是资源包中的key,而没有显示value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值