4

私は助けが必要です。私は複数のページと複数のフォームがあるプロジェクトに取り組んでいます。各ページには1つのフォームがあります。あるjspから別のjspに値を渡すことができる必要があります。私は何をすべきか?

SpringMVCは初めてです。スプリング2.5.6を使用しています。

これが私のデザインです:

formPage1.jsp-> Controller1->formPage2a.jsp->Controller2にはvalfrmpg1&pg2aが必要です。formPage1.jsp-> Controller1->formPage2b.jsp->Controller3にはvalfrmpg1&pg2bが必要です。formPage1.jsp-> Controller1->formPage2c.jsp->Controller4にはvalfrmpg1&pg2cが必要です。

上記のように、formPage1.jspはformPage2a、formPage2b、またはformPage2cのいずれかをロードできます。formPage1.jspで提供された入力に基づいて、コントローラー(SimpleFormControllerの拡張)に移動し、コントローラーはuser=コマンドオブジェクトによって入力された値を取得します。

これらのコマンドオブジェクト値を別のコントローラーに送信するときに、formPage2a、formPage2b、またはformPage2cのいずれかで使用できるようにしたいと思います。

現在のコードは次のとおりです。


formPage1.jsp:

<form:form method="post" commandName="gainLossRequest">
            <form:errors path="*" cssClass="error"/>
            <table>
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td><h4>Choose Client</h4></td>
                                <td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
                            </tr>
                        </table>
                    </td>
                    <td>
                        <form:select path="client">
                            <form:option value="none" label="Select" />
                            <form:option value="abc" label="abc" />
                            <form:option value="def" label="def" />
                            <form:option value="xyz" label="xyz" />
                        </form:select>
                    </td>
                </tr>
<tr>
                    <td colspan="2">
                        <input type="reset" value="Reset" />
                        <input type="submit" value="Next" />
                    </td>
                </tr>
            </table>
            </form:form>

Controller1.java

public class TestController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    public TestController() {
        logger.info("entering TestController.constructor..");
        setCommandClass(UserPreference.class);
        setCommandName("userPreference");
    }

    public ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws ServletException {
        logger.info("entering TestController.onSubmit all..");

        UserPreference userPreference = (UserPreference) command;

        ModelAndView view = null;

        if ("abc".equals(userPreference.getClient())) {
            GainLossRequest gainLossRequest = new GainLossRequest(userPreference);
            view = new ModelAndView("redirect:/test/gainLossRequest.htm",
                    "gainLossRequest", gainLossRequest);
                } else if ("def".equals(userPreference.getClient())) {
            IncomingPositionsRequest incomingPositionsRequest = new IncomingPositionsRequest();
            view = new ModelAndView(
                    "redirect:/test/incomingPositionsRequest.htm",
                    "incomingPositionsRequest", incomingPositionsRequest);
        } else if ("xyz".equals(userPreference
                .getClient())) {
            TaxStrategyRequest taxStrategyRequest = new TaxStrategyRequest();
            view = new ModelAndView("redirect:/test/taxStrategyRequest.htm",
                    "taxStrategyRequest", taxStrategyRequest);
        }
        }
}

formPage2a.jsp

<form:form method="post" commandName="gainLossRequest">
            <form:errors path="*" cssClass="error"/>
            <table style="width: 60%">
                <tr>
                     <td>Account Number (s):</td>
                     <td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
                </tr>
                <tr>
                    <td>
                        User Chosen Client: 
                    </td>
                    <td>
                        <c:out value="${gainLossRequest.client}"/>
                    </td>
                </tr>
                                <tr colspan="2">
                                        <td>
                        <input type="reset" value="Reset" />
                        <input type="submit" value="Submit" />
                    </td>
                </tr>

ディスパッチャサーブレット設定

<!-- setupNew.jsp is the first jsp --> 

<bean name="/test/setupNew.htm" class="chimeraweb.web.TestController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="userPreference"/>
        <property name="commandClass" value="chimeraweb.service.UserPreference"/>
        <property name="validator">
            <bean class="chimeraweb.service.UserPreferenceValidator"/>
        </property>
        <property name="formView" value="/test/setupNew"/>
    </bean>


<!-- gainLossRequest.jsp is the 2nd jsp where I want to display the values captured in the first jsp page -->

    <bean name="/test/gainLossRequest.htm" class="chimeraweb.web.SimpleGainLossController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="gainLossRequest"/>
        <property name="commandClass" value="chimeraweb.service.GainLossRequest"/>
        <property name="validator">
            <bean class="chimeraweb.service.GainLossValidator"/>
        </property>
        <property name="formView" value="/test/gainLossRequest"/>
    </bean>

助けてください!!

4

3 に答える 3

7

次のように、手動でセッション属性を使用することもできます。

public ModelAndView handleRequest(HttpServletRequest request){
      request.getSession().getAttribute("nameOfAttribute");
}

これを注釈付きコントローラーとして記述してしまったことをお詫びします。xml構成コントローラーがこの機能を提供しているかどうかは思い出せません...

そのためのSpringコードはありません。もう1つのオプションは、コントローラーで@SessionAttributeアノテーションを使用することです。

@Controller 
@SessionAttributes("nameOfAttribute")
public class MyController{
//your session attribute can be accessed in controller methods using @ModelAttribute
public ModelAndView handleRequest(@ModelAttribute("nameOfAttribute")){
      session.getAttribute("nameOfAttribute");
}

ノート

完了したら、セッション属性をクリアする必要があります。

request.getSession().setAttribute("nameOfAttribute", null);
于 2012-06-04T13:23:20.840 に答える
2

JB Nizetが前述したように、非表示の変数を使用して、ユーザーが最初のページに入力した情報を永続化する必要があります。または、対応するコントローラーで返されるモデル属性に値を設定できます。

あなたのための擬似コード。

formPage1.jsp->Controller1->Requestオブジェクトから取得してこのフォームに値を設定します->formPage2a.jsp->Controller2にはpg1とpg2aの両方のvalfrmがあります。

このように、セッション属性を維持する必要はありません。

于 2012-06-04T13:56:35.117 に答える
1

最も簡単な方法は、2番目のページの非表示フィールドを使用して、ユーザーが最初のフォームで入力した内容を保存することです。このようにして、最初のページのフィールドを含むすべてのフィールドが2番目のフォームで送信されます。

于 2012-06-03T16:07:08.890 に答える