1

私のStrutsプロジェクトの構造は次のとおりです。

page1-> action1-> page2-> action2->page3

私が必要とするのは、入力タグに入力した値page1を でアクセスすることaction2です。

これが私のコードです:

ページ1:

<div class = "container">
    <s:form id = "idinput" method = "post" action = "idEntered">
        Enter id: <input id = "txtid" name = "txtid" type = "text" />
        <input id = "cmdsubmit" name = "cmdsubmit" type = "submit" value = "enter details" />
    </s:form> 
</div>

アクション1:

public class AddId extends ActionSupport {

private int txtid;
    //getter and setter

@Override
public String execute() throws Exception {      
    return "success";
}   
}

ページ2:

<div class = "container">
    <s:form id = "formvalues" method = "post" action = "formEntered">
        <p>Your id entered is: <s:property value = "txtid" /></p>
        First name: <input id = "txtfname" name = "txtfname" type = "text" />
        Last name: <input id = "txtlname" name = "txtlname" type = "text" />
        Age: <input id = "txtage" name = "txtage" type = "text" />
        <input id = "cmdform" name = "cmdform" type = "submit" value = "submit form" />     
    </s:form>
</div>

アクション2:

public class AddForm extends ActionSupport {    
    private String txtfname;
private String txtlname;
private int txtage;
private int txtid;
      //getters and setters 

@Override
public String execute() throws Exception {
    
    return "success";
}
}

すべてを表示します

ページ3:

<div class = "container">
    ID: <s:property value = "txtid" /><br>
    first name: <s:property value = "txtfname" /><br>
    last name: <s:property value = "txtlname" /><br>
    age: <s:property value = "txtage" />
</div>

これは私が問題に直面している場所であり、そこtxtidから値が からにnull渡されていないと推測しましたpage2action2

私が思いついた解決策は、使用することです

<s:hidden value = "%{txtid}" name = "txtid2 /> 

as inpage2の値を使用できるようにする私の形式では、これは実際の解決策というよりもハックのように見えるため、他の提案は大歓迎です。txtidtxtid2action2

4

1 に答える 1

3

あるアクションから別のアクションに渡されたフィールド値を保持したい状況では、フィールドのスコープを構成できます。各アクションにゲッターとセッターを含む同じフィールドを配置するだけです。あなたの場合はaction1andになりaction2ます。フィールド名はtxtid. scopeインターセプターが含まれていないだけでなくdefaultStack、アクション構成で参照する必要があります。

例えば:

<action name="action1" class="com.package.action.AddId">
    <result>/jsp/page2.jsp</result>
    <interceptor-ref name="basicStack"/>
    <interceptor-ref name="scope">
        <param name="key">mykey</param>
        <param name="session">txtid</param>
        <param name="autoCreateSession">true</param>
    </interceptor-ref>
</action>
<action name="action2" class="com.package.action.AddForm">
    <result>/jsp/page3.jsp</result>
    <interceptor-ref name="scope">
        <param name="key">mykey</param>
        <param name="session">txtid</param>
        <param name="autoCreateSession">true</param>
    </interceptor-ref>
    <interceptor-ref name="basicStack"/>
</action> 

mykeyこれで、その下にキーとフィールドを持つスコープができましtxtidた。各アクションでフィールドへのアクセサーを提供すると、フィールド値をあるアクションから別のアクションに転送できます。

上記の例basicStackでは、インターセプター スタックのスケルトンである を使用しており、インターセプターを含むいくつかのインターセプターは含まれていませvalidation

アクションに他の機能が必要な場合は、カスタム スタックを構築するか、アクション構成で他のインターセプターを参照する必要があります。

于 2013-08-27T10:16:55.877 に答える