我下载了这样一个例子,并学习他做一个查询并显示列表
不过很多地方不明白,希望各位指教
-------------华丽的分割线----------------------
第一部分是servlet :SelectServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
DataBean db = new DataBean();
String selectValue="";
if (request.getParameter("selectValue") != null)
{
selectValue = request.getParameter("selectValue");
int page;
if (request.getParameter("page") != null)
{
page = Integer.parseInt(request.getParameter("page"));
}
else
{
page = 1;
}
if (request.getParameter("action") != null)
{
if (request.getParameter("action").equals("frist"))
{
page = 1;
}
else if (request.getParameter("action").equals("last"))
{
page = db.PAGECOUNT;
}
else if (request.getParameter("action").equals("back"))
{
page -= 1;
}
else if (request.getParameter("action").equals("frist"))
{
page += 1;
}
}
ArrayList list = db.selectCDBean(selectValue, page, 10);
request.getSession().removeAttribute("selectList");
request.getSession().setAttribute("selectList",list);
int pagecount=db.PAGECOUNT;
request.setAttribute("selectValue",selectValue);
request.setAttribute("page",page);
request.setAttribute("pagecount",pagecount);
}
RequestDispatcher rd=request.getRequestDispatcher("select.jsp?select="+selectValue);
rd.forward(request,response);
}
----------------还是华丽的分割线----------------------
第二部分是Bean: DataBean
public ArrayList selectCDBean(String selectValue, int page, int count) //查询分页
{
ArrayList list = new ArrayList();
if (!checkParameter(selectValue))
{
selectValue = "";
}
try
{
proc = conn.prepareCall("{call proc_page(?,?,?,?)}");
proc.setInt(1, page);
proc.setInt(2, count);
proc.setString(3, selectValue);
proc.registerOutParameter(4, Types.INTEGER); //OUTPUT参数 返回结构共多少页
res = proc.executeQuery(); //接收存储过程的结果集
while (res.next()) //提取结果集的每条记录
{
CDBean cb = new CDBean();
cb.setCdAlbum(res.getString("CDalbum"));
cb.setCdCompany(res.getString("CDcompany"));
cb.setCdName(res.getString("CDname"));
cb.setCdId(res.getLong("CDid"));
cb.setCdType(getCDType(res.getInt("CDtypeId")));
list.add(cb);
}
PAGECOUNT = proc.getInt(4);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return list;
}
-----------------又是华丽的分割线---------------
之后我按照他的方法自己做了个类似的
servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
int parentid;
if (request.getParameter("parentid")==null)
{
parentid=0;
}
else
{
parentid=Integer.parseInt(request.getParameter("parentid"));
ClassManageBean mb = new ClassManageBean();
ArrayList list = mb.classList(parentid);
request.getSession().setAttribute("classname", list);
}
response.sendRedirect("admin/classManage.jsp");
}
bean:
public ArrayList classList(int parentid)
{
conn= new DBConn().getConn();
ArrayList list = new ArrayList();
String sql = "select * from news_type where parentid=? order by classid";
try
{
prepar = conn.prepareStatement(sql);
prepar.setInt(1, parentid);
res = prepar.executeQuery();
while (res.next())
{
News_Type nt = new News_Type();
nt.setClassid(res.getInt("classid"));
nt.setClassname(res.getString("classname"));
nt.setDepth(res.getInt("Depth"));
nt.setParentid(res.getInt("parentid"));
nt.setType(res.getString("Type"));
list.add(nt);
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
return list;
}
}
---------------这是最关键的最华丽的分割线--------------------
现在是问题了,例子是用jstl做的,我还不会,请问如何在页面把这个list的值用列表表现出来
我是第一次做项目,问题很初级不要笑我
8 个解决方案
你的list用session或者用,request.Attribute("name")传到JSP
jsp上可以用
<%
ArrayList list = (ArrayList)request.getAttribute("name");
for(int i=0;i<list.size();i++){
CDBean cb = new CDBean();
cb=(CDBean)list.get(i);
%>
<tr>
<td>
<%=cb.get****%>//这里是你的BEAN里面的GET方法,你要使用这个BEAN可能要在JSP的头文件上加相应的IMPORT
</td>
</tr>
<%
}
%>
这是大概,如果你会用标签的话可能更快,不用这么复杂,JSTL不会,
为什么不用jstl?jstl比较方便的啊,
一个<c:forEach>就搞定了整个的循环.
例:
<c:forEach var="p" items="${requestScope.你写到request中的list的集合}" varStatus="status">
${p.属性}
</c:forEach>
百度下c:forEach就知道了,挺方便的.
我就是不会啊!还没学过jstl
一会儿我也试试
另外刚才CDBean cb = new CDBean(); 这一段好像不能直接用
还要 用<jsp:useBean>加入bean吗?
把你的CDBean import到jsp的头里面 <%@page import="..."%>
为什么我取出的list是个空值啊?
开始数据库为空,我加了数据还是空。
执行后出错的显示:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
JSP FileName:null
Java FileName:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/work/Catalina/localhost/school//org/apache/jsp/admin\classManage_jsp.java
An error occurred at line: 171 in the generated java file
Syntax error, insert "}" to complete Block
JSP FileName:null
Java FileName:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/work/Catalina/localhost/school//org/apache/jsp/admin\classManage_jsp.java
An error occurred at line: 171 in the generated java file
Syntax error, insert "while ( Expression ) ;" to complete BlockStatements
JSP FileName:/admin/classManage.jsp
Java FileName:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/work/Catalina/localhost/school//org/apache/jsp/admin\classManage_jsp.java
An error occurred at line: 94 in the jsp file: /admin/classManage.jsp
Syntax error, insert "Finally" to complete TryStatement
91:
92: </div>
93: <!-- </c:forEach> -->
94: <%} %>
95:
96: <table width="96%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
97: <tr>
JSP FileName:null
Java FileName:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/work/Catalina/localhost/school//org/apache/jsp/admin\classManage_jsp.java
An error occurred at line: 240 in the generated java file
Syntax error on tokens, delete these tokens
JSP FileName:null
Java FileName:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/work/Catalina/localhost/school//org/apache/jsp/admin\classManage_jsp.java
An error occurred at line: 247 in the generated java file
Syntax error on token "finally", delete this token
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:98)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.
用jstl的标签不报错不过什么也没显示,是不是list传值就没传过来?