2
2

More than 3 years have passed since last update.

【struts】 jsp間の値受け渡し方法

Last updated at Posted at 2020-03-06

strutsフレームワークによるjspの値受け渡しのやり方2パターン書き残しておきます。

① <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>足し算</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="実行"/>
</html:form>
</body>
</html:html>

Action.javaは以下の通りに記載する。

TestAction.java

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

    TestActionForm taf = (TestActionForm)form;

    //TestActionFormから値を取りだす
    int leftNum  = taf.getLeftNum();
    int rightNum = taf.getRightNum();
    int resultNum = 0;

    resultNum = leftNum + rightNum;

    //計算結果をTestActionFormにセットする
    taf.setResultNum(resultNum);

    //アクション・クラス実行後の遷移先を指定する。
    return (mapping.findForward("result.jsp"));
  }
}

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>
        <!--beanにはデータを格納するActionFormクラスを設定-->
        <!--nameは略称typeはpathを含むファイル名-->
        <form-bean
            name="Action"
            type="struts.TestActionForm"/>
    </form-beans>

    <action-mappings>
        <!--actionは実際に処理を行うActionクラスを設定-->
        <!--pathはjspで指定するときの略称-->
        <!--nameは使用するbean(ActionForm)の略称-->
        <!--typeはpathを含むファイル名-->
        <!--scopeはデータ転送形式-->
        <!--inputは実行される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>

計算結果のjspは以下を使用。

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>計算結果</title>
    <meta http-equiv="Content-Type"
            content="text/html; charset=Windows-31J">
  </head>
<body>
<%= left_Num %> + <%= right_Num %> =

</body>
</html:html>

bean:parameterタグでnameに使いたいリクエストパラメータを入力し、idに適当な値を入れます。
idに定義することでpageスコープに変数を格納してます。
pageスコープはなんぞや?って調べてみると、変数の有効範囲はjsp内であることを意味しているとのことです。

左のテキストボックスに3,右のテキストボックスに4を入力して、実行ボタンを押下。
すると、実行結果は以下のとおり。

3 + 4 =

値の受け渡しができました。しかし、bean:parameterはリクエストパラメータを取得し、画面に表示させるタグなので、計算結果は表示できません。なので次の方法で計算結果を取得します。

②<bean:write>で取得する方法

計算結果のjspに1文付け加えます。

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>計算結果</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>

nameにはstruts-config.xmlで定義したbean名を入力します。
propertyにはbeanのプロパティ名(Formに定義した変数)を入力します。
scopeにはどの場所から検索するかを定義します。計算結果はHTTPリクエストに保存してあるので、今回はリクエストを選択します。スコープに設定できる項目は以下の通りです。

page:JSPページ
request:HTTPリクエスト
session:HTTPセッション
application:Webアプリケーション

先程と同じく、左のテキストボックスに3,右のテキストボックスに4を入力して、実行ボタンを押下。
計算結果も表示されました。

3 + 4 = 7

計算結果も受け渡し出来ました!!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2