struts2中从数据库中读取数据,并在JSP页面中遍历保存有JavaBean对象的List对象
2016-02-20 14:23
701 查看
0x1:前言
前面搭建struts就不说了,大家都能成功完毕。0x2:Model
这里我们须要一个Model类来接收<span style="font-size:10px;font-weight: normal;">package com.model; public class BlogContent { private int id; private String title; private String content; public BlogContent(int id, String title, String content) { super(); this.id = id; this.title = title; this.content = content; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }</span><span style="color:#ff6666;font-size:18px;"> </span>
d dsfadd dsfasdf
0x3:Action处理control
首先创建一个Action类,用来处理我们的请求。因为是測试,写的代码比較乱,大家将就看下。
package com.wzw; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.model.BlogContent; import com.opensymphony.xwork2.ActionSupport; import com.wzw.db.dbUtil; public class BlogContentAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 7223376836405887303L; @Override public String execute() throws Exception { // TODO 自己主动生成的方法存根 List<BlogContent> BlogContentlist=new ArrayList<BlogContent>(); String sql="select * from content"; dbUtil dbutil=new dbUtil(); Connection con=dbutil.getCon(); PreparedStatement pstmt= con.prepareStatement(sql); ResultSet rs=pstmt.executeQuery(); while(rs.next()) { int id=rs.getInt("id"); String title=rs.getString("title"); String content=rs.getString("content"); BlogContent temp=new BlogContent(id,title,content); BlogContentlist.add(temp); } HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("bloglist", BlogContentlist); return SUCCESS; } }
PS:因为我之前用的这张表。字段各自是id主键,title 和content,用来測试。
0x4 映射
配置struts.xml文件。添加我们的映射。<action name="BlogContent" class="com.wzw.BlogContentAction" > <result name="success">/1.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> </action>
訪问以后跳转到1.jsp上去。
0x5 显示层 View
编写1.jsp来遍历我们request中存放的数据,代码例如以下<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:iterator value="#request.bloglist" id="mycontent" status="status"> <s:property value="#mycontent.id"/><br> <s:property value="#mycontent.title"/><br> <s:property value="#mycontent.content"/><br><hr> </s:iterator> </body> </html>
0x6 result
訪问我们的映射地址http://localhost:8080/struts//BlogContent
结果例如以下
1
这是測试标题1
这是測试标题内容1。
2
Nothing is as sweet as you
Nothing is as sweet as you.....
0x6 END
END相关文章推荐
- 转载 java定时器时间配置
- spring mvc 的配置 (mvc 和servlet<action>的配置)
- JAVA 强制类型转换错误提示
- spring mvc 的两种配置方式继承controller和xml配置delegate
- 用JAVA写的插入排序
- String类型
- windows下,用eclipse连接虚拟机中的hadoop
- Java并发读取List2
- springMVC搭建
- JAVA集合类总结
- SpringMVC项目使用Thymeleaf模板引擎
- 浅析Java内存分配机制
- JAVA super() this() 详解
- Java学习笔记2016.2.20 访问修饰符、包
- Java測试覆盖率工具----Cobertura,EclEmma
- 命令行运行程序 解决 java “错误:编码GBK 的不可映射字符”
- Java多线程的通信2
- Spring+SpringMVC项目搭建
- JAVA多线程-线程间通信(一)-等待/通知机制(wait/notify)
- Java多线程的通信1