I am running a basic Struts2 application in which I got one confusion. My action class implements Preparable and ModelDriven interfaces and extends ActionSupport class.The model bean has a single property named "User".
My home page(jsp) has one input field corresponding to the only property "User" of a model bean.
In prepare() method I am initializing the bean and setting its property to some default value say "Test" and the model() method is returning this bean object.
In validate(), I have a validation that if "User" property of bean has value equals to "Test" then addFieldError else proceed.
public Student getModel() {
System.out.println("inside getModel.."+ st.getName());
return st;
}
public void validate(){
System.out.println("inside validate"+st.getName());
if(st.getName().equals("Test")){
addFieldError("name","blank field");
}
}
public void prepare() throws Exception {
st = new Student();
st.setName("Test");
}
Now, my question is When I am accessing the action directly then the the error comes and in console I got below logs:
inside getModel..Test
inside getModel..Test
inside validate...Test
but If I input any value say "Stack" in the form field and submit the form then validate method prints the value that user has input while model method is printing what prepare has initialized.
inside getModel..Test
inside getModel..Test
inside validate...Stack
Why so? Why both methods are not in sync? Do the validate method and model method are picking the property value from different locations?
Thanks.