小弟做了个表的映射,初始化后对ArrayList进行强制转化.可是传到页面上后只显示一行数据(将表中最后一行数据重复),其他都丢失了?以下是代码,小弟是新手请多多包涵;在线等!!
是否还有其他的方法?
表映射的代码:
public class Content {
private String name;
private String content;
private String time;
public String getname(){
return name;
}
public void setname(String name){
this.name = name;
}
public String getcontent(){
return content;
}
public void setcontent(String content){
this.content = content;
}
public String gettime(){
return time;
}
public void settime(Date time){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String timeString = formatter.format(time);
this.time = timeString;
}这是从数据库中取数并存到ArrayList中
public String execute() throws Exception {
DataConnection da = new DataConnection();
int i = da.OpenConn();
Content cont = new Content();
ResultSet rs = null;// here ,it is only used to identify wether the password and name are correct
String sql = "select * from content";
System.out.println(i);
rs = da.SqlExecute(sql);
int j = 1;//here, i have to pay attention to 'Column Index out of range, 0 < 1.'
int k = 0;
while(rs.next())
{
cont.setname(rs.getString("name"));
cont.settime(rs.getDate("time"));
//here ,it could happen that the data may make collision just pay attention to it .this is a very important method
cont.setcontent(rs.getString("content"));
System.out.println("this is the current time "+cont.gettime());
arr.add(cont);
cont=(Content)arr.get(k);
System.out.println("this is"+cont.getcontent());
k++;
}
System.out.println("the number is " + k);
System.out.println("the number of the rs is" + arr.size());
da.closeConn();
if(arr.size()>0)
{
HttpServletRequest Request = ServletActionContext.getRequest();
System.out.println("check the number"+arr.size());
System.out.print("success");
Request.setAttribute("array", arr);
return SUCCESS;
} else {
return ERROR;
}
}
以下是jsp页面上的代码:
<s:form action="Show">
<%ArrayList arr = new ArrayList(); %>
<%arr = (ArrayList)request.getAttribute("array"); %>
<%for(int i=0; i<arr.size();i++) {%>
<%Content con =new Content(); %>
<% con = (Content)arr.get(i); %>
<%out.println(con.getname()); %>
<%out.println(con.getcontent()); %>
<%out.println(con.gettime()); %>
<%} %></s:form>
是否还有其他的方法?
表映射的代码:
public class Content {
private String name;
private String content;
private String time;
public String getname(){
return name;
}
public void setname(String name){
this.name = name;
}
public String getcontent(){
return content;
}
public void setcontent(String content){
this.content = content;
}
public String gettime(){
return time;
}
public void settime(Date time){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String timeString = formatter.format(time);
this.time = timeString;
}这是从数据库中取数并存到ArrayList中
public String execute() throws Exception {
DataConnection da = new DataConnection();
int i = da.OpenConn();
Content cont = new Content();
ResultSet rs = null;// here ,it is only used to identify wether the password and name are correct
String sql = "select * from content";
System.out.println(i);
rs = da.SqlExecute(sql);
int j = 1;//here, i have to pay attention to 'Column Index out of range, 0 < 1.'
int k = 0;
while(rs.next())
{
cont.setname(rs.getString("name"));
cont.settime(rs.getDate("time"));
//here ,it could happen that the data may make collision just pay attention to it .this is a very important method
cont.setcontent(rs.getString("content"));
System.out.println("this is the current time "+cont.gettime());
arr.add(cont);
cont=(Content)arr.get(k);
System.out.println("this is"+cont.getcontent());
k++;
}
System.out.println("the number is " + k);
System.out.println("the number of the rs is" + arr.size());
da.closeConn();
if(arr.size()>0)
{
HttpServletRequest Request = ServletActionContext.getRequest();
System.out.println("check the number"+arr.size());
System.out.print("success");
Request.setAttribute("array", arr);
return SUCCESS;
} else {
return ERROR;
}
}
以下是jsp页面上的代码:
<s:form action="Show">
<%ArrayList arr = new ArrayList(); %>
<%arr = (ArrayList)request.getAttribute("array"); %>
<%for(int i=0; i<arr.size();i++) {%>
<%Content con =new Content(); %>
<% con = (Content)arr.get(i); %>
<%out.println(con.getname()); %>
<%out.println(con.getcontent()); %>
<%out.println(con.gettime()); %>
<%} %></s:form>
解决方案 »
- 在JSP中写一段JAVA代码如何将String转为Date
- ireport+jasperReport多笔数据打印的问题
- 解答完就送分,init.properties的问题
- lucene索引问题求教
- 在jsp页如何改变样式???
- 请问SessionContainer这个类具体是做什么用的?谢谢
- JSP和数据库的问题
- 我想找个女朋友
- 服务器端如何调用Vb中的动态连接库,散所有分(就只有50)
- 我用EDITPLUS编辑的JSP文件,不小心把它的打开方式改成用Explore打开,结果在tomcat中运行不成功在线恭候!
- hibernate多表查询
- jxl.jar包读取excel中的合并单元格,求救~~~~
DataConnection da = new DataConnection();
int i = da.OpenConn();
Content cont = null;
ResultSet rs = null;// here ,it is only used to identify wether the password and name are correct String sql = "select * from content";
System.out.println(i);
rs = da.SqlExecute(sql);
int j = 1;//here, i have to pay attention to 'Column Index out of range, 0 < 1.'
int k = 0;
while(rs.next())
{
con = new Content();
cont.setname(rs.getString("name"));
cont.settime(rs.getDate("time"));
//here ,it could happen that the data may make collision just pay attention to it .this is a very important method
cont.setcontent(rs.getString("content"));
System.out.println("this is the current time "+cont.gettime());
arr.add(cont);
cont=(Content)arr.get(k);
System.out.println("this is"+cont.getcontent());
k++;
}
System.out.println("the number is " + k);
System.out.println("the number of the rs is" + arr.size());
da.closeConn();
if(arr.size()>0)
{
HttpServletRequest Request = ServletActionContext.getRequest();
System.out.println("check the number"+arr.size());
System.out.print("success");
Request.setAttribute("array", arr);
return SUCCESS;
} else {
return ERROR;
}
}
Content cont = new Content();
放到while(rs.next()) 中去.
while(rs.next())
{
Content cont = new Content();
......
意思就是struts2是管不了DAO的.
因为,如果你取得了所有,你肯定就知道,不过问个小问题,楼主知道简单调试不?如果知道的话,立即就能够解决了,还有,如果你知道上面代码的流程中的意思的话,也能够解决,因为,你实例化他放到了while的外面,这样,只是一次,而,你每次都需要取出下一条值,所以必须每次都来,,
不知道明白没,
如果你就仅仅是显示出来,这个没有问题,直接一个list,没问题,而且效果也还可以,如果你还有其他的什么,就应该具体分析了,
里面用声明一个方法(这里可以是一个dao类,),List findByAll(String ***);/或者是不用,因为你直接使显示,你就直接用List findByAll();,
其次,在一个具体的(这里可以是一个impl类),具体的implements,这个接口,然后具体的实现,也就是,也就是select * from ×××;然后执行出来,返回一个List,比如:
public List findByAll(String serviceName,int cltNodeId) {
// TODO Auto-generated method stub
String sql = "select * from NET_CLT_PARA_TBL where SERVICE_NAME = ? and CLT_NODE_ID = ? ";
return getJdbcTemplate().queryForList(sql, new Object[] {serviceName,cltNodeId});
},这里我用到了spring的东西,你可以不用,只要你查找出数据return就可以了,方法可以随便用,其次,就是action了,(这里过程有个转化,因为这里用到的是int,string,之类的,而前台全部都是string,所以需要new integer一下,大小写我就不顾及了,你应该能够理解,然后就在你的execute当中 来一个setList(类的实例。findByAll());就可以了,
然后在前台的jsp当总,你就直接用上面的,就可以得到,