Hi
The first few lines within the controller is used to check if request has some values if yes then bind and validate bean. everything works fine on the jsp page navigation and input box validation
I have used two submit buttons and made it look like links using css both calls same method which binds the request to bean and validates the values
WebUtils.hasSubmitParameter is used to differentiate which button was clicked.
Here's the code
jsp page
Code:
<form:form action="myexample.htm?action=validateValues"
method="POST">
<input type="text" name="text1" size="20" />
<form:errors path="text1" />
<input type="submit" name="add" style="cursor: pointer" value="Add">
<input type="text" name="text2" size="20" />
<form:errors path="text2" />
<input type="submit" name="Edit" style="cursor: pointer" value="Edit">
Controller
Code:
public class MyExampleController extends MultiActionController{
BindingResult errors;
public BindingResult getErrors() {
return errors;
}
public void setErrors(BindingResult errors) {
this.errors = errors;
}
protected void bind(HttpServletRequest request, Object command) throws Exception {
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
errors = binder.getBindingResult();
binder.closeNoCatch();//considers all errors fatal
}
public void validate(Object command) {
Validator[] validators = getValidators();
if (validators != null){
for(int index = 0; index < validators.length; index++) {
Validator validator = validators[index];
if(validator instanceof MyExampleValidator) {
if(((MyExampleValidator) validator).supports(command .getClass())) {
ValidationUtils.invokeValidator(validators[index], command,errors);
}
}else if (validator.supports(command.getClass())) {
ValidationUtils.invokeValidator(validators[index], command, errors);
}
}
}
}
public ModelAndView validateValues(HttpServletRequest request, HttpServletResponse response) throws Exception{
ModelAndView mav=null;
MyExampleForm myExampleForm = (MyExampleForm) newCommandObject(MyExampleForm.class);
bind(request, myExampleForm);
//client side validation
validate(myExampleForm);
if (errors.hasErrors()) {
mav=getPageData(request,false); --> gets look ups
mav.addAllObjects(errors.getModel());--> this is what loads <forms:errors path="text1">
return mav;
}
}
//server side validation
....................
//all above is fine then
//check which button was clicked and redirect to different url WebUtils.hasSubmitParameter(request,"add");
}
config file
Code:
<bean id="myExampleValidator" class="com.validator.MyExampleValidator" />
<bean id="myExampleController" class="com.controller.MyExampleController" >
<property name="methodNameResolver">
<ref bean="paramResolver" />
</property>
<property name="validators" ref="myExampleValidator"/>
</bean>
<bean id="paramResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="defaultMethodName" value="loadPage"></property>
<property name="paramName">
<value>action</value>
</property>
</bean>
everything works fine now w/o javascript
i will be trying to move paging to annotations based whenever time permits
Further questions are
1. When trying to use
<form:form action="myexample.htm?action=validateValues"
method="POST" CommandName="command"> --> also tried myExampleForm/MyExampleForm
<form:input path="text1"> -> binding error occurs
but <form:errors path="text1"> works fine with/without CommandName="command". Don't want to use old spring:bind
2. With my current example jsp page shows errors but user input values are disappered, how do i retain it?