struts 来自外部操作时不显示操作表单
struts action form not displaying when coming from an external action
我有两个应用程序(比如 ExternalAPP 和 InternalAPP),每个应用程序在同一台服务器上都有自己的 EAR,我使用的是 Struts 1.2。这个想法是从 InternalAPP 调用 ExternalAPP 的 jsp 文件之一,并用 InternalAPP 的信息填充它。
由于我无法从 InternalAPP 访问 ExternalAPP 的 ActionForm,我创建了一个类似的表单 (InternalForm) 并将其设置在请求属性中,然后将操作转发给 ExternalAPP 以设置其名为 ExternalForm 的操作表单:
内部APP代码:
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";
}
内部APP的struts-config.xml
<global-forwards>
....
<forward name="forwardExternalAction" path="/forward/path/externalFormInit.do"/>
</global-forwards>
外挂APP代码:
<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 之前,我在操作 class 中设置了 ExternalAction 中的所有值,为什么它不拾取它们?
如果有更好的方法一定要告诉我,我觉得应该有更好的方法。
提前致谢。
我的错误是:
ExternalForm myForm = new ExternalForm();
不是解析现有的 ActionForm 进入方法...通过初始化它我重置了值,现在它可以工作了。
我有两个应用程序(比如 ExternalAPP 和 InternalAPP),每个应用程序在同一台服务器上都有自己的 EAR,我使用的是 Struts 1.2。这个想法是从 InternalAPP 调用 ExternalAPP 的 jsp 文件之一,并用 InternalAPP 的信息填充它。
由于我无法从 InternalAPP 访问 ExternalAPP 的 ActionForm,我创建了一个类似的表单 (InternalForm) 并将其设置在请求属性中,然后将操作转发给 ExternalAPP 以设置其名为 ExternalForm 的操作表单:
内部APP代码:
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";
}
内部APP的struts-config.xml
<global-forwards>
....
<forward name="forwardExternalAction" path="/forward/path/externalFormInit.do"/>
</global-forwards>
外挂APP代码:
<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 之前,我在操作 class 中设置了 ExternalAction 中的所有值,为什么它不拾取它们?
如果有更好的方法一定要告诉我,我觉得应该有更好的方法。
提前致谢。
我的错误是:
ExternalForm myForm = new ExternalForm();
不是解析现有的 ActionForm 进入方法...通过初始化它我重置了值,现在它可以工作了。