在资源配置文件中配置了如下:
user.required={0} is required
使用struts2的验证框架配置文件
<field name="name">
<!-- 该表单域必须填写 -->
<field-validator type="requiredstring">
<!-- 如果校验失败,显示user.required对应的信息 -->
<message key="user.required" >
</message>
</field-validator>
</field>
请问一下如何在 <message key="user.required" > </message>中为user.required={0} is required传递参数?
试着添加 <message key="user.required" arg0="123"> </message>又提示Attribute "arg0" must be declared for element type "message".
请帮忙解答,非常感谢
------解决方案--------------------
是不是没有添加在错误集合里面 例子如下:
1:
写在actionfrom 里面的前端验证
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();
if(name==null ||"".equals(name))
{
ActionMessage message=new ActionMessage("intput.count.null","name");
errors.add("name",message);
}
if(name.length() <=6 ||name.length()>=10)
{
ActionMessage message=new ActionMessage("input.count.length","6","10");
errors.add("name",message);
}
if(null==password||"".equals(password))
{
ActionMessage message=new ActionMessage("input.password","password");
errors.add("password",message);
}
return errors;
}
2: 在action 里面的逻辑验证
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String name=loginForm.getName();
if("luoluoluo".equals(name))
{
ActionMessages messages=new ActionMessages();
ActionMessage actionMessage=new ActionMessage("input.jingzhi",name);
messages.add("qingzhi", actionMessage);
this.saveErrors(request, messages); //保存在errors集合里面
return mapping.getInputForward();
}
return null;
}
3: 在jsp页面显示
<html:form action="login">
<!--arg0 表示输入参数 bundle 表示指定的资源文件 -->
<bean:message key="into.input" arg0="name"/>
<html:text property="name" />
<html:errors property="name" bundle="warn"/> <br/>
<bean:message key="into.input" arg0="pass" />
<html:password property="password"/>
<html:errors property="password" bundle="warn"/>
<html:errors property="qingzhi" bundle="warn"/> <br/>
<html:submit/>
<html:cancel/>
</html:form>
------解决方案--------------------