<head>
<title>JSP for pageForm form</title>
</head>
<body>
<logic:iterate id="a" name="arrayList" type="com.yourcompany.struts.action.PageMain">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
<html:link page="/index.do?action=nextPage">nextPage</html:link>
</body>
</html>
out.print(request.getAttribute("arrayList"))里面有东西,PageMain:
package com.yourcompany.struts.action;
public class PageMain
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageMain(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
24 个解决方案
#1
com.yourcompany.struts.action.PageMain这个类不在你的类路径里(或者根本没有这个累) 检查下
#2
找不到你要的类,路径错了
#3
你连<html:form>都没有呀
<html:form action="your_action" >
<logic:iterate id="a" name="formbeanName" property="arrayList">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
</html:form>
<html:form action="your_action" >
<logic:iterate id="a" name="formbeanName" property="arrayList">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
</html:form>
#4
package com.yourcompany.struts.action;
public class PageMain
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageMain(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
public class PageMain
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageMain(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
#5
你没指定到对应的action,那么它也找不到你对应的form。
当然就无法取出form里面的属性了。
解决的办法有 1: 按Octer(米粒)所说,加form指定action。
2: 先执行action(可通过URL链接),将你的form放入scope当中,比如request,session。
当然就无法取出form里面的属性了。
解决的办法有 1: 按Octer(米粒)所说,加form指定action。
2: 先执行action(可通过URL链接),将你的form放入scope当中,比如request,session。
#6
PageMain这个类要继承ActionForm
#7
按照哥们们的说法做了,还是出现“javax.servlet.jsp.JspException: Cannot find bean a in any scope”
index.jsp:
<html:form action="/index">
<head>
<title>JSP for pageForm form</title>
</head>
<body>
<logic:iterate id="a" name="arrayList" type="com.yourcompany.struts.action.PageMain">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
<html:link page="/index.do?action=nextPage">nextPage</html:link>
</body>
</html:form>
struts-config.xml:
<struts-config>
<data-sources />
<form-beans >
<form-bean name="Form" type="com.yourcompany.struts.action.PageMain" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/index.jsp"
path="/index"
scope="request"
name="Form"
type="com.yourcompany.struts.action.PageAction">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
PageMain.java
public class PageMain extends ActionForm
index.jsp:
<html:form action="/index">
<head>
<title>JSP for pageForm form</title>
</head>
<body>
<logic:iterate id="a" name="arrayList" type="com.yourcompany.struts.action.PageMain">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
<html:link page="/index.do?action=nextPage">nextPage</html:link>
</body>
</html:form>
struts-config.xml:
<struts-config>
<data-sources />
<form-beans >
<form-bean name="Form" type="com.yourcompany.struts.action.PageMain" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/index.jsp"
path="/index"
scope="request"
name="Form"
type="com.yourcompany.struts.action.PageAction">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
PageMain.java
public class PageMain extends ActionForm
#8
再次强调:请确保你的PageMain是在request或者是session域当中。
要么是在action类的form当中赋值,要么是手动往request或者session中加。已经说的很明白了。
或者将action代码贴出来。
要么是在action类的form当中赋值,要么是手动往request或者session中加。已经说的很明白了。
或者将action代码贴出来。
#9
PageMain不继承ActionForm,再写一个PageForm
class PageForm extends ActionForm {
List arrayList = new ArrayList();
get and set method;
}
struts-config.xml
<form-bean name="Form" type="com.yourcompany.struts.action.PageForm" />
jsp
<logic:iterate id="a" name="Form" property="arrayList ">
或者直接将arrayList 放到request或者session中
class PageForm extends ActionForm {
List arrayList = new ArrayList();
get and set method;
}
struts-config.xml
<form-bean name="Form" type="com.yourcompany.struts.action.PageForm" />
jsp
<logic:iterate id="a" name="Form" property="arrayList ">
或者直接将arrayList 放到request或者session中
#10
你没有把arrayList 放到request或者session中,请确认一下。
#11
谢谢哥们们的批评,以前的错误没了,出现
java.lang.ClassCastException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
PageAction.java:
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
java.lang.ClassCastException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
PageAction.java:
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
#12
已经把我说糊渡了,我现在的arrayList里有值,out.print(request.getAttribute("arrayList"));可以输出,我想用STRUTS标签把值输出。
PageAction:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
PageForm:
package com.yourcompany.struts.action;
import org.apache.struts.action.ActionForm;
public class PageForm extends ActionForm
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageForm(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
PageAction:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
PageForm:
package com.yourcompany.struts.action;
import org.apache.struts.action.ActionForm;
public class PageForm extends ActionForm
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageForm(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
#13
首先说一下,标签 logic:iterate 和 bean:write 跟标签 html:form 没有任何直接关联的,都可以单独使用。所以肯定不是这个问题。
-------------------------
楼主提到:
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
-------------------------
所以要重点检查你的arrayList里确实放的就是PageMain的实例。
能把你的out.print(request.getAttribute("arrayList"))输出贴出来吗?
-------------------------
楼主提到:
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
-------------------------
所以要重点检查你的arrayList里确实放的就是PageMain的实例。
能把你的out.print(request.getAttribute("arrayList"))输出贴出来吗?
#14
顶楼上的! 不一定要用form
#15
出现ClassCastException,说明是类型转换错误,检查一下你的arraylist里面的到底是什么类型。
另外,你先不用iterate:logic,你先用
<%
List list=(List)request.getAttribute("arraylist");
%>
然后用Iterator依次读出,看结果是否对,同时顺便验证一下arraylist里的数据类型。如果对的话,然后再讨论标签的问题,否则,没有任何意义。
另外,你先不用iterate:logic,你先用
<%
List list=(List)request.getAttribute("arraylist");
%>
然后用Iterator依次读出,看结果是否对,同时顺便验证一下arraylist里的数据类型。如果对的话,然后再讨论标签的问题,否则,没有任何意义。
#16
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
while(it.hasNext())
{
out.println(it.next().toString());
}
输出:
com.yourcompany.struts.action.PageForm@8a4156 com.yourcompany.struts.action.PageForm@c787d7 com.yourcompany.struts.action.PageForm@1a76e19 com.yourcompany.struts.action.PageForm@12d49e7 com.yourcompany.struts.action.PageForm@4f14b0 com.yourcompany.struts.action.PageForm@1d74bb1
Iterator it= arr.iterator();
while(it.hasNext())
{
out.println(it.next().toString());
}
输出:
com.yourcompany.struts.action.PageForm@8a4156 com.yourcompany.struts.action.PageForm@c787d7 com.yourcompany.struts.action.PageForm@1a76e19 com.yourcompany.struts.action.PageForm@12d49e7 com.yourcompany.struts.action.PageForm@4f14b0 com.yourcompany.struts.action.PageForm@1d74bb1
#17
晕,这样打出的信息有什么用?(至少我看不懂?)
你应该把it.next()强制转换成PageMain 或者PageForm? 然后看看是否可以打出你要的name,psw;如果此转换过程出错,说明你arrayList里面存放的根本不是PageMain的对象,当然出现你所说的错误
你应该把it.next()强制转换成PageMain 或者PageForm? 然后看看是否可以打出你要的name,psw;如果此转换过程出错,说明你arrayList里面存放的根本不是PageMain的对象,当然出现你所说的错误
#18
这说明你的arraylist里存的是pageform对象啊,你用
while(it.hasNext())
{
PageForm pf=(PageForm)it.next();
System.out.println(pf.getXX());
...
}
看看能否打印出结果来。
然后在看看,你标签里取的那个对象,到底是pageform还是pagemain.
while(it.hasNext())
{
PageForm pf=(PageForm)it.next();
System.out.println(pf.getXX());
...
}
看看能否打印出结果来。
然后在看看,你标签里取的那个对象,到底是pageform还是pagemain.
#19
你的PageForm跟PageMain是什么关系啊?如果PageMain不是PageForm的父类,当然会出错了,从你打印出来的信息就可以很清楚判断的了。
#20
按照上面的的朋友我把PageMain改成了PageForm,现在没有PageMain了。我输出结果:
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
}
又出现错误,我的request.getAttribute("arrayList")里面有值呀?!
java.lang.NullPointerException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.*;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
//request.setAttribute("arrayList",arrayList);
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
//out.println(it.next());
}
//System.out.println(fm.getName());
request.setAttribute("a",fm.getName());
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
}
又出现错误,我的request.getAttribute("arrayList")里面有值呀?!
java.lang.NullPointerException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.*;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
//request.setAttribute("arrayList",arrayList);
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
//out.println(it.next());
}
//System.out.println(fm.getName());
request.setAttribute("a",fm.getName());
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
#21
晕倒,楼主还用ArrayList arr =(ArrayList)request.getAttribute("arrayList");这个在action中这么取值??这样不是空指针才怪,恩,看来楼主得好好补补基础知识才行。把这个注释掉就成了,明显的程序基本错误哦。
#22
另外,把前一句注释打开
#23
的确应该好好补补了。
#24
解决了,STRUTS自动给我生成了一个PageForm,里面什么也没有。
#1
com.yourcompany.struts.action.PageMain这个类不在你的类路径里(或者根本没有这个累) 检查下
#2
找不到你要的类,路径错了
#3
你连<html:form>都没有呀
<html:form action="your_action" >
<logic:iterate id="a" name="formbeanName" property="arrayList">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
</html:form>
<html:form action="your_action" >
<logic:iterate id="a" name="formbeanName" property="arrayList">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
</html:form>
#4
package com.yourcompany.struts.action;
public class PageMain
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageMain(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
public class PageMain
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageMain(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
#5
你没指定到对应的action,那么它也找不到你对应的form。
当然就无法取出form里面的属性了。
解决的办法有 1: 按Octer(米粒)所说,加form指定action。
2: 先执行action(可通过URL链接),将你的form放入scope当中,比如request,session。
当然就无法取出form里面的属性了。
解决的办法有 1: 按Octer(米粒)所说,加form指定action。
2: 先执行action(可通过URL链接),将你的form放入scope当中,比如request,session。
#6
PageMain这个类要继承ActionForm
#7
按照哥们们的说法做了,还是出现“javax.servlet.jsp.JspException: Cannot find bean a in any scope”
index.jsp:
<html:form action="/index">
<head>
<title>JSP for pageForm form</title>
</head>
<body>
<logic:iterate id="a" name="arrayList" type="com.yourcompany.struts.action.PageMain">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
<html:link page="/index.do?action=nextPage">nextPage</html:link>
</body>
</html:form>
struts-config.xml:
<struts-config>
<data-sources />
<form-beans >
<form-bean name="Form" type="com.yourcompany.struts.action.PageMain" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/index.jsp"
path="/index"
scope="request"
name="Form"
type="com.yourcompany.struts.action.PageAction">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
PageMain.java
public class PageMain extends ActionForm
index.jsp:
<html:form action="/index">
<head>
<title>JSP for pageForm form</title>
</head>
<body>
<logic:iterate id="a" name="arrayList" type="com.yourcompany.struts.action.PageMain">
<tr>
<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>
</tr>
</logic:iterate>
<html:link page="/index.do?action=nextPage">nextPage</html:link>
</body>
</html:form>
struts-config.xml:
<struts-config>
<data-sources />
<form-beans >
<form-bean name="Form" type="com.yourcompany.struts.action.PageMain" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
input="/index.jsp"
path="/index"
scope="request"
name="Form"
type="com.yourcompany.struts.action.PageAction">
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
PageMain.java
public class PageMain extends ActionForm
#8
再次强调:请确保你的PageMain是在request或者是session域当中。
要么是在action类的form当中赋值,要么是手动往request或者session中加。已经说的很明白了。
或者将action代码贴出来。
要么是在action类的form当中赋值,要么是手动往request或者session中加。已经说的很明白了。
或者将action代码贴出来。
#9
PageMain不继承ActionForm,再写一个PageForm
class PageForm extends ActionForm {
List arrayList = new ArrayList();
get and set method;
}
struts-config.xml
<form-bean name="Form" type="com.yourcompany.struts.action.PageForm" />
jsp
<logic:iterate id="a" name="Form" property="arrayList ">
或者直接将arrayList 放到request或者session中
class PageForm extends ActionForm {
List arrayList = new ArrayList();
get and set method;
}
struts-config.xml
<form-bean name="Form" type="com.yourcompany.struts.action.PageForm" />
jsp
<logic:iterate id="a" name="Form" property="arrayList ">
或者直接将arrayList 放到request或者session中
#10
你没有把arrayList 放到request或者session中,请确认一下。
#11
谢谢哥们们的批评,以前的错误没了,出现
java.lang.ClassCastException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
PageAction.java:
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
java.lang.ClassCastException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
PageAction.java:
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
#12
已经把我说糊渡了,我现在的arrayList里有值,out.print(request.getAttribute("arrayList"));可以输出,我想用STRUTS标签把值输出。
PageAction:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
PageForm:
package com.yourcompany.struts.action;
import org.apache.struts.action.ActionForm;
public class PageForm extends ActionForm
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageForm(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
PageAction:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.ArrayList;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
request.setAttribute("arrayList",arrayList);
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
PageForm:
package com.yourcompany.struts.action;
import org.apache.struts.action.ActionForm;
public class PageForm extends ActionForm
{
private String name="";
private String psw="";
public void setName(String name)
{
this.name=name;
}
public void setPsw(String psw)
{
this.psw=psw;
}
public String getName()
{
return name;
}
public String getPsw()
{
return psw;
}
public void PageForm(String name,String psw)
{
this.name=name;
this.psw=psw;
}
}
#13
首先说一下,标签 logic:iterate 和 bean:write 跟标签 html:form 没有任何直接关联的,都可以单独使用。所以肯定不是这个问题。
-------------------------
楼主提到:
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
-------------------------
所以要重点检查你的arrayList里确实放的就是PageMain的实例。
能把你的out.print(request.getAttribute("arrayList"))输出贴出来吗?
-------------------------
楼主提到:
在这里,我将<td><bean:write name="a" property="psw"/></td>
<td><bean:write name="a" property="name"/></td>去掉,不报错,加上就报。
-------------------------
所以要重点检查你的arrayList里确实放的就是PageMain的实例。
能把你的out.print(request.getAttribute("arrayList"))输出贴出来吗?
#14
顶楼上的! 不一定要用form
#15
出现ClassCastException,说明是类型转换错误,检查一下你的arraylist里面的到底是什么类型。
另外,你先不用iterate:logic,你先用
<%
List list=(List)request.getAttribute("arraylist");
%>
然后用Iterator依次读出,看结果是否对,同时顺便验证一下arraylist里的数据类型。如果对的话,然后再讨论标签的问题,否则,没有任何意义。
另外,你先不用iterate:logic,你先用
<%
List list=(List)request.getAttribute("arraylist");
%>
然后用Iterator依次读出,看结果是否对,同时顺便验证一下arraylist里的数据类型。如果对的话,然后再讨论标签的问题,否则,没有任何意义。
#16
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
while(it.hasNext())
{
out.println(it.next().toString());
}
输出:
com.yourcompany.struts.action.PageForm@8a4156 com.yourcompany.struts.action.PageForm@c787d7 com.yourcompany.struts.action.PageForm@1a76e19 com.yourcompany.struts.action.PageForm@12d49e7 com.yourcompany.struts.action.PageForm@4f14b0 com.yourcompany.struts.action.PageForm@1d74bb1
Iterator it= arr.iterator();
while(it.hasNext())
{
out.println(it.next().toString());
}
输出:
com.yourcompany.struts.action.PageForm@8a4156 com.yourcompany.struts.action.PageForm@c787d7 com.yourcompany.struts.action.PageForm@1a76e19 com.yourcompany.struts.action.PageForm@12d49e7 com.yourcompany.struts.action.PageForm@4f14b0 com.yourcompany.struts.action.PageForm@1d74bb1
#17
晕,这样打出的信息有什么用?(至少我看不懂?)
你应该把it.next()强制转换成PageMain 或者PageForm? 然后看看是否可以打出你要的name,psw;如果此转换过程出错,说明你arrayList里面存放的根本不是PageMain的对象,当然出现你所说的错误
你应该把it.next()强制转换成PageMain 或者PageForm? 然后看看是否可以打出你要的name,psw;如果此转换过程出错,说明你arrayList里面存放的根本不是PageMain的对象,当然出现你所说的错误
#18
这说明你的arraylist里存的是pageform对象啊,你用
while(it.hasNext())
{
PageForm pf=(PageForm)it.next();
System.out.println(pf.getXX());
...
}
看看能否打印出结果来。
然后在看看,你标签里取的那个对象,到底是pageform还是pagemain.
while(it.hasNext())
{
PageForm pf=(PageForm)it.next();
System.out.println(pf.getXX());
...
}
看看能否打印出结果来。
然后在看看,你标签里取的那个对象,到底是pageform还是pagemain.
#19
你的PageForm跟PageMain是什么关系啊?如果PageMain不是PageForm的父类,当然会出错了,从你打印出来的信息就可以很清楚判断的了。
#20
按照上面的的朋友我把PageMain改成了PageForm,现在没有PageMain了。我输出结果:
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
}
又出现错误,我的request.getAttribute("arrayList")里面有值呀?!
java.lang.NullPointerException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.*;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
//request.setAttribute("arrayList",arrayList);
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
//out.println(it.next());
}
//System.out.println(fm.getName());
request.setAttribute("a",fm.getName());
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
}
又出现错误,我的request.getAttribute("arrayList")里面有值呀?!
java.lang.NullPointerException
com.yourcompany.struts.action.PageAction.execute(PageAction.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.*;
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 com.yourcompany.struts.form.PageForm;
public class PageAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageForm pageForm = (PageForm) form;
// TODO Auto-generated method stub
String action;
action=request.getParameter("action");
//if(action=="nextPage")
//{
PageController pagecontroller=new PageController();
pagecontroller.Controller();
pagecontroller.intPage(action);
ArrayList arrayList = new ArrayList();
arrayList=pagecontroller.start();
//}
//request.setAttribute("arrayList",arrayList);
ArrayList arr =(ArrayList)request.getAttribute("arrayList");
Iterator it= arr.iterator();
com.yourcompany.struts.action.PageForm fm=null;
while(it.hasNext())
{
fm=(com.yourcompany.struts.action.PageForm)it.next();
//out.println(it.next());
}
//System.out.println(fm.getName());
request.setAttribute("a",fm.getName());
//HttpServletRequest request=null;
//request.setAttribute("arrayList1","2");
return (mapping.findForward("success"));
}
}
#21
晕倒,楼主还用ArrayList arr =(ArrayList)request.getAttribute("arrayList");这个在action中这么取值??这样不是空指针才怪,恩,看来楼主得好好补补基础知识才行。把这个注释掉就成了,明显的程序基本错误哦。
#22
另外,把前一句注释打开
#23
的确应该好好补补了。
#24
解决了,STRUTS自动给我生成了一个PageForm,里面什么也没有。