Vivek
Vivek

Reputation: 997

Struts2 Form Validation issue

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 is required.

Please help...

Thanks

Upvotes: 1

Views: 1990

Answers (3)

Nick
Nick

Reputation: 301

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>

Upvotes: 0

Dustin Wilhelmi
Dustin Wilhelmi

Reputation: 1819

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?

Upvotes: 1

sdc
sdc

Reputation: 89

Please ensure that you have registered your validator file as follows

  1. Add validators.xml to WEB-INF/classes folder
  2. Add an entry for your validator to this file

<validators>
   <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>

</validators>
  1. Ensure that your action class extends AbstractValidationActionSupport class

Upvotes: 0

Related Questions