How to pass jsp value by struts framework 2 pattern I will write it down.
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.
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