struts的下拉列表问题,救急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111
我要做的是在一个页面里有一个下拉列表,列表的内容是从数据库中取出,取出数据是在ACTION里,并装入一个ArrayList中,用request.setAttribute发送到JSP页面,但是老是显示org.apache.jasper.JasperException: Cannot find bean under name list错误,我在网上找了二天了,都没解决问题,郁闷死了,请各位大虾帮帮忙吧,代码我贴在下面:
PositionSetAction.java:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xsl
package com.hrmis.struts.action;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.hrmis.dbbean.DbConn;
import com.hrmis.struts.form.PositionSetActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
/**
* MyEclipse Struts
* Creation date: 04-12-2007
*
* XDoclet definition:
* @struts.action path= "/position " name= "positionSetForm " input= "/positionSet.jsp " scope= "request "
*/
public class PositionSetAction extends Action {
// --------------------- Instance Variables
// --------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PositionSetActionForm positionSetForm = (PositionSetActionForm) form;
DbConnconn=new DbConn();
String query= "SELECT * FROM `department` ";
System.out.println(query);
ResultSet rs=conn.executeQuery(query);
ArrayList departmentList = new ArrayList();
try{
while(rs.next()){
departmentList.add(new LabelValueBean(rs.getString( "departmentName "),rs.getString( "departmentId ")));
}
request.setAttribute( "list ",departmentList);
}catch(SQLException e){
System.err.println( "err: "+e.getMessage());
}
// TODO Auto-generated method stub
return mapping.findForward( "positionSet ");
}
}
persitionSet.jsp:
<%@ page language= "java " import= "java.util.* " pageEncoding= "UTF-8 "%>
<%@ page import= "org.apache.struts.util.LabelValueBean " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-bean " prefix= "bean " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-html " prefix= "html " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-logic " prefix= "logic " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-tiles " prefix= "tiles " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-template " prefix= "template " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-nested " prefix= "nested " %>
<html>
<head>
<title> JSP for positionSetForm form </title>
<link href= "/hrmis/style/main.css " rel= "stylesheet " type= "text/css ">
<Script Language= "JavaScript ">
function departmentSet(theForm)
{
var flag=false;
if(theForm.departmentId.value == " ")
{
alert( "请输入部门编号! ");
theForm.departmentId.focus();
return false;
}
if(theForm.departmentName.value == " ")
{
alert( "请输入部门名称! ");
theForm.departmentName.focus();
return false;
}
}
</Script>
</head>
<body>
<h1> 职业设置 </h1>
<html:form action= "/departmentSetAction.do " method= "POST " onsubmit= "return departmentSet(this) ">
<P> <TABLE border= "0 " width= "234 ">
<!--DWLayoutTable-->
<TR>
<TD width= "142 " height= "18 " align= "right " valign= "middle "> 部门名称: </TD>
<TD width= "132 " align= "left " valign= "middle ">
<html:select property= "departmentName ">
<html:options collection= "list " property= "value " labelProperty= "label " />
</html:select>
</TD>
</TR>
<TR>
<TD height= "18 " align= "right " valign= "middle "> 职位名称: </TD>
<TD align= "left " valign= "middle ">
<html:text property= "positionName " value= " "/> </TD>
</TR>
<TR>
<TD height= "24 " colspan= "2 " align= "center " valign= "middle ">
<html:submit property= "submit " value= "确定 "/> <html:reset value= "清除 " /> </TD>
</TR>
</TABLE>
</html:form>
</body>
</html>
PositionSetActionForm.java:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xsl
package com.hrmis.struts.form;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.hrmis.GetDepartmentList;
import com.hrmis.departmentBean;
/**
* MyEclipse Struts Creation date: 04-12-2007
*
* XDoclet definition:
*
* @struts.form name= "positionSetForm "
*/
public class PositionSetActionForm extends ActionForm {
// --------------------- Instance
// Variables
/** position property */
private String position;
/** department property */
private String departmentName;
// --------------------- Methods
/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the position.
*
* @return String
*/
public String getPosition() {
return position;
}
/**
* Set the position.
*
* @param position
* The position to set
*/
public void setPosition(String position) {
this.position = position;
}
/**
* Returns the department.
*
* @return String
*/
public String getDepartmentName() {
return departmentName;
}
/**
* Set the department.
*
* @param department
* The department to set
*/
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
struts-config.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN " "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<struts-config>
<data-sources />
<form-beans >
<form-bean name= "departmentSetActionForm " type= "com.hrmis.struts.form.DepartmentSetActionForm " />
<form-bean name= "positionSetActionForm " type= "com.hrmis.struts.form.PositionSetActionForm " />
</form-beans>
<global-exceptions />
<global-forwards>
<forward name= "departmentSet " path= "/departmentSet.jsp "/>
<forward name= "positionSet " path= "/positionSet.jsp "/>
</global-forwards>
<action-mappings >
<action
attribute= "departmentSetActionForm "
name= "departmentSetActionForm "
path= "/departmentSetAction "
type= "com.hrmis.struts.action.DepartmentSetAction "
validate= "false " />
<action
attribute= "positionSetActionForm "
name= "positionSetActionForm "
path= "/positionSetAction "
scope= "request "
type= "com.hrmis.struts.action.PositionSetAction "
validate= "false " />
</action-mappings>
<message-resources parameter= "com.hrmis.struts.ApplicationResources " />
</struts-config>
[解决办法]
你在页面导包没啊
看下 存了list后转发的时不时那个页面
[解决办法]
建议你看看孙维琴那本书。
[解决办法]
<html:select property= "departmentName ">
<html:options collection= "list " property= "value " labelProperty= "label " />
</html:select>
换成下面的:
<html:select property= "departmentName ">
<html:optionsCollection property= "list " label= "label " value= "value " />
</html:select>
下次不要贴这么多代码了,头都大了
[解决办法]
和我贴出的是同一个问题,但是用session解决并不是一个好办法,在等待中