指出2点: 1. 之所以出现Cannot find bean a in any scope 的问题是因为没有关于bean:define关于a的定义就直接用了,只要加如下形式的定义<bean:define id="a" property="xxx" name="xxxxxxForm"/>就可解决该问题 2.楼主的代码存在一个潜在的问题,你没有判断logic:iterate的数据源是否为空, 如果一旦指定数据源为空时就会报会出现Tag can't set to null的错误 现给出一段我以前写过的代码,希望能对楼主有所帮助(该工程已经实施了一年多了,代码应该是没有问题的,不过老早写的代码可能不是很规范,^_^)
小弟菜鸟,还得请各位帮助啊,怎么没人回答呢?
加上httpServletRequest.setAttribute("stuffshowActionForm",StuffActionForm);jsp页面也相应的改一下
一、bean(showStuff)package hongmei.insert;import hongmei.database.DBConn;
import java.sql.*;
import java.util.*;public class showStuff{
private String id;
private String stuffName;
private String re;
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setStuffName(String stuffName){ this.stuffName=stuffName;
}
public String getStuffName(){
return stuffName;
}
public void setRe(String re){
this.re=re;
}
public String getRe(){
return re;
}
public showStuff() {
}
public showStuff(String id,String stuffName,String re) throws Exception
{
this.id=id;
this.stuffName=stuffName;
this.re=re;
} public Collection getAllstuffshow() throws Exception
{
showStuff s=null;//modi
Collection v=new ArrayList();//modi
DBConn conn=new DBConn();
String sql="select ID,Stuff_Name,Re from StuffName_Table";
ResultSet rsstuff = conn.executeQuery(sql);
while(rsstuff.next()){
s=new showstuff();//modi
s.id=Integer.toString(rsstuff.getInt("ID"));
s.stuffName=rsstuff.getString("Stuff_Name");
s.re=rsstuff.getString("Re");
v.add(s);
System.out.println(s.id +" "+s.stuffName+" "+s.re);
}
conn.finalize(); return v;
}
}
一、数据BEAN(StuffName)
package hongmei.insert;public class StuffName {
public StuffName() {
}
private String id;
private String stuffName;
private String re; public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setStuffName(String stuffName){ this.stuffName=stuffName;
}
public String getStuffName(){
return stuffName;
}
public void setRe(String re){
this.re=re;
}
public String getRe(){
return re;
}
}二、查询BEAN(showStuff)
package hongmei.insert;import hongmei.database.DBConn;
import hongmei.insert.StuffName;
import java.sql.*;
import java.util.*;public class showStuff{
StuffName stuffname=null;
Collection v;
public showStuff() {
v=new Vector();
}
public Collection getAllstuffshow() throws Exception
{
stuffname=new StuffName();
DBConn conn=new DBConn();
String sql="select ID,Stuff_Name,Re from StuffName_Table";
ResultSet rsstuff = conn.executeQuery(sql);
while(rsstuff.next()){
stuffname=new StuffName();
stuffname.setId(Integer.toString(rsstuff.getInt("ID")));
stuffname.setStuffName(rsstuff.getString("Stuff_Name"));
stuffname.setRe(rsstuff.getString("Re"));
v.add(stuffname);
System.out.println(stuffname.getId() +" "+stuffname.getStuffName()+" "+stuffname.getRe());
}
conn.finalize(); return v;
}
public static void main(String args[]){
showStuff b=new showStuff();
Collection k=new ArrayList();
try{
k=b.getAllstuffshow();
if(k.isEmpty()){
System.out.println("空的空的空的");
}
else{
System.out.println("实的实的实的");//这个BEAN是得到内容的
}
}
catch(Exception e){
System.out.print(e.getMessage());
}
}
}
三、Action(stuffshowAction)
package hongmei;import org.apache.struts.action.*;
import javax.servlet.http.*;
import hongmei.insert.showStuff;
import java.util.*;
public class stuffshowAction extends Action {
public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
//throw new java.lang.UnsupportedOperationException("Method perform() not yet implemented.");
//stuffshowActionForm StuffActionForm=(stuffshowActionForm)actionForm;
//ArrayList v;
Collection a;
showStuff showstuff;
try{
showstuff=new showStuff();
// a=new ArrayList();
a=showstuff.getAllstuffshow();
// StuffActionForm.setStufflist(a);
httpServletRequest.setAttribute("a",a); }
catch(Exception e){
System.out.println(e.getMessage());
}
return new ActionForward("Success");
}
}
四、jsp(stuffshow.jsp)
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<link rel="stylesheet" href="images/style.css" type="text/css">
<link rel="stylesheet" href="images/ebb.css" type="text/css">
<html:html>
<head>
<title>
<bean:message key="hongmei.title"/>
</title>
</head>
<body>
<html:form action="/stuffshowAction.do" method="POST">
<table width="80%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<th width="18%" align="center" scope="col"><bean:message key="hongmei.del"/></th>
<th width="33%" align="center" scope="col"><bean:message key="hongmei.name"/></th>
<th width="49%" align="center" scope="col"><bean:message key="hongmei.re"/></th>
</tr>
<logic:iterate id="stuffName" name="a">
<tr>
<td width="18%" align="center" scope="col"><bean:write name="stuffName"
property="id"/></td>
<td width="33%" align="center" scope="col"><bean:write name="stuffName"
property="stuffName"/></td>
<td width="49%" align="center" scope="col"><bean:write name="stuffName"
property="re"/></td>
</tr>
</logic:iterate>
</table>
</html:form>
</body>
</html:html>
1. 之所以出现Cannot find bean a in any scope 的问题是因为没有关于bean:define关于a的定义就直接用了,只要加如下形式的定义<bean:define id="a" property="xxx" name="xxxxxxForm"/>就可解决该问题
2.楼主的代码存在一个潜在的问题,你没有判断logic:iterate的数据源是否为空, 如果一旦指定数据源为空时就会报会出现Tag can't set to null的错误
现给出一段我以前写过的代码,希望能对楼主有所帮助(该工程已经实施了一年多了,代码应该是没有问题的,不过老早写的代码可能不是很规范,^_^)
<%--注意:要对数据源判断,否则会出现Tag can't set to null错误--%>
<logic:notEqual property="dataSources" name="prjMngForm" value="">
<bean:define id="arr" property="dataSources" name="prjMngForm"/>
<%
int no=0;
%>
<logic:iterate id="prjInfoID" offset="<%=String.valueOf(no)%>" name="arr">
<a target="_parent" href="prjMngAction.do?prjCode=<bean:write name="prjInfoID" property="prjCode"/>">
<tr onmouseover="this.style.backgroundColor='#CEE0F0';this.style.cursor='hand';this.title='点击查看详细信息';" onmouseout="this.style.backgroundColor='';" <%if (no%2==0) {%> bgcolor="white" <%;} else {%> bgcolor="#f7f7ef"<%;}%>>
<td height="14" valign="middle"> <div align="center"><%=no+1%></div></td>
<td><bean:write name="prjInfoID" property="prjVoltageLevel"/></td>
<td><bean:write name="prjInfoID" property="prjName"/></td>
<td><bean:write name="prjInfoID" property="prjMainCapacity"/></td>
<td><bean:write name="prjInfoID" property="lineLength"/></td>
<logic:equal name="prjInfoID" property="PZGS" value="0.0">
<td align="right"><font color="#0033FF"><bean:write name="prjInfoID" property="CSGS"/></font></td>
</logic:equal>
<logic:notEqual name="prjInfoID" property="PZGS" value="0.0">
<td align="right"><bean:write name="prjInfoID" property="PZGS"/></td>
</logic:notEqual>
<td align="right"><bean:write name="prjInfoID" property="prjRunPlan"/></td>
<%no=no+1;%>
</tr>
</a>
</logic:iterate>
</logic:notEqual>
------
加上httpServletRequest.setAttribute("stuffshowActionForm",StuffActionForm);
1.在Bean中s对象没有构造函数.
2.v没有初始化getAllstuffshow类应该改为:
所以你的 public Collection getAllstuffshow() throws Exception
{
DBConn conn=new DBConn();
String sql="select ID,Stuff_Name,Re from StuffName_Table";
ResultSet rsstuff = conn.executeQuery(sql);
v=new ArrayList(); //初始化对象 while(rsstuff.next()){
s=new showStuff(); //每条记录作为一个showStuff对象,进行构造函数 s.id=Integer.toString(rsstuff.getInt("ID"));
s.stuffName=rsstuff.getString("Stuff_Name");
s.re=rsstuff.getString("Re");
v.add(s);
System.out.println(s.id +" "+s.stuffName+" "+s.re);
}
conn.finalize(); return v;
}
其它的地方应该不用改了,不过boneeater(狗狗)的意见很好,最好在jsp中取值的时候先看看是不是为空,如果你不做判断的化,如果结果是空,程式就会出现错误.在jsp中改成:
<!--<html:form action="/stuffshowAction.do" method="POST">-->
<table width="80%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<th width="18%" align="center" scope="col"><bean:message key="hongmei.del"/></th>
<th width="33%" align="center" scope="col"><bean:message key="hongmei.name"/></th>
<th width="49%" align="center" scope="col"><bean:message key="hongmei.re"/></th>
</tr>
<logic:notEmpty name="stuffshowActionForm" property="stufflist"> //判断当值不为空时在执行 <logic:iterate id="stufflist" name="stuffshowActionForm" property="stufflist"
type="hongmei.insert.showStuff">
<tr>
<td width="18%" align="center" scope="col"><bean:write name="stufflist"
property="id"/></td>
<td width="33%" align="center" scope="col"><bean:write name="stufflist"
property="stuffName"/></td>
<td width="49%" align="center" scope="col"><bean:write name="stufflist"
property="re"/></td>
</tr>
</logic:iterate>
</logic:notEmpty> //结束标签
</table>
<!--</html:form>-->
</body>
</html:html>