Hi,

I am creating my first struts2 application with xml based validation.
However, I am not able to get the validation part working i.e. its not
returning the field error message to UI. Please help.

My code looks like this.


*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.

LoginAction, LoginAction-Validation.xml and LoginAction.properties all are
in same package. Also I am building my application via ant.

Any help will be appreciated.

Thanks,
Vivek

Reply via email to