'Struts2 keep same action url if validation fail using struts2-bean-validation-plugin
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;
}
}
Solution 1:[1]
Actually I need some configuration changes in struts.xml file you can see I have create two package as I don't need validation in every action i.e showing registration form doesn't need any validation where submit registration form needed. so I put u userRegister action in struts-bean-validation package. and whenever userRegister(submit) action return input result because of validation fail we will redirect userRegisterForm to show registration form but at this point struts will create new request object so we will loose error result so for that we use struts default interceptor store with operation mode STORE which will store error result in session and in userRegisterForm action again we use store interceptor but with operation mode RETRIEVE so that struts will fetch error result from session.
Note here you can see I use beanValidationDefaultStack interceptor which is mention in struts2-bean-validation-plugin.jar struts-plugin.xml file. because what I assume if we use interceptor then struts will now use default interceptor.
<?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">
<action name="userRegisterForm" method="userRegisterForm" class="com.pc.collabtest.actions.UserRegisterAction">
<interceptor-ref name="store"><param name="operationMode">RETRIEVE</param></interceptor-ref>
<result name="success">/userRegisterForm.jsp</result>
</action>
</package>
<package name="my-bean-validation" extends="struts-bean-validation">
<action name="userRegister" method="userRegister" class="com.pc.collabtest.actions.UserRegisterAction">
<interceptor-ref name="store"><param name="operationMode">STORE</param></interceptor-ref>
<interceptor-ref name="beanValidationDefaultStack"></interceptor-ref>
<result name="userList" type="redirectAction">userList</result>
<result name="userRegisterForm" type="redirectAction">userRegisterForm</result>
<result name="input" type="redirectAction">userRegisterForm</result>
</action>
</package>
</struts> ```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pratik |