2011-08-04 70 views
4

我有一個jsp作爲我的視圖,它顯示了一個用於添加新用戶/更新用戶(如果已選擇)的表單。如果選擇了用戶,我無法弄清楚如何預填充我的表單。我使用兩個操作來閱讀有關解決方案,使用相同的表單,其中一個僅用於填充字段,另一個用於提交數據。 但是,這對我不起作用,因爲我的動作(在表單的action屬性中定義的動作)在加載jsp時未被調用(無法真正解釋這個,菜單和頁面在xml文件)。我不明白如何在我的jsp中指定第二個動作,以及如何確保在首次加載jsp時調用動作。如果可能的話,我更喜歡不涉及AJAX的解決方案。謝謝。在Struts1中預填充表格

回答

8

當你擁有Struts的力量時,你爲什麼想要去AJAX?我有一個簡單的例子(它已經過測試)。

MyForm.java

package com.tusar.action; 

    import java.io.Serializable; 
    import org.apache.struts.action.ActionForm; 
    import org.apache.struts.action.ActionMapping; 
    import javax.servlet.http.HttpServletRequest; 

    public class MyForm extends ActionForm implements Serializable{ 

     private static final long serialVersionUID = 1043346271910809710L; 
     private String fullName = null; 

     public String getFullName() { 
      return fullName; 
     } 

     public void setFullName(String fullName) { 
      this.fullName = fullName; 
     } 

     /*This method will be called when you press the reset button 
       or load the form. You may want to populate the form here also*/ 

     public void reset(ActionMapping mapping, HttpServletRequest request){ 
     String reset = (String)request.getAttribute("myForm.reset"); 
      if ((null != reset)|| ("true".equals(reset))) { 
       fullName = null; 
      } 
     } 
    } 

MyFormSetupAction.java

package com.tusar.action; 

    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.apache.struts.action.Action; 
    import org.apache.struts.action.ActionForm; 
    import org.apache.struts.action.ActionForward; 
    import org.apache.struts.action.ActionMapping; 

    public class MyFormSetupAction extends Action{ 

     /*Set your form-bean properties here*/ 

     @Override 
     public ActionForward execute(ActionMapping mapping, ActionForm form, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 
      MyForm hwForm = (MyForm) form; 
      hwForm.setFullName("tusar"); 
      return mapping.findForward("success"); 
     } 

    } 

MyFormSuccessAction.java

package com.tusar.action; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 

public class MyFormSuccessAction extends Action{ 

    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
     return mapping.findForward("success"); 
    } 

} 

的struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?> 

    <!DOCTYPE struts-config PUBLIC 
     "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
     "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 

    <struts-config> 

    <!-- ==================== Form Bean Definitions --> 

    <form-beans> 
    <form-bean name="myForm" type="com.tusar.action.MyForm"> 
    <form-property name="fullName" type="java.lang.String"/> 
    </form-bean> 

    <!-- ============= Global Forward Definitions --> 

    <global-forwards> 
    <!-- Default forward to "Welcome" action --> 
    <!-- Demonstrates using index.jsp to forward --> 
    <forward name="home" path="/home.do"/> 
    </global-forwards> 

    <!-- ===================== Action Mapping Definitions --> 

    <action-mappings> 

    <!-- This action will load the form--> 

    <action path="/home" 
     type="com.tusar.action.MyFormSetupAction" 
     name="myForm" 
     validate="false" 
     input="/WEB-INF/jsp/home.jsp"> 

     <forward name="success" path="/WEB-INF/jsp/home.jsp" /> 
    </action> 

    <!-- This action will evalutae the form and pass form data to 
      success page--> 

    <action path="/successAction" 
     type="com.tusar.action.MyFormSuccessAction" 
     name="myForm" 
     validate="true" 
     input="/WEB-INF/jsp/home.jsp"> 

     <forward name="success" path="/WEB-INF/jsp/success.jsp" /> 
    </action> 

    </action-mappings> 

     <!-- ============= Message Resources Definitions --> 

    <message-resources parameter="MessageResources" /> 

    </struts-config> 

回到Home.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page isELIgnored="false"%> 
<html> 
<head> 
<title>Struts 1</title> 
</head> 
<body> 

    <html:form action="/successAction.do"> 

    Name: 
    <html:text property="fullName"></html:text> 

    <html:submit value="Next"></html:submit> 

    <html:reset value="Cancel"></html:reset> 

</html:form> 

</body> 
</html> 

的success.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> 
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ page isELIgnored="false"%> 
<html> 
<head> 
<title>Successful !</title> 
</head> 
<body> 
    <h3>Your details:</h3> 

    Name: 

    <bean:write name="myForm" property="fullName" /> 

</body> 
</html> 

每次你打電話的home.do動作,全名屬性填充「tusar」。只是評論,如果你需要任何進一步的協會,我會很樂意幫助你。謝謝!

+3

這段代碼讓我想起爲什麼人們在有機會的時候儘早離開Struts 1 - 一個噩夢,只是爲了得到一個用戶名的形式:( –

+0

@tusar:多頁表單存在問題點擊後退按鈕重新填充表單,而不考慮用戶所做的更改(在編輯模式下) – Sefran2

+0

@Cricket,嗨!我認爲,由於瀏覽器中的高速緩存,您正在遇到該問題。緩存在瀏覽器中(設置響應頭緩存等)請將你的問題作爲一個單獨的線程 – tusar

0

There 是選擇用戶時調用的操作。在向前返回動作之前,應將該用戶的屬性複製到動作表單中。

沒有任何配置信息,除此之外很難提供幫助。