我这主要是在页面上显示数据库里所有信息,连接数据库就不写了,javabean里就2个属性,一个ID,一个name
这段代码是连接数据库取得结果然后放在集合里面填写在集合里
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.db.DB;
import com.vo.Play;
public class DAO {
private Connection conn=DB.getdb().getConn();
public List find(){
List list=new ArrayList();
String sql="select * from play";
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
Play play=new Play();
play.setId(rs.getString(1));
play.setName(rs.getString(2));
list.add(play);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return list;
}
public static void main(String[] args){
DAO dao=new DAO();
List list=new ArrayList();
list=dao.find();
System.out.println(list);
}
}
这是struts action里的execute方法
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
FindForm findForm = (FindForm) form;
DAO dao=new DAO();
List list=dao.find();
if(list.size()>0){
request.setAttribute("list",list);
return mapping.findForward("success");
}
return null;
}
这是jsp页面的代码
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<title>JSP for findForm form</title>
</head>
<body>
<logic:present name="aaa" scope="request">
<logic:iterate id="list" name="list" scope="request">
<logic:present name="play">
<bean:write name="play" property="id"/>
<bean:write name="play" property="name"/>
</logic:present>
</logic:iterate>
</logic:present>
</body>
</html>
这是struts-config.xml里的配置文件
<action-mappings >
<action
attribute="findForm"
input="/find.jsp"
name="findForm"
path="/find"
scope="request"
type="com.yourcompany.struts.action.FindAction" >
<forward name="success" path="/find.do"/>
</action>
</action-mappings>
现在问题就是打印不出东西来,望指点,在线等!