与大家分享一下,最近自己结合他人的案例做出的的小case:
声明一下:本人也是边做边学,还有非常多的地方不明白,理解不透彻,希望与大家一起成长。代码写的有点烦嗦,w但作为初学者,细细理解每一步是非常的重要
数据库表自己创建,我把表中的字段写成了实体类(Mmlib)的属性,
1:配置 web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2:找好所需要的jar包:
a:struts2-core-2.3.15.1.jar
b:xwork-core-2.3.15.1.jar
c:ognl-connector-java-5.16-bin.jar
d:freemarker-2.3.19.jar
e:jstl-1.2.jar
f:standard-1.1.0.jar
g:mysql-connector-java-5.1.6-bin.jar
3:建立数接据库连接代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnUtil {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/mmlib";
private static final String username="root";
private static final String password="1q2w3e";
static{
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnect(){
Connection conn=null;
try{
conn=DriverManager.getConnection(url,username,password);
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn,Statement st,ResultSet rs){
try{
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
4 创建分页代码
<1>
public class Page {
private int everyPage;
private int totalCount;
private int totalPage;
private int currentPage;
private int beginIndex;
private boolean hasPrePage;
private boolean hasNextPage;
public Page(int everyPage, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage) {
super();
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
public Page() {
super();
}
public int getEveryPage() {
return everyPage;
}
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public boolean isHasPrePage() {
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
}
<2>
public class PageUtil {
public static Page createPage(int everyPage,int totalCount,int currentPage) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
public static Page createPage(Page page,int totalCount) {
int everyPage = getEveryPage(page.getEveryPage());
int currentPage = getCurrentPage(page.getCurrentPage());
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
//设置每页显示记录数
public static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
//设置当前页
public static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
//设置总页数,需要总记录数,每页显示多少
public static int getTotalPage(int everyPage,int totalCount) {
int totalPage = 0;
if(totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
//设置起始点,需要每页显示多少,当前页
public static int getBeginIndex(int everyPage,int currentPage) {
return (currentPage - 1) * everyPage;
}
//设置是否有上一页,需要当前页
public static boolean getHasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
//设置是否有下一个,需要总页数和当前页
public static boolean getHasNextPage(int totalPage, int currentPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
5 创建实体类(Mmlib)
public class Mmlib {
private String guid;
private int state;
private String inData;
private String outData;
private String logType;
private Date inTime;
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getInData() {
return inData;
}
public void setInData(String inData) {
this.inData = inData;
}
public String getOutData() {
return outData;
}
public void setOutData(String outData) {
this.outData = outData;
}
public String getLogType() {
return logType;
}
public void setLogType(String logType) {
this.logType = logType;
}
public Date getInTime() {
return inTime;
}
public void setInTime(Date inTime) {
this.inTime = inTime;
}
6:创建一个类专门调用与数据库操作的类,
blic class TestQuery {
public static List<Mmlib> queryData(){
Connection conn=null;
PreparedStatement ptmt=null;
ResultSet rs=null;
List<Mmlib> data=null;
Mmlib mmlib=null;
try{
conn=ConnUtil.getConnect();
String sql="select guid,state,inData,outData,logType,inTime from mmliblog";
ptmt=conn.prepareStatement(sql);
rs=ptmt.executeQuery();
data=new ArrayList<Mmlib>();
while(rs.next()){
mmlib=new Mmlib();
mmlib.setGuid(rs.getString(1));
mmlib.setState(rs.getInt(2));
mmlib.setInData(rs.getString(3));
mmlib.setOutData(rs.getString(4));
mmlib.setLogType(rs.getString(5));
mmlib.setInTime(rs.getDate(6));
data.add(mmlib);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
ConnUtil.closeConn(conn, ptmt, rs);
}
return data;
}
7:写一个jsp登陆页面login.jsp.
<title>信息登记页面</title>
<script>
function validate(){
var username=document.fm.username.values;
var password=document.fm.password.values;
if(username==""){
alert("用户名不能为空");
}
if(password==""){
alert("密码不能为空");
}
}
</script>
</head>
<body>
<center>
<h3>看查日志登陆页面</h3>
<form name="fm" action="show" method="post" οnsubmit="return validate()">
用户名:<input type="text" name="username" value="" title="用户名不能为空"/><br>
密 码:<input type="password" name="password" value="" title="密码吗不能为空"/><br>
<input type="submit" value="登陆"/>
</form>
</center>
</body>
8:创建一个action的控制类
public class User extends ActionSupport{
private int currentPage;
private String username;
private String password;
private List<Mmlib> mmlib;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Mmlib> getMmlib() {
return mmlib;
}
public void setMmlib(List<Mmlib> mmlib) {
this.mmlib = mmlib;
}
public String execute()throws Exception{
if("admin".equals(this.getUsername())&&"1q2w3e".equals(this.getPassword())){
System.out.println("登陆成功");
}
List<Mmlib> mmlibs=TestQuery.queryData();
Page page=PageUtil.createPage(10, mmlibs.size(),currentPage);
int endIndex = page.getBeginIndex()+page.getEveryPage();
if((page.getBeginIndex()+page.getEveryPage())>=page.getTotalCount()){
endIndex = page.getTotalCount();
}
mmlibs=mmlibs.subList(page.getBeginIndex(), endIndex);
ServletActionContext.getRequest().setAttribute("page",page);//保存记录到request范围
ActionContext.getContext().getSession().put("mmlibs", mmlibs);
return SUCCESS;
}
9:再写一个显示所有数据的页面ShopMmlib.jsp
<%@ 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>日志记录</title>
</head>
<body><br>
<center><h2>看查日志记录</h2></center>
<table border="1" width="80%">
<tr>
<td width="15%">guid</td>
<td width="5%">state</td>
<td width="25%">inData</td>
<td width="25%">outData</td>
<td width="5%">logType</td>
<td width="15%">inTime</td>
</tr>
<tr>
<s:iterator value="#session.mmlibs" var="mmlibs">
<tr>
<td width="15%">${mmlibs.guid }</td>
<td width="5%">${mmlibs.state }</td>
<td width="25%">${mmlibs.inData }</td>
<td width="25%">${mmlibs.outData }</td>
<td width="5%">${mmlibs.logType }</td>
<td width="15%">${mmlibs.inTime }</td>
</tr>
</s:iterator>
</tr>
<s:iterator>
<tr>
<td colspan="5" align="center">
<s:if test="#request.page.hasPrePage">
<a href="show.action?currentPage=1">首页</a>
<a href="show.action?currentPage=${page.currentPage-1 }">上一页</a>
</s:if>
<s:else>首页 上一页</s:else>
<s:if test="#request.page.hasNextPage">
<a href="show.action?currentPage=${page.currentPage+1 }">下一页</a>
<a href="show.action?currentPage=${page.totalPage }">尾页</a>
</s:if>
<s:else>下一页 尾页</s:else>
当前为第<s:property value="#request.page.currentPage"/>页
每页共<s:property value="#request.page.everyPage"/>条
共<s:property value="#request.page.totalPage"/>页
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
10最后配置struts.xml
<struts>
<package name="struts2" extends="struts-default" >
<action name="show" class="com.Interlib.Action.User" >
<result name="success">/ShowMmlib.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>