views:

24

answers:

1

where and how to implement the validate(){} method for validating the data on the form, in struts 2, please help me, Thanks in advance.

A: 

I got it, the validate method is delcared in the ActionSupport class and we should override it in our Action class (First we should extend the ActionSupport Class) as follows,

public class Login extends ActionSupport {
//execute method goes here 
//getter/setters goes here
@Override
public void validate() {
super.validate();
    System.out.println("User Name " + getUserName());
    if(getUserName().length()==0)
         addFieldError("userName", "User Name Required");

    }

}

and also you should define your action in strus.xml as follows

<action name="DemoLogin" class="com.demo.Login">
    <result name="SUCCESS">/LoginSuccess.jsp</result>
    <result name="ERROR">/LoginError.jsp</result>
    <result name="input">/Login.jsp</result>
</action>

here <result name="input">/Login.jsp</result> this tag is imp bcoz, if dont add this tag the filter dispathcer, wont come to know which page to render if validation-error occur.

Jitendra