读书人

struts1.x中怎么从数据库中取出数据并

发布时间: 2012-03-30 17:32:09 作者: rapoo

struts1.x中如何从数据库中取出数据并显示到JSP上
用户发出showinfo.do请求,ShowAction接收请求,调用ShowInfoBean从数据库中取出数据然后显示到JSP上,这个过程应该如何实现呢,我是struts的初学者
我的代码是这样的,可是最后还是打印不出数据库中的数据

//ShowAction.java
//action类获取用户的请求、查询数据库信息

package info.action;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import info.bean.*;
import java.io.*;
import java.util.*;
public class ShowAction extends Action
{
protected ArrayList getInfos(){
PreparedStatement prst=null;
ResultSet rs=null;
Connection con=null;
DBConnection dbcon=new DBConnection();
Info info=null;
ArrayList infos=new ArrayList();
//下面的id、job都是在登陆的时候存进session的
String id=(String)request.getSession().getAttribute("id");
String job=(String)request.getSession().getAttribute("job");
dbcon.setSql(job);
try{
con=dbcon.getConnection();
prst=con.prepareStatement(dbcon.getSql());
prst.setString(1,id);
rs=prst.executeQuery();
while(rs.next()){
info=new Info();
info.setId(rs.getString(1));
info.setName(rs.getString(2));
info.setSex(rs.getString(3));
info.setAge(rs.getInt(4));
info.setMail(rs.getString(5));
info.setPost(rs.getString(6));
info.setAdress(rs.getString(7));
info.setPhone(rs.getString(8));
infos.add(info);
}
}
catch (SQLException e)
{}finally{
dbcon.closedb(rs,prst,con);
}
return infos;

}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
return mapping.findForward("myinfo");
}
}




//Info.java

package info.vo;
public class Info
{
private String id;
private String name;
private String sex;
private int age;
private String mail;
private String post;
private String phone;
private String adress;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
}


//ShowInfo.jsp用来显示取出的数据

<%@ page language="java" contentType="text/html; charset=gb2312"
import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="../WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="../WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="../WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//Dtd HTML 4.01 transitional//EN">
<!-- 判断用户是否正常登陆-->
<logic:notPresent name="id">
<logic:forward name="login"/>
</logic:notPresent>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">


<LINK href="../css/main.css" rel=stylesheet>
</head>
<body>
<table width="80%" border="1" align="center">

<logic:iterate id="info" name="infos">
<tr>
<td>用户名</td>
<td>
<bean:write name="info" property="id"/>
</td>
</tr>
<tr>
<td>姓名</td>
<td>
<bean:write name="info" property="name"/>
</td>
</tr><tr>
<td>性别</td><td>
<bean:write name="info" property="sex"/>
</td>
</tr><tr>
<td>年龄</td><td>
<bean:write name="info" property="age"/>
</td>
</tr><tr>
<td>邮箱</td><td>
<bean:write name="info" property="mail"/>
</td>
</tr><tr>
<td>邮编</td><td>
<bean:write name="info" property="post"/>
</td>
</tr><tr>
<td>地址</td><td>
<bean:write name="info" property="adress"/>
</td>
</tr><tr>
<td>电话</td><td>
<bean:write name="info" property="phone"/>
</td>
</tr>
</logic:iterate>
</table>
</body>
</html>



[解决办法]
<logic:iterate name= 'dbList ' id= 'dbList ' type= '类型 ' scope= 'request '>
在你的action中都没有将protected ArrayList getInfos(){}查询出来的值放入request中啊。
[解决办法]
把查询结果放入到request范围 在页面通过逻辑标签来循环输出就可以了。。
[解决办法]
查询的结果放到request中去
request.setAttribute("infos",list);
通过jsp logic:iterator输出
[解决办法]

探讨

引用:

查询的结果放到request中去
request.setAttribute("infos",list);
通过jsp logic:iterator输出

试过了,可是取不出来的

[解决办法]
探讨

引用:

查询的结果放到request中去
request.setAttribute("infos",list);
通过jsp logic:iterator输出

试过了,可是取不出来的

读书人网 >J2EE开发

热点推荐