I am using Struts 2 and I have two action one will show the register form and second will be register user.
I want to achieve is while submitting form if any validation fail then user will be redirect to previous page with errors details.
I have use struts2-bean-validation-plugin for validating user bean.
My configuration files are as below
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="beanValidation" class="org.apache.struts.beanvalidation.validation.interceptor.BeanValidationInterceptor" />
<interceptor-stack name="appDefaultStack">
<interceptor-ref name="beanValidation"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<action name="userRegisterForm" method="userRegisterForm" class="com.pc.collabtest.actions.UserRegisterAction">
<result name="success">/userRegisterForm.jsp</result>
</action>
<action name="userRegister" method="userRegister" class="com.pc.collabtest.actions.UserRegisterAction" >
<interceptor-ref name="appDefaultStack"/>
<result name="userList" type="redirectAction">userList</result>
<result name="userRegisterForm" type="redirectAction">userRegisterForm</result>
<result name="input" type="redirectAction">userRegisterForm</result>
</action>
</package>
</struts>
And Action class is
package com.pc.collabtest.actions;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
import com.pc.collabtest.model.User;
import com.pc.collabtest.service.UserService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class UserRegisterAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Valid
public User user;
@Autowired
private UserService userService;
public String userRegisterForm() throws Exception {
return "success";
}
public String userRegister() throws Exception {
if (user != null) {
if(userService.saveUser(user) != null) {
return "userList";
}
}
return "userRegisterForm";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}