javax.servlet.jsp.JspException: Cannot find bean: "departments" in any scope
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=开发者_如何学JAVA"id">
show
</html:link>
</td>
</tr>
</logic:iterate>
</table>
<%@include file="/Footer.jsp" %>
</body>
</html>
FORM BEAN
/*
* 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>
Yeah that is right if you print that particular data or property into the jsp then you need to use that particular bean that you have config into the struts-config.xml file.
and that way you can access that data.
for example into the struts-config.xml
<form-beans>
<form-bean name="xyz" type="package.xyz"></form-bean>
</form-beans>
and into the jsp page for print data. eample
<bean:write name="xyz" property="name"/>
What happens if you either (a) rename the form bean config (the "name" attribute in the action mapping config) or (b) rename the departments collection to something else besides the form bean name?
精彩评论