ここに私の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>
プロファイル Bean
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;
}
}