开发者

How to manually validate data in struts 2

where and how to implement the validate开发者_如何学Python(){} method for validating the data on the form, in struts 2, please help me, Thanks in advance.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜