use*_*944 1 java struts struts2 web
我是Struts 2的新手,之前曾在Struts 1工作。
我们如何将错误消息与UI组件(例如,文本框)绑定在一起?我不希望该错误消息成为全局消息。
为了在Struts 1中实现相同效果:
在表单验证方法中,我使用了以下方法:
ActionErrors errors = new ActionErrors();
if(userName != null && userName.length() <= 0)
errors.add("userName",new ActionError("error.userName.required"));
Run Code Online (Sandbox Code Playgroud)
并在用户界面中显示消息:
<html:messages id="userName" property="userName">
<bean:write name="userName"/>
</html:messages>
Run Code Online (Sandbox Code Playgroud)
在Struts 2中,如果我扩展Action类ActionSupport
并使用它:
addActionError(getText("Please enter UserId"));
Run Code Online (Sandbox Code Playgroud)
然后,这似乎是一条全局消息,可以使用以下命令在UI中显示:
<s:actionerror />
Run Code Online (Sandbox Code Playgroud)
因此,不确定如何在Struts 2中实现相同的功能。请让我知道这一点。
该<s:fieldError>
标记将是最接近的等效标记:
<s:fielderror fieldName="userName" />
Run Code Online (Sandbox Code Playgroud)
在Java方面,您将用于ValidationAware.addFieldError
添加特定于字段的消息:
public class AnyAction extends ActionSupport {
public void validate() {
addFieldError("userName", "User ID is required");
}
}
Run Code Online (Sandbox Code Playgroud)
就是说,对于低级别的验证,我会尽可能使用默认的基于XML的机制,因为它为您完成了很多工作,并使I18N / properties的使用变得更加容易。
归档时间: |
|
查看次数: |
12115 次 |
最近记录: |