📄 5.mvn
字号:
public int doStartTag() throws JspException {
if (this.page != null) {
//从session里面取出来的是PageInfo对象的引用
PageInfo pageBean = new PageInfo();
pageBean = (PageInfo) (pageContext.getSession().getAttribute(this.
page));
//只要该PageInfo对象的总页数等于当前页数就不处理主体部分
if (pageBean.getPageCountNumber() <= pageBean.getCurrentlyPage()) {
return this.SKIP_BODY;
}
else {
return this.EVAL_PAGE;//否则继续处理主体部分
}
}
else {
return this.SKIP_BODY;
}
}
}
/**/
package com.bookshop.util;
import java.util.List;
import java.util.ArrayList;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class DBUtil {
public DBUtil() {
}
private static String driver = ApplicationUtil.driver;
private static String url = ApplicationUtil.url;
private static String user = ApplicationUtil.user;
private static String password = ApplicationUtil.password;
private static List list = null;
private static Connection con = null;
private static Statement sta = null;
private static PreparedStatement psta = null;
private static ResultSet res = null;
//获得满足查询条件的记录行数
public static int executeQuery(String sql) {
int countNum = 0;
try {
execute(sql);
while (res.next()) {
countNum = res.getInt(1);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
close();
return countNum;
}
}
//删除记录
public static int execUpdate(String sql) {
int i = -1;
try {
getStatement();
i = sta.executeUpdate(sql);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
close();
return i;
}
}
//添加新记录
public static int execUpdate(String sql, Map newRecord) {
int i = -1;
try {
getPreparedStatement(sql);
if (newRecord != null && !newRecord.isEmpty()) {
psta.setString(1, (String) newRecord.get("column2"));
psta.setString(2, (String) newRecord.get("column3"));
psta.setString(3, (String) newRecord.get("column4"));
psta.setString(4, (String) newRecord.get("column5"));
psta.setString(5, (String) newRecord.get("column1"));
}
i = psta.executeUpdate();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
close();
return i;
}
}
//查询单个记录(用于修改)
public static BookBean execQuery(String sql) {
BookBean book = null;
try {
execute(sql);
while (res.next()) {
book = new BookBean(ApplicationUtil.toGBK(res.getString(1)),
ApplicationUtil.toGBK(res.getString(2)),
ApplicationUtil.toGBK(res.getString(3)),
ApplicationUtil.toGBK(res.getString(4)),
ApplicationUtil.toGBK(res.getString(5))
);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
close();
return book;
}
}
//查询记录(只查询指定页要显示的记录)
public static List executeQuery(String sql, int startIndex, int count) {
try {
list = new ArrayList();
getResultSet(sql, startIndex, count);
while (res.next()) {
list.add(new BookBean(ApplicationUtil.toGBK(res.getString(1)),
ApplicationUtil.toGBK(res.getString(2)),
ApplicationUtil.toGBK(res.getString(3)),
ApplicationUtil.toGBK(res.getString(4)),
ApplicationUtil.toGBK(res.getString(5))
));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
close();
return list;
}
}
private static void getConnection() throws Exception {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
//con.setAutoCommit(false);
}
private static void getPreparedStatement(String sql) throws Exception {
getConnection();
psta = con.prepareStatement(sql);
}
private static void execute(String sql) throws Exception {
getStatement();
res = sta.executeQuery(sql);
}
private static void getStatement() throws Exception {
getConnection();
sta = con.createStatement();
}
private static void getResultSet(String sql, int startIndex, int count) throws
Exception {
getPreparedStatement(sql);
psta.setInt(1, startIndex);
psta.setInt(2, count);
res = psta.executeQuery();
}
//释放资源
private static void close() {
try {
/*
if(con!=null){
con.commit();
}
*/
if (res != null) {
res.close();
}
if (psta != null) {
psta.close();
}
if (sta != null) {
sta.close();
}
if (con != null) {
con.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
/**/
package com.bookshop.util;
import java.io.Serializable;
public class BookBean
implements Serializable {
private String author = "";
private String bookId = "";
private String bookName = "";
private String price = "";
private String publish = "";
public BookBean() {
this.bookId = "";
this.bookName = "";
this.author = "";
this.publish = "";
this.price = "";
}
public BookBean(String bookId, String bookName, String author, String publish,
String price) {
this.author = author;
this.bookId = bookId;
this.bookName = bookName;
this.publish = publish;
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPublish(String publish) {
this.publish = publish;
}
public void setPrice(String price) {
this.price = price;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookId() {
return bookId;
}
public String getBookName() {
return bookName;
}
public String getPrice() {
return price;
}
public String getPublish() {
return publish;
}
/*
public static void main(String[] args) {
BookBean bookbean = new BookBean();
}
*/
}
/**/
package com.bookshop.util;
import java.io.Serializable;
public class PageInfo
implements Serializable {
private int recordCountNumber = 0; //总记录数
private int pageCountNumber = 0; //总页数
private int recordPerPage = ApplicationUtil.recordPerPage; //每页记录数
private int currentlyPage = 0; //当前页数
private int previousPageNumber = 0; //当前页的前一页数
private int nextPageNumber = 0; //当前页的后一页数
public PageInfo() {
}
//总记录数 当前页码
public PageInfo(int recordCountNumber,
int currentPage) {
this.recordCountNumber = recordCountNumber;
if (this.recordCountNumber % this.recordPerPage == 0) {
this.pageCountNumber = this.recordCountNumber / this.recordPerPage;
}
else {
this.pageCountNumber = (this.recordCountNumber / this.recordPerPage) + 1;
}
this.currentlyPage = currentPage;
}
public int getRecordCountNumber() {
return this.recordCountNumber;
}
public int getPageCountNumber() {
return this.pageCountNumber;
}
public int getRecordPerPage() {
return this.recordPerPage;
}
public int getCurrentlyPage() {
return this.currentlyPage;
}
public int getLastPageNumber() {
return this.pageCountNumber;
}
public int getPreviousPageNumber() {
this.previousPageNumber=this.currentlyPage-1;
return this.previousPageNumber;
}
public int getNextPageNumber() {
this.nextPageNumber = this.currentlyPage + 1;
return this.nextPageNumber;
}
}
以下是所有的JSP页面因为使用了Tiles框架,所以好象页面显得很多:
head.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<font color=red><html:errors/></font><br/>
<html:link href="operatorAction.do?operator=ChangeCH">
<bean:message key="head.language.chinese"/>
</html:link>
<html:link href="operatorAction.do?operator=ChangeEN">
<bean:message key="head.language.english"/>
</html:link>
left.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:link href="index.jsp">
<bean:message key="left.link.index"/>
</html:link>
<br/>
<html:link href="operatorAction.do?operator=browser">
<bean:message key="left.link.show"/>
</html:link>
<br/>
<html:link href="operatorAction.do?operator=showFind">
<bean:message key="left.link.findbykey"/>
</html:link>
<br/>
<html:link href="operatorAction.do?operator=showAdd">
<bean:message key="left.link.addnew"/>
</html:link>
<br/>
foot.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<center>
<bean:message key="foot.copyright"/>
</center>
layout.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@tagliburi="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html:html locale="true">
<body bgcolor="#ffffff">
<table border="0" width="100%">
<tr>
<td width="100%" height="91" colspan="2">
<tiles:insert attribute="head"/>
</td>
</tr>
<tr>
<td width="16%" height="290">
<tiles:insert attribute="left"/>
</td>
<td width="84%" height="290">
<tiles:insert attribute="body"/>
</td>
</tr>
<tr>
<td width="100%" height="83" colspan="2">
<tiles:insert attribute="foot"/>
</td>
</tr>
</table>
</body>
</html:html>
index_body.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<center>
<html:link href="operatorAction.do?operator=browser">
<bean:message key="index.body.nextshow"/>
</html:link>
</center>
index.jsp:
<%@page contentType="text/html; charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert definition="index-definition"/>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -