2011-08-22 21 views
1
JSP PAGE 
<%-- 
    Document : DeptListing 
    Created on : 20-Aug-2011, 10:12:36 
    Author  : LenasalonM01 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<%@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"%> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Department listing</title> 
    </head> 
    <body> 
     <%-- <jsp:include page="Header.jsp"> 
      <jsp:param name="header" value="Dept Listing"/> 
</jsp:include>--%> 
     <table> 
      <logic:iterate id="dept" name="departments"> 
       <tr> 
        <td> 
         <bean:write name="dept" property="name" /> 
        </td> 
        <td> 
         <html:link page="/listEmployees.do" 
            paramId="deptid" paramName="dept" 
            paramProperty="id"> 
          show 
         </html:link> 
        </td> 
       </tr> 
      </logic:iterate> 
     </table> 
     <%@include file="/Footer.jsp" %> 
    </body> 
</html> 

form bean的javax.servlet.jsp.JspException:在任何範圍內 「部門」

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.hrms; 

import javax.servlet.http.HttpServletRequest; 
import org.apache.struts.action.ActionErrors; 
import org.apache.struts.action.ActionMapping; 
import org.apache.struts.action.ActionMessage; 

/** 
* 
* @author LenasalonM01 
*/ 
public class EmployeeForm extends org.apache.struts.action.ActionForm { 

    public static final String EDIT_MODE = "edit"; 
    public static final String DELETE_MODE = "delete"; 
    public static final String ADD_MODE = "add"; 
    String action; 
    Employee employee; 

    public EmployeeForm() { 
     employee = new Employee(); 
     action = EmployeeForm.ADD_MODE; 
    } 

    public Employee getEmployee() { 
     return employee; 
    } 

    public void setEmployee(Employee employee) { 
     this.employee = employee; 
    } 

    /** 
    * Returns the action. 
    * @return String 
    */ 
    public String getAction() { 
     return action; 
    } 

    /** 
    * Sets the action. 
    * @param action The action to set 
    */ 
    public void setAction(String action) { 
     this.action = action; 
    } 

    /** 
    * @see org.apache.struts.action.ActionForm#reset(ActionMapping, 
    HttpServletRequest) 
    */ 
    /** 
    * 
    */ 
    @Override 
    public void reset(ActionMapping mapping, 
      HttpServletRequest request) { 
     this.employee = new Employee(); 
     this.action = ADD_MODE; 
    } 

    /** 
    * This is the action called from the Struts framework. 
    * @param mapping The ActionMapping used to select this instance. 
    * @param request The HTTP Request we are processing. 
    * @return 
    */ 
    @Override 
    public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) { 
     ActionErrors errors = new ActionErrors(); 
     if ((employee.getFirstName() == null) 
       || (employee.getFirstName().length() < 3)) { 
      errors.add("FirstName", new ActionMessage("error.employee.firstname")); 

     } 
     return errors; 
    } 
} 

DEPARTMENT ACTION

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package com.hrms; 

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

/** 
* 
* @author LenasalonM01 
*/ 
public class ListDepartmentsAction extends org.apache.struts.action.Action { 

    /* forward name="success" path="" */ 

    /** 
    * This is the action called from the Struts framework. 
    * @param mapping The ActionMapping used to select this instance. 
    * @param form The optional ActionForm bean for this request. 
    * @param request The HTTP Request we are processing. 
    * @param response The HTTP Response we are processing. 
    * @throws java.lang.Exception 
    * @return 
    */ 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
     request.setAttribute("departments", Dept.getDepartments()); 
     return mapping.findForward("listing"); 
    } 
} 

的struts-config

<action input="/" 
       name="EmployeeForm" 
       path="/listEmployees" 
       scope="request" 
       validate="true" 
       type="action.ListEmployeesAction"> 
      <forward name="listing" path="/EmployeeListing.jsp"/> 
     </action> 
     <action path="/listDepartments" 
       scope="request" 
       name="departments" 
       validate="true" 
       type="action.ListDepartmentsAction"> 
      <forward name="listing" path="/DeptListing.jsp"/> 
     </action> 
     <action path="/editEmployee" 
       type="action.EditEmployeeAction" 
       name="employeeForm" 
       attribute="employeeForm" 
       input="/EmployeeForm.jsp" 
       scope="request" 
       validate="true"> 
      <forward name="form" path="/EmployeeForm.jsp"/> 
      </action> 
     <action input="/EmployeeForm.jsp" 
       name="employeeForm" 
       action="action.UpdateEmployeeAction" 
       path="/updateEmployee"    
       scope="request" 
       type="action.UpdateEmployeeAction"> 
        <forward name="listing" path="/EmployeeListing.jsp"/> 
       </action> 
<!--   <action input="/employee_registration.jsp" name="loginform" path="/login" type="com.hrms.formlogin"> 
     <forward name="employee_reg" path="/register_employee.jsp"/> 
     </action>--> 

    </action-mappings> 
:找不到豆

回答

1

如果您(a)重命名fo rm bean config(動作映射配置中的「name」屬性)或(b)將departments集合重命名爲除表單bean名稱外的其他內容?

+0

我已經完成了後者(b),現在沒關係。謝謝@Dave牛頓 – mykey

2

是的,如果你將特定的數據或屬性打印到jsp中,那麼你需要使用你已經配置到struts-config.xml文件中的那個特定的bean。

這樣你就可以訪問這些數據。

例如 成在struts-config.xml

<form-beans> 
    <form-bean name="xyz" type="package.xyz"></form-bean> 
</form-beans> 

和成用於打印數據jsp頁面。 範例

<bean:write name="xyz" property="name"/>