剖析:使用struts2然后用ognl接受数据,只要在action里面定义了list,,,对象,,,属性,写上set get方法在页面拿到值非常容易,,,,前提需要配置struts.xml去找action的方法
有个分页的脚本,,样式可以用
<style> .pages{ width:100.5%; text-align:right; padding:10px 0; clear:both;} .pages span,.pages a,.pages b{ font-size:12px; font-family:Arial, Helvetica,
sans-serif; margin:0 2px;} .pages span font{ color:#f00; font-size:12px;} .pages a,.pages b{ border:1px solid #5FA623; background:#fff; padding:2px
6px; text-decoration:none} .pages span { padding-right:10px } .pages b,.pages a:hover{ background:#7AB63F; color:#fff;} </style> <div class="pages"><span>共<font>1678</font>条评论</span><b>1</b><a
href="">2</a><a href="">3</a><a href="">4</a><a href="">5</a>...<a
href="">168</a><a href="">下一页>></a></div>
<?xml version="1.0" encoding="UTF-8"?> < web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> < /web-app>
3、建一个数据库表,用于保存学员信息:
4、编写PageDAO类,该类用于从数据库中取出所有的学员信息,把这些学员信息封装成一个Student对象并将 其放入List中。
package org.hnylj.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class PageDAO {
private Connection conn ;
private PreparedStatement pstmt ;
private ResultSet rs ;
private static final String DRIVER = "com.mysql.jdbc.Driver" ;
private static final String URL = "jdbc:mysql://localhost:3306/page" ;
private static final String USERNAME = "root" ;
private static final String PASSWORD = "123" ;
private Student student ;
//数据库连接 public synchronized Connection getConnection () {
try {
Class.forName (DRIVER) ;
conn = DriverManager.getConnection (URL,USERNAME,PASSWORD) ;
} catch (ClassNotFoundException e) {
e.printStackTrace () ; return null ;
} catch (SQLException e) {
e.printStackTrace () ;
return null ;
}
return conn ;
}
//分页查询
public List<Student> queryByPage (int pageSize, int pageNow) {
List<Student> list = new ArrayList<Student> () ;
try { if (this.getConnection()!=null && pageSize>0 && pageNow>0) {
pstmt = this.getConnection().prepareStatement(
"select * from student order by stu_id limit "+(pageNow*pageSize-pageSize)+","+pageSize );
rs = pstmt.executeQuery () ;
while (rs.next()) {
student = new Student () ;
student.setStu_id (rs.getInt(1)) ;
student.setStuName (rs.getString(2)) ;
student.setAddress (rs.getString(3)) ;
student.setStuPhone (rs.getString(4)) ;
list.add (student) ;
}
}
} catch(SQLException e) {
e.printStackTrace() ;
} return list ;
}
}
5、由于要对数据库中的学员信息进行封装,所以需要编写一个JavaBean,即Student类:
package org.hnylj.util; public class Student {
private int stu_id ;
private String stuName ;
private String address ;
private String stuPhone ;
然后set get方法,,你晓得:::::::
至此底层的代码就写好了。接下来我们需要编写web层的代码。
6、编写web层的jsp文件,分别有index.jsp,show.jsp和error.jsp,其代码分别如下:
<%@ page language="java" pageEncoding="UTF-8"%>
< %@ taglib uri="/struts-tags" prefix="s"%>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html>
<head>
<title>Struts2实现分页显示</title> </head> <body> <div align="center">
<s:a href="show.action">进入查看学员列表</s:a>
</div> </body> < /html> < %@ page language="java" pageEncoding="UTF-8"%>
< %@ taglib uri="/struts-tags" prefix="s"%>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html>
<head>
<title>Struts2实现分页显示</title>
</head> <body> <div align="center"> <table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>地址</th>
<th>电话</th>
</tr> <s:iterator value="students">
<tr>
<td><s:property value="stu_id"/></td>
<td><s:property value="stuName"/></td>
<td><s:property value="address"/></td>
<td><s:property value="stuPhone"/></td>
</tr>
</s:iterator>
</table>
<s:url id="url_pre" value="show.action">
<s:param name="pageNow" value="pageNow-1"></s:param>
</s:url>
<s:url id="url_next" value="show.action">
<s:param name="pageNow" value="pageNow+1"></s:param> </s:url>
<s:a href="%{url_pre}">上一页</s:a>
<s:iterator value="students" status="status">
<s:url id="url" value="show.action">
<s:param name="pageNow" value="pageNow"/>
</s:url>
</s:iterator>
<s:a href="%{url_next}">下一页</s:a>
</div>
</body>
< /html>
< %@ page language="java" pageEncoding="UTF-8"%>
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html> <head> <title>发生错误</title> </head> <body> 业务逻辑发生异常,请稍候再试。。。。。。 </body> < /html> 7、编写struts2的action来拦截用户的请求:
package org.hnylj.web; import java.util.List; import org.hnylj.util.PageDAO; import org.hnylj.util.Student; import com.opensymphony.xwork2.ActionSupport; public class ShowAction extends ActionSupport { private List<Student> students ; private int pageNow = 1 ; //初始化为1,默认从第一页开始显示 private int pageSize = 5 ; //每页显示5条记录
private PageDAO pageDAO = new PageDAO () ;
public List<Student> getStudents() { return students; }
public void setStudents(List<Student> students) { this.students = students; }
public int getPageNow() { return pageNow; }
public void setPageNow(int pageNow) { this.pageNow = pageNow; }
public int getPageSize() { return pageSize; }
public void setPageSize(int pageSize) { this.pageSize = pageSize; }
public String execute() throws Exception { students = pageDAO.queryByPage(pageSize, pageNow) ; return SUCCESS ; } }
8、配置struts.xml文件,即action和响应结果页面之间的映射:
<?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
< struts>
<package name="struts2" namespace="/" extends="struts-default">
<action name="show" class="org.hnylj.web.ShowAction">
<result name="success">show.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
< /struts>