来自外部动作时不显示struts动作表单

时间:2015-11-03 18:17:07

标签: java jsp struts actionform

我有两个应用程序(比如ExternalAPP和InternalAPP),每个应用程序在同一台服务器上都有自己的EAR,我使用的是Struts 1.2。我们的想法是从InternalAPP调用ExternalAPP的一个jsp文件,并用InternalAPP的信息填充它。

由于我无法从InternalAPP访问ExternalAPP的ActionForm,我创建了一个类似的表单(InternalForm)并在请求属性中设置它,然后将操作转发给ExternalAPP以设置它的名为ExternalForm的操作表单:

InternalAPP的代码:

public final String process(HttpServletRequest request, ActionForm form)
            throws Exception {
    ...
    InternalForm myForm = new InternalForm();
    myForm.setTo("email@email.com");
    myForm.setFrom("someone@email.com");

    //pass this form to the next action in json form
    ObjectMapper mapper = new ObjectMapper();
    String myFormToJson = mapper.writeValueAsString(myForm);

    request.setAttribute("myForm", myFormToJson);

    return "forwardExternalAction";
}

InternalAPP的struts-config.xml

<global-forwards>
    ....
    <forward name="forwardExternalAction" path="/forward/path/externalFormInit.do"/>
</global-forwards>

ExternalAPP的代码:

<action path="/path/externalFormInit" type="com.action.ParseFormAction" >
    <forward name="success" path="/externalAction.do" />
</action>

<action path="/externalAction"
    type="org.apache.struts.actions.ForwardAction"
    name="ExternalForm"
    validate="false"
    scope="request"
    input="/task/myDesiredPage.jsp">
    <forward name="success" path="/task/myDesiredPage.jsp" />
        <forward name="error" path="/task/myDesiredPage.jsp" />
</action>

ParseFormAction.java

public ActionForward doPerform(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    JSONObject jsonObj = new JSONObject((String) request.getAttribute("myForm"));
    ExternalForm  myForm = new ExternalForm();
    myForm.setTo(jsonObj.getString("to"));
    myForm.setFrom(jsonObj.getString("from"));

    request.setAttribute("ExternalForm", myForm);

    return mapping.findForward("success");
}

myDesiredPage.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<html>
....    
                <html:form action="/path/sendOutForm.do" method="POST"  >

                    <div class="form-body">
                        <div class="frm-row">

                            <div class="colm6">

                                <div class="section">
                                    <label class="field prepend-icon">
                                        <html:text property="from" styleClass="gui-input" styleId="fromStyle" disabled="true"  />
                                        <span class="field-icon"><i class="fa fa-envelope"></i></span>
                                    </label>
                                </div><!-- end section -->

                                <div class="section">
                                    <label class="field prepend-icon">
                                        <html:text property="to" styleClass="gui-input" styleId="toStyle" />
                                        <span class="field-icon"><i class="fa fa-envelope"></i></span>
                                    </label>
                                </div><!-- end section -->
                            </div><!-- end .colm6 section -->

                        </div><!-- end .frm-row section -->                             
            </html:form>
...     
</html> 

我可以从InternalAPP获得myDesiredPage.jsp,但它不会填充我在请求中发送的信息,所有值都将为空。

我错过了什么?我在调用JSP之前在action类中设置了ExternalAction中的所有值,为什么不提取它们呢?

如果有更好的方法可以做到这一点请告诉我,在我看来应该有更好的方法来做到这一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我的错误是:

 ExternalForm  myForm = new ExternalForm();

而不是解析现有的ActionForm进入方法...通过初始化它重置值,现在它可以工作。