论坛首页 Java版 Webwork

使用Struts 2的select标签的问题

浏览 4251 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2007-05-11
有一个Employee类:

public class Employee implements java.io.Serializable {

	private int id;
	private String name;
	private Department department;

	public Employee() {
	}

	public int getId() {
		return this.id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Department getDepartment() {
		return this.department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

}


每个Employee属于一个Department,Department类如下:

public class Department implements java.io.Serializable {

	private int id;
	private String name;

	public Department() {
	}

	public int getId() {
		return this.id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}

}


我为这两个类分别写了相应的DAO(EmployeeDao、DepartmentDao)、Action(EmployeeAction、DepartmentAction)并实现了相应的crud操作。其中在DepartmentDao中实现了findAll方法如下:

	public List findAll() {
		String sql = " FROM Department ORDER BY id";
		return this.getHibernateTemplate().find(sql);
	}


DepartmentDao在applicationContext.xml中已经配置为一个bean:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<!-- DAO -->
	<bean id="departmentDao"
		class="cn.net.cogent.powermedical.sysinfo.dao.DepartmentDao">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
  <!-- 其它配置略 -->

</beans>


打算在编辑一个Employee时允许用户从已有的Department列表中选择一个指定给department属性,jsp页面代码如下:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>

<title>编辑员工</title>
</head>
<body>
<s:form name="editForm" action="save" validate="true">
	 <s:textfield label="编号" name="employee.id" readonly="true"/>
	 <s:textfield label="姓名" name="employee.name"/>
	 <s:if test="null == employee">
	 	<s:hidden name="employee.id" value="%{id}"/>
	 </s:if>
     <s:else>
	 	<s:hidden name="employee.id" />
	 </s:else>
	 <s:select label="部门" name="employee.department" value="employee.department.id"
	     list="#departmentDao.findAll()" listKey="id" listValue="name" />
	 <s:submit value="保存" />
</s:form>
</body>
</html>


但在访问该页面的时候报如下错误:

WARN - Caught an exception while evaluating expression '#departmentDao.findAll()' against value stack
java.lang.NullPointerException: target is null for method findAll
	at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:848)


这是怎么回事呢?我应该怎样配置才能在jsp页面上显示部门列表,并允许用户选择一个呢?
   
最后更新时间:2007-05-11

应该是这样的形式得到部门列表:

  1. list="#getAllDepartment.departmentList"   

其中getAllDepartment是个action,需要在jsp页面和xml文件里申明

departmentList是action里的一个属性,需要有get取值函数

在action里面要干的事是给departmentList付值

 

   
0 请登录后投票
最后更新时间:2007-05-12
to 温柔一刀:

按照你的建议进行了修改,jsp页面代码为:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>  
<%@ taglib prefix="s" uri="/struts-tags" %>  
  
<html>  
<head>  
  
<title>编辑员工</title>  
</head>  
<body>  
<s:form name="editForm" action="save" validate="true">  
     <s:textfield label="编号" name="employee.id" readonly="true"/>  
     <s:textfield label="姓名" name="employee.name"/>  
     <s:if test="null == employee">  
        <s:hidden name="employee.id" value="%{id}"/>  
     </s:if>  
     <s:else>  
        <s:hidden name="employee.id" />  
     </s:else>  
     <s:select label="部门" name="employee.department" value="employee.department.id"  
         list="#departmentAction.departments" listKey="id" listValue="name" />  
     <s:submit value="保存" />  
</s:form>  
</body>  
</html>
 

其中departmentAction已经在applicationContext.xml中配置为一个bean,与departments属性相关的代码如下:

public class DepartmentAction extends ActionSupport {

	private List departments;

	public List getDepartments() {
		if (null == departments)
			departments = departmentDao.findAll();
		return this.departments;
	}
}


访问页面时报如下错误:

2007-5-12 8:53:43 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'employee.department': The requested list key '#departmentAction.departments' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
	at org.apache.struts2.components.Component.fieldError(Component.java:231)


departmentAction的departments被声明为List类型,为什么还会出错呢?
   
0 请登录后投票
最后更新时间:2007-05-12
你那样做是不对的

在jsp页面要有这样的申明:
<ww:action name="departmentAction" id="departmentAction"/>


name是指在xml里面配置的action,id是这样引用需要的#departmentAction

你的action大概应该是这样子的:
public class DepartmentAction extends ActionSupport {   
		  
	    private List departments;   
	  
	    public List getDepartments() {   	        
	        return this.departments;   
	    }   
	    
	    public String getAllDepartment() {
	    	departments = departmentDao.findAll();   
			return null;
		}
	}  


xml配置文件大概是这样子的:

<action name="departmentAction"
			class="xxx.DepartmentAction " method="getAllDepartment">
</action>
   
0 请登录后投票
最后更新时间:2007-05-12
谢谢 温柔一刀:

确实是忘了在jsp页面上声明departmentAction了。添加了声明后就正常了
   
0 请登录后投票
论坛首页 Java版 Webwork

跳转论坛:
JavaEye推荐