[Struts] How to pass values between jsps

How to pass jsp value by struts framework 2 pattern I will write it down.

① How to get with << bean: parameter >>

addition.jsp


<%@ page contentType="text/html; charset=Windows-31J" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<html:html>
  <head>
    <title>addition</title>
    <meta http-equiv="Content-Type"
                  content="text/html; charset=Windows-31J">
  </head>
<body>

<html:form action="/TestAction">
  <html:text property="leftNum" size="10" maxlength="7" /> +
  <html:text property="rightNum" size="10" maxlength="7" />
  <html:submit property="submit" value="Run"/>
</html:form>
</body>
</html:html>

Action.java is described as follows.

TestAction.java



public final class TestAction extends Action {
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest req,
                               HttpServletResponse res) {

    TestActionForm taf = (TestActionForm)form;

    //Get the value from the TestActionForm
    int leftNum  = taf.getLeftNum();
    int rightNum = taf.getRightNum();
    int resultNum = 0;

    resultNum = leftNum + rightNum;

    //Set the calculation result in TestActionForm
    taf.setResultNum(resultNum);

    //Specify the transition destination after executing the action class.
    return (mapping.findForward("result.jsp"));
  }
}

Define as follows in struts-config.xml.

struts-config.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>

	<form-beans>
		<!--Set ActionForm class to store data in bean-->
		<!--name is an abbreviation, type is a file name including path-->
		<form-bean
			name="Action"
			type="struts.TestActionForm"/>
	</form-beans>

	<action-mappings>
		<!--action sets the Action class that actually processes-->
		<!--path is an abbreviation when specified by jsp-->
		<!--name is the bean to use(ActionForm)Abbreviation of-->
		<!--type is the file name including path-->
		<!--scope is a data transfer format-->
		<!--input is executed jsp-->
		<action
			path="/Welcome"
			forward="/pages/Welcome.jsp"/>
		<action
			path="/TestAction"
			type="struts.TestAction"
			name="Action"
			scope="request"
			input="/addition.jsp">
			<forward name="result.jsp" path="/result.jsp"/>
		</action>
	</action-mappings>
	<message-resources parameter="java/MessageResources"/>
</struts-config>

The following is used for jsp of the calculation result.

result.jsp


<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<bean:parameter id ="left_Num" name ="leftNum"/>
<bean:parameter id ="right_Num" name ="rightNum"/>

<html:html>
  <head>
    <title>Calculation result</title>
    <meta http-equiv="Content-Type"
            content="text/html; charset=Windows-31J">
  </head>
<body>
<%= left_Num %> + <%= right_Num %> =

</body>
</html:html>

Enter the request parameter you want to use for name in the <bean: parameter> tag, and enter an appropriate value for id. Variables are stored in the page scope by defining them in id. What is the page scope? It means that the valid range of the variable is in jsp.

Enter 3 in the left text box and 4 in the right text box and press the execute button. Then, the execution result is as follows.

3 + 4 =

The value has been passed. However, since <bean: parameter> is a tag that acquires the request parameter and displays it on the screen, the calculation result cannot be displayed. So, get the calculation result by the following method.

② How to get with << bean: write >>

Add one sentence to jsp of the calculation result.

result.jsp


<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<bean:parameter id ="left_Num" name ="leftNum"/>
<bean:parameter id ="right_Num" name ="rightNum"/>

<html:html>
  <head>
    <title>Calculation result</title>
    <meta http-equiv="Content-Type"
            content="text/html; charset=Windows-31J">
  </head>
<body>
<%= left_Num %> + <%= right_Num %> =

<bean:write name="Action" property="resultNum" scope="request" /> 

</body>
</html:html>

For name, enter the bean name defined in struts-config.xml. Enter the bean property name (variable defined in Form) in property. The scope defines where to search. Since the calculation result is saved in the HTTP request, select the request this time. The items that can be set in the scope are as follows.

page: JSP page request: HTTP request session: HTTP session application: web application

As before, enter 3 in the left text box and 4 in the right text box, and press the execute button. The calculation result is also displayed.

3 + 4 = 7

I was able to hand over the calculation results! !!

Recommended Posts

[Struts] How to pass values between jsps
How to pass Oracle Java Silver
Java: How to send values from Servlet to Servlet
How to pass the value to another screen
[Spring MVC] How to pass path variables
How to switch between multiple Java versions
How to use scope and pass processing (Jakarta)
[ruby] How to receive values from standard input?
Ruby How to convert between uppercase and lowercase
[Rails] How to search by multiple values ​​with LIKE
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
How to pass image pixel information natively from Unity
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
Steps to deploy Struts 2.5.8
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use hidden_field_tag
How to write dockerfile
How to uninstall Rails
How to install docker-machine
[How to use label]
How to make shaded-jar
How to write docker-compose
How to use identity
How to use hashes
How to write Mockito
How to create docker-compose
How to use JUnit 5
How to install MySQL
How to write migrationfile
How to build android-midi-lib
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to install ngrok
How to type backslash \
How to concatenate strings
How to prevent conflicts between JBoss modules and application libraries
How to pass an object to Mapper in MyBatis without arguments
Difference between Java and JavaScript (how to find the average)
How to get values in real time with TextWatcher (Android)
[Rails] Differences between redirect_to and render methods and how to output render methods
Differences in how to handle strings between Java and Perl
How to make th: value of select have multiple values
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
How to dynamically switch between FIN and RST in Netty