Struts2: How to transfer values ​​from one action to another action. Or from one JSP to another JSP. Without using a session

Hi, I am building a project using Struts2. However, during development, I ran into many problems. The biggest problem is session management in Struts2. There is very limited information related to this.

I have a solution to solve my problem, but I have only limited information that does not meet my needs.

I do not want to use a container session (Apache Tomcat 7), as it will become overhead when several users try to use the application.

I created 2 classes of Action and 2 JSP. The values ​​entered in JSP1 (Login.jsp and LoginAction.java POJO) must be used in another action (HomeAction.java POJO), and later they should be displayed in subsequent JSPs (Home.jsp).

I also use tiles.

I tried using the tag, but I cannot set the value for it.

In Struts2, How do I pass a JSP1 / Action object to another Action / JSP2?

A small example of the login page will be appreciated. for example, the username entered in the text box should appear on another JSP page using two different Action classes.

struts.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default"> <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /> </result-types> <action name="login" class="org.myaction.LoginAction" method="validateLoginCredentials"> <result name="autheticatedUser" type="redirect" >homePage</result> <result name="fail" >/jsp/Login.jsp</result> </action> <action name="homePage" class="org.myaction.HomeAction" method="execute"> <result name="responseOK" type="tiles" >/tile.NextPage</result> <result name="error" type="tiles" >/tile.HomePage</result> </action> </package> </struts> 

LoginAction.java

  public class LoginAction extends ActionSupport { //private static final String SUCCESS = "success"; private static final String FAIL = "fail"; private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String validateLoginCredentials() { if(this.userName.equals("admin") && this.password.equals("allow")) { return "autheticatedUser"; } addActionError(getText("error.login")); return FAIL; } } 

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Login</display-name> <context-param> <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> <param-value>/WEB-INF/tiles.xml</param-value> </context-param> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> <welcome-file-list> <welcome-file>/jsp/Login.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

My question is, In Struts2, How do I pass the username from Login.jsp / LoginAction.java to Home.jsp / HomeAction.java ?

Note. Saving to a session is not a preferred option, but can be used if there is no other way to maintain user state in Struts2.

A small example of the login page will be appreciated. for example, the username entered in the text box should appear on another JSP page using two different Action classes.

Edited

Hi thanks I really appreciate your answer. I guess this will work. Another thing that I forgot to mention. Another class property (JavaBean) is present in the LoginAction.java class.

 public class LoginAction extends ActionSupport { private UserData userData; public void setUserData(UserData userData) { this.userData = userData; } public String getUserData() { return userData; } } 

UserData.java

 public class UserData { private String userName; public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } } 

How can I set the userName property of the UserData object that is in LoginAction.java?

I tried this

 <s:hidden name="userData.userName" value="userName" /> 

but it does not work ....

Do you know what mistake I am making here?

+4
source share
2 answers

Use redirectAction with param

  <action name="login" class="org.myaction.LoginAction" method="validateLoginCredentials"> <result name="autheticatedUser" type="redirectAction"> <param name="actionName">homePage</param> <param name="userName">${userName}</param> </result> <result name="fail" >/jsp/Login.jsp</result> </action> 

Also, remember to add getter / setter userName to the HomeAction class. Then in Home.jsp you can directly use <s:property value="userName"/> (or something else)

+6
source

conversation plugin is a good wrapper around the session that can be used to control the lifetime of objects between actions. ( https://code.google.com/p/struts2-conversation/ )

This solution will help a lot if you have multiple jsp pages based on a step (more than two steps). The in plugin is an annotation database and has the necessary jsp tags.

0
source

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


All Articles