我的测试代码:servlet:
public class Test extends HttpServlet {
.....
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { List<Vo> alist = new ArrayList<Vo>();
alist.add(new Vo("a","1"));
alist.add(new Vo("b","2"));
alist.add(new Vo("c","3"));
request.setAttribute("indexList", alist);
request.getRequestDispatcher("../index.jsp").forward(request, response);
}
.....
}
VO:
public class Vo {
private String cName;
private String demo;
public String getCName() {
return cName;
}
public void setCName(String name) {
cName = name;
}
public String getDemo() {
return demo;
}
public void setDemo(String demo) {
this.demo = demo;
}
public Vo(String name, String demo) {
super();
cName = name;
this.demo = demo;
}
}html:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
${indexList[0].cName }<br>
</body>
</html> 奇怪的问题是为什么${indexList[0].cName }会报错提示如下,而${indexList[0].demo }显示正常,不明白!!请高手帮帮忙!!!!
javax.servlet.ServletException: Unable to find a value for "cName" in object of class "Vo" using operator "."
public class Test extends HttpServlet {
.....
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { List<Vo> alist = new ArrayList<Vo>();
alist.add(new Vo("a","1"));
alist.add(new Vo("b","2"));
alist.add(new Vo("c","3"));
request.setAttribute("indexList", alist);
request.getRequestDispatcher("../index.jsp").forward(request, response);
}
.....
}
VO:
public class Vo {
private String cName;
private String demo;
public String getCName() {
return cName;
}
public void setCName(String name) {
cName = name;
}
public String getDemo() {
return demo;
}
public void setDemo(String demo) {
this.demo = demo;
}
public Vo(String name, String demo) {
super();
cName = name;
this.demo = demo;
}
}html:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
${indexList[0].cName }<br>
</body>
</html> 奇怪的问题是为什么${indexList[0].cName }会报错提示如下,而${indexList[0].demo }显示正常,不明白!!请高手帮帮忙!!!!
javax.servlet.ServletException: Unable to find a value for "cName" in object of class "Vo" using operator "."
楼主【kaz33】截止到2008-07-29 21:54:20的历史汇总数据(不包括此帖):
发帖的总数量:3 发帖的总分数:70 每贴平均分数:23
回帖的总数量:19 得分贴总数量:6 回帖的得分率:31%
结贴的总数量:3 结贴的总分数:70
无满意结贴数:1 无满意结贴分:20
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:33.33 % 无满意结分率:28.57 %
敬礼!
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=kaz33
使用
indexList[0].getCName() 试一下
你就用indexList["cName"]试下
也好像是private 属性的关系 以前用的时候没注意...
你的cname 和其他不同的地方又2个地方`
第一个是 只cname 用public
第二个是 构造方法里 只cname 没又this.不过这好像并不是原因所在``
关注中....
这个地方我也试了很久啊!但总是有异常,搞不清什么意思啊!
但有个解决的方法:你别用构造函数传那个cName,
也用set属性接受传够来的参数,这样就可以!我试了!