这是我的 Register-validation.xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="requiredstring">
<param name="fieldname">userBean.username</param>
<message>User name is required.</message>
</validator>
<validator type="required">
<param name="fieldname">profileBean.Age</param>
<message>Age is required.</message>
</validator>
</validators>
这是我的表格。
<s:form action="register">
<label>UserName</label>
<s:textfield cssClass="formfield" name="userBean.username"
label="User Name" />
<br />
<label>Password</label>
<s:password cssClass="formfield" name="userBean.password"
label="Password" />
<br />
<label>First Name</label>
<s:textfield cssClass="formfield" name="profileBean.firstName"
label="First Name" />
<br />
<label>Last Name</label>
<s:textfield cssClass="formfield" name="profileBean.lastName"
label="Last Name" />
<br />
<label>Age</label>
<s:textfield cssClass="formfield" name="profileBean.age" label="Age" />
<s:submit />
</s:form>
即使我没有输入用户名和年龄的值,表单仍然会继续显示在成功页面中。为什么它不工作?
编辑
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>
<constant name="struts.devMode" value="false" />
<constant name="struts.ui.theme" value="simple" />
<package name="basicstruts2" namespace="/Welcome" extends="struts-default">
<action name="index">
<result>WEB-INF/index.jsp</result>
</action>
<action name="HelloWorld" class="actions.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
<package name="Registration" namespace="/" extends="struts-default">
<action name="register" class="actions.Register"
method="execute">
<result name="success">/thankyou.jsp</result>
</action>
</package>
</struts>
配置文件豆
package model;
public class Profile {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
和用户 Bean
package model;
public class User {
private String username;
private String password;
private int accountType;//Admin - 0; User - 1;
private Profile profile;
public User(){
accountType = 1;
}
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 int getAccountType() {
return accountType;
}
public void setAccountType(int accountType) {
this.accountType = accountType;
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
注册动作
public class Register extends ActionSupport implements ServletRequestAware{
public String execute(){
userBean.setProfile(getProfileBean());
return "success";
}
private User userBean;
private Profile profileBean;
HttpServletRequest request;
public User getUserBean() {
return userBean;
}
public void setUserBean(User userBean) {
this.userBean = userBean;
}
public Profile getProfileBean() {
return profileBean;
}
public void setProfileBean(Profile profileBean) {
this.profileBean = profileBean;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletRequest getServletRequest() {
return this.request;
}
}