用Struts Validate機制,因為一個form需要在多個頁面進行操作,不同頁面會有不同的驗證需求,所以我采用了ValidatorActionForm
代碼如下:
public class UserForm extends ValidatorActionForm {
private Integer id;
private String name;
private String password;
private String confirmpwd;
private String email;
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
...
}
Action類略過
validation.xml:
<formset>
<form name="/register/register">
<field property="name" depends="required">
<arg0 key="user.name"/>
</field>
<field property="confirmpwd" depends="required">
<arg0 key="user.confirmpwd"/>
</field>
</form>
...
ApplicationContext.properties:
user.name = {0}\u6d93\u5d88\u5158\u6d93\u8679\u2516
...
編譯后,ApplicationContext.properties能在WEB-INF\classes下找到,且struts-config.xml里也已添加
JSP頁面:
...
<html:form action="/register/register" method="post" enctype="multipart/form-data">
<input type="hidden" name="method" value="register" />
<div class="reg03">
<html:text property="name" styleId="name" style="width:245px" />
<font color="red"><html:errors property="name"/></font>
</div>
...
結果:驗證起作用,但是提示信息打印不出來。
HELP!!!
已解決
需要修改validation.xml和ApplicationContext.properties
validation.xml:
...
<field property="name" depends="required">
<msg name="required" key="user.required"/>
<arg0 key="user.name"/>
...
ApplicationContext.properties (未轉碼前)
...
user.name=姓名
user.required={0}不能為空
...
posted on 2008-06-15 00:58
EvanLiu 閱讀(1641)
評論(0) 編輯 收藏 所屬分類:
Struts