[英]How to handle Action's form data that is not coming from JSP in Struts 2?
[英]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)並將其設置在request屬性中,然后將操作轉發到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中設置了所有值,為什么它不選擇它們呢?
如果有一種更好的方法可以一定做到,請告訴我,在我看來,應該有一種更好的方法。
提前致謝。
我的錯誤發生在:
ExternalForm myForm = new ExternalForm();
無需解析現有的ActionForm方法,而是通過初始化它來重置值,現在可以使用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.