Validating Struts 2 Using Message Interceptor

I have an action in which I try to log in.

public class RegisteredUserAction extends ActionSupport implements SessionAware { .. .. public String login() throws Exception { DBLogic dBLogic = new DBLogic(); RegisteredUser user = null; try { user = dBLogic.getRegisteredUser(getUserUsername(), getUserPassword()); } catch (CredentialException e) { addFieldError("userUsername", e.getMessage()); addActionError(e.getMessage()); return INPUT; } if (user != null) { session.put("user", user); return SUCCESS; } return ERROR; } } 

As you can see, if the username or password is invalid, I throw a CredentialException and populate this exception message in my Error field. I even tried to add an action error.

With some tips and searching on the Internet, I managed to use a message interceptor, using annotations in RegisteredUserAction.action and MainAction.action, respectively, storing and retrieving.

 import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "STORE"}), @InterceptorRef("defaultStack"),}) 

and

 import org.apache.struts2.convention.annotation.InterceptorRefs; @InterceptorRefs({ @InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"}), @InterceptorRef("defaultStack"),}) 

This is the contents of the struts.xml file, which matters:

 <package name="pages" extends="struts-default" namespace="/"> <global-results> <result name="displayPage">/WEB-INF/template/page.jsp</result> <result name="input">/WEB-INF/template/page.jsp</result> </global-results> <action name="registrationPage" class="actions.MainAction" method="loadRegistrationPage"></action> <action name="loginPage" class="actions.MainAction" method="loadLoginPage"></action> </package> <package name="operations" extends="struts-default" namespace="/"> <action name="userLogin" class="actions.RegisteredUserAction" method="login"> <result type="redirect" name="input">loginPage.action</result> <result type="redirect" name="success">homePage.action</result> </action> </package> 

I use a template-based approach where the average content (in my case LoginPage.jsp) is dynamically included in the main page.

My page.jsp (which includes mainContent):

 <s:include value="../%{mainContent}.jsp" ></s:include> 

My login.jsp (which is included):

 <%@taglib prefix="s" uri="/struts-tags"%> <s:form action="userLogin" method="POST"> <s:textfield name="userUsername" />Error:<s:fielderror name="userUsername" /> <s:password name="userPassword" /> <s:submit /> </s:form> 

The redirect works correctly and the login form appears, but there are no errors. I even tried using the Message Store interceptor in struts.xml with no luck. :(

+4
source share
1 answer

Use the redirectAction result instead of redirect . And try using the configuration in struts.xml instead of annotations.

 <action name="userLogin" class="actions.RegisteredUserAction" method="login"> <interceptor-ref name="store"> <param name="operationMode">STORE</param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result type="redirectAction" name="input">loginPage</result> <result type="redirectAction" name="success">homePage</result> </action> 
+5
source

Source: https://habr.com/ru/post/1446418/


All Articles