2012-10-21 19 views
2

如何在jsp中輸入新的東西時將值提交給actionclass?目前我能夠從jsp提交給動作類並查看我在動作中創建的新列表。但我希望能夠將我輸入到jsp中的值放回原處。將獲得的值輸入到jsp到動作類

Employee對象類:

public class Employee { 
    String empid; 
    String empname; 

     public String getEmpid() { 
      return empid; 
     } 

     public void setEmpid(String empid) { 
      this.empid = empid; 
     } 

     public String getEmpname() { 
      return empname; 
     } 

     public void setEmpname(String empname) { 
      this.empname = empname; 
     } 

     public Employee(String empid, String empname) { 
      this.empid = empid; 
      this.empname = empname; 
     } 



    } 

僱員JSP:

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1"%> 
    <%@ include file="../imports.jsp" %> 
    @page import="java.util.List"%> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title> Struts Logic Iterate Tag</title> 
</head> 
<body bgcolor="#ffcc66"> 
<html:form action="/logiciterate" styleId="LogicIterateForm" method="post"> 

    <h1>Struts logic:iterate tag </h1> 
     <table style="font-weight:bold"> 
     <tr><td>Employee ID</td> 
     <td>Employee Name</td></tr> 


    <logic:iterate name="LogicIterateForm" property="emp" id="employee"> 
      <tr> 
        <td><html:text name="employee" value="${employee.empid}"   property="empid" 
          indexed="true" /></td> 
        <td><html:text name="employee" value="${employee.empname}" 
          property="empname" indexed="true" /></td> 
      </tr> 
    </logic:iterate> 

  <input type="submit" id="submit" value="Submit" title="Submit" name="Submit" > 

  </html:form> 
</body> 

Action類:

public class LogicIterateAction extends org.apache.struts.action.Action { 

private final static String SUCCESS = "success"; 


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

    if(request.getParameter("Submit") == null) {   

           LogicIterateForm formBean=(LogicIterateForm)form; 
           ArrayList<Employee> list = new ArrayList<Employee>(); 
           list.add(new Employee("11A0","Jack")); 
           list.add(new Employee("11A1","Sam")); 
           list.add(new Employee("11A2","Joe")); 
           list.add(new Employee("11A3","John")); 
           formBean.setEmp(list); 
            return mapping.findForward(SUCCESS); 

} 



else{ 

    LogicIterateForm formBean=(LogicIterateForm)form; 
     ArrayList<Employee> list2 = new ArrayList<Employee>(); 
       list2.add(new Employee("673","Queenie")); 
       list2.add(new Employee("123","Wally")); 
       list2.add(new Employee("345","Spike")); 
       list2.add(new Employee("564","Red")); 
       formBean.setEmp(list2); 



    return mapping.findForward(SUCCESS); 

} 


} 
} 

回答

1

如何生根粉這個 第一步... 寫作味精動作類.. example--
了request.setAttribute( 「信息」, 「用戶刪除成功」 );

第二步......在jsp頁面 gettng味精.. example-- <%= request.getAttribute( 「信息」)%>

是ü尋找這個

+0

沒有,當我鍵入Bob,莎莉,蘇等進入JSP我想這些值回動作類以便在頁面重新加載時提交時,看到我在jsp中輸入的內容而不是缺省值 – billy

+0

abt如何爲bean創建對象並獲取這些值 –

0

使用的例子下面。

在JSP:

<logic:iterate name="monthlyGainLossForm" property="ptcList" id="productTaxCat"> 
    <html:hidden name="productTaxCat" property="ptcId" indexed="true" /> 
    <html:hidden name="productTaxCat" property="ptcName" indexed="true" /> 
    <html:hidden name="productTaxCat" property="ptcActive" indexed="true" /> 
    <mf:tr> 
     <mf:td><bean:write name="productTaxCat" property="ptcName"/></mf:td> 
     <mf:td><bean:write name="productTaxCat" property="ptcActive"/></mf:td> 
     <mf:td><html:checkbox name="productTaxCat" property="msaPtcLtcGLType" styleClass="input" indexed="true"/></mf:td> 
     <mf:td><html:text name="productTaxCat" property="msaPtcLtcGLAmt" styleClass="label" style="width:100px;vertical-align:middle;" disabled="true" indexed="true"/></mf:td> 
     <mf:td><html:checkbox name="productTaxCat" property="msaMthlyStcGLType" styleClass="input" indexed="true"/></mf:td> 
     <mf:td><html:text name="productTaxCat" property="msaMthlyStcGLAmt" disabled="true" styleClass="label" style="width:100px;vertical-align:middle;" indexed="true"/></mf:td> 
     <mf:td><html:checkbox name="productTaxCat" property="nonMsaPtcGLType" styleClass="input" indexed="true"/></mf:td> 
     <mf:td><html:text name="productTaxCat" property="nonMsaPtcGLAmt" disabled="true" styleClass="label" style="width:100px;vertical-align:middle;" indexed="true"/></mf:td> 
     <mf:td><html:checkbox name="productTaxCat" property="nonMsaStcGLType" styleClass="input" indexed="true"/></mf:td> 
     <mf:td><html:text name="productTaxCat" property="nonMsaStcGLAmt" disabled="true" styleClass="label" style="width:100px;vertical-align:middle;" indexed="true"/></mf:td> 
    </mf:tr> 
</logic:iterate> 

在行動:

for(int m = 0; m < monthGainLossForm.getPtcList().size(); m++) { 
    ProductTaxCat prodTaxCat = (ProductTaxCat)monthGainLossForm.getPtcList().get(m); 

    if(prodTaxCat.getMsaMthlyStcGLType()) { 
     selectedPtcTypeCounter++; 
    } 

    if(prodTaxCat.getMsaPtcLtcGLType()) { 
     selectedPtcTypeCounter++; 
    } 

    if(prodTaxCat.getNonMsaPtcGLType()) { 
     selectedPtcTypeCounter++; 
    } 

    if(prodTaxCat.getNonMsaStcGLType()) { 
     selectedPtcTypeCounter++; 
    } 
}