javax.servlet.jsp.JspException:找不到 bean:“部门”在任何范围内
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>
表单 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;
}
}
部门操作
/*
* 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>
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
/*
* 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果您将特定的数据或属性打印到 jsp 中,那么您需要使用在 struts-config.xml 文件中配置的特定 bean。
这样您就可以访问该数据。
例如
进入struts-config.xml
并进入打印数据的jsp页面。
示例
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
and into the jsp page for print data.
eample
如果 (a) 重命名表单 Bean 配置(操作映射配置中的“名称”属性)或 (b) 将部门集合重命名为表单 Bean 名称之外的其他名称,会发生什么情况?
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?