I am creating a simple login application as my first struts2 form validation application. However, I am not able to get the validation part working. I have tried what all solutions/examples I could find at google or at struts docs. Please help.....
Here is my code
login.jsp
<s:form action="LoginAction" method="post">
<s:textfield name="username" label="Login Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
<s:fielderror></s:fielderror>
</s:form>
struts.xml
<action name="LoginAction" class="com.helloworld.action.LoginAction" method="execute">
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
<result name="success">/HelloWorld.jsp</result>
</action>
LoginAction.java
public class LoginAction extends ActionSupport{
private String username;
private String password;
public String execute() throws Exception {
if(this.username.equals("admin") && this.password.equals("admin"))
return SUCCESS;
return ERROR;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername(){
return this.username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword(){
return this.password;
}
}
LoginAction-Validation.xml
<validators>
<field name="username">
<field-validator type="requiredstring">
<message key="user.required"/>
</field-validator>
</field>
</validators>
LoginAction.properties
user.required=UserName 开发者_如何学Cis required.
Please help...
Thanks
First thing that jumps out at me is the name of your validation file, which is LoginAction-Validation.xml. I know that the first part of that file name is case sensitive, but I don't know about the second part. Try changing it to LoginAction-validation.xml (lower case v).
Also, can you describe your build environment and project layout? Are you using Maven to build? Is your validation file in the same directory as the action class it refers to?
Lastly, are you using any custom interceptor stacks, or just the default interceptor stack?
Please ensure that you have registered your validator file as follows
- Add validators.xml to WEB-INF/classes folder
- Add an entry for your validator to this file
<validators>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
</validators>
- Ensure that your action class extends AbstractValidationActionSupport class
Try adding validate="true" into your form tag since the default theme is xhtml...
<s:form action="LoginAction" method="post" validate="true">
<s:textfield name="username" label="Login Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
<s:fielderror></s:fielderror>
</s:form>
精彩评论