📄 5.mvn
字号:
}
//执行修改操作
public ActionForward modifyRecord(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
BookForm bookForm = (BookForm) actionForm;
if (Operator.modifyRecord(bookForm.loadBook()) != -1) {
return this.getOperatorOkForward(actionMapping);
}
else {
this.saveGlobalErrors(httpServletRequest, "editrecord.jsp.modifyerror");
return this.getErrorForward(actionMapping);
}
}
//跳转到添加记录编辑页面
public ActionForward showAdd(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
httpServletRequest.getSession().setAttribute("bookBean", new BookForm());
httpServletRequest.getSession().setAttribute("method",
new String("addRecord"));
return this.getShowAddForward(actionMapping);
}
//跳转到修改记录编辑页面
public ActionForward showModify(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
BookBean book = new BookBean();
String str = httpServletRequest.getParameter("bookid").toString();
book = Operator.getRecord(str);
httpServletRequest.getSession().setAttribute("bookBean",
new BookForm(book.getBookId(),
book.getBookName(), book.getAuthor(), book.getPublish(), book.getPrice()));
httpServletRequest.getSession().setAttribute("method",
new String("modifyRecord"));
return this.getShowModifyForward(actionMapping);
}
//删除记录
public ActionForward showDelete(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
String str = httpServletRequest.getParameter("bookid").toString();
if (Operator.deleteRecord(str) != -1) {
return this.getOperatorOkForward(actionMapping);
}
else {
this.saveGlobalErrors(httpServletRequest, "edit.body.error");
return this.getErrorForward(actionMapping);
}
}
public ActionForward showFind(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
//传递参数
httpServletRequest.getSession().setAttribute("bookBean", new BookForm());
httpServletRequest.getSession().setAttribute("method",
new String("findRecord"));
return this.getShowFindForward(actionMapping);
}
}
以下是三个ActionForm文件:
package com.bookshop.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.HashMap;
public class BookForm
extends ActionForm {
private String author;
private String bookId;
private String bookName;
private String price;
private String publish;
private String beanId;
public BookForm() {
this.bookId = "";
this.bookName = "";
this.author = "";
this.publish = "";
this.price = "";
this.beanId = "";
}
public BookForm(String id, String name, String author, String publish,
String price) {
this.bookId = id;
this.bookName = name;
this.author = author;
this.publish = publish;
this.price = price;
this.beanId = id;
}
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 String getBeanId() {
return this.beanId;
}
public void setBeanId(String beanId) {
this.beanId = beanId;
}
public Map loadBook() {
Map record = new HashMap();
record.put("column1", this.getBookId().trim());
record.put("column2", this.getBookName().trim());
record.put("column3", this.getAuthor().trim());
record.put("column4", this.getPublish().trim());
record.put("column5", this.getPrice().trim());
return record;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
ActionErrors errors = new ActionErrors();
if (this.bookId == null || this.bookId.equals("") ||
this.bookId.length() < 1) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("book.bookid.error"));
}
if (this.bookName == null || this.bookName.equals("") ||
this.bookName.length() < 1) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("book.bookname.error"));
}
if (this.author == null || this.author.equals("") ||
this.author.length() < 1) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("book.author.error"));
}
if (this.publish == null || this.publish.equals("") ||
this.publish.length() < 1) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("book.publish.error"));
}
// if ( (Float.isNaN(this.price)) && (this.price < 0)) {
if ( (Float.isNaN(Float.parseFloat(this.price))) &&
(Float.parseFloat(this.price) < 0)) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("book.price.error"));
}
return errors;
}
}
/**/
package com.bookshop.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class FindRecordForm
extends ActionForm {
private String findByKey;
private String findByValue;
public String getFindByKey() {
return findByKey;
}
public void setFindByKey(String findByKey) {
this.findByKey = findByKey;
}
public void setFindByValue(String findByValue) {
this.findByValue = findByValue;
}
public String getFindByValue() {
return findByValue;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
ActionErrors errors = null;
if (this.findByKey.equals("") || this.findByValue.equals("")) {
errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("find.jsp.error"));
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
/**/
package com.bookshop.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
public class OperatorForm
extends ActionForm {
private String operator;
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
ActionErrors errors = new ActionErrors();
if (httpServletRequest.getParameter("operator") != null) {
String lang = httpServletRequest.getParameter("operator");
/* if ( (lang.length() < 6) || (lang.length() > 6)) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("index.jsp.operator.error"));
}
*/
}
else {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("index.jsp.operator.null"));
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
以下是业务类和数据库访问类:
package com.bookshop.model;
import java.util.Map;
import java.util.List;
import com.bookshop.util.*;
public class Operator {
private static int recordPerPage = ApplicationUtil.recordPerPage;
//动态SQL
private static String sqlNumber = "";
//留出接口设置每页显示多少条记录
public static void setRecordPerPage(int number) {
recordPerPage = number;
}
public Operator() {
}
//获得所有记录集(只查询一页记录)
public static List getRecords(int startIndex) {
String sql = "select * from booktab limit ?,?";
sqlNumber = "select count(*) from booktab";
return DBUtil.executeQuery(sql, startIndex, recordPerPage);
}
//按条件查找记录集(只查询一页记录)
public static List getRecords(String key, String value, int startIndex) {
String sql = "select * from booktab where " + key + "='" + value +
"' limit ?,?";
sqlNumber = "select count(*) from booktab where " + key + "='" + value +
"'";
return DBUtil.executeQuery(sql, startIndex, recordPerPage);
}
//查询单条记录 用于修改
public static BookBean getRecord(String value) {
String sql = "select * from booktab where bookid='" + value + "'";
BookBean book = new BookBean();
book = DBUtil.execQuery(sql);
return book;
}
//添加一条新记录
public static int addRecord(Map newRecord) {
String sql =
"insert into booktab(bookname,author,publish,price,bookid)values(?,?,?,?,?)";
return DBUtil.execUpdate(sql, newRecord);
}
//修改指定的记录
public static int modifyRecord(Map newRecord) {
String sql =
"update booktab set bookname=?,author=?,publish=?,price=? where bookid=?";
return DBUtil.execUpdate(sql, newRecord);
}
//删除指定的记录
public static int deleteRecord(String value) {
String sql =
"delete from booktab where bookid='" + value + "'";
return DBUtil.execUpdate(sql);
}
//获得表中所有记录的条数
public static int getRecordsNumber() {
return DBUtil.executeQuery(sqlNumber);
}
/*
public static void main(String[] args) {
Operator operator = new Operator();
}
*/
}
/**/
package com.bookshop.util;
import javax.servlet.http.HttpServletRequest;
public class ApplicationUtil {
public ApplicationUtil() {
}
public static final String driver = "org.gjt.mm.mysql.Driver";
public static final String url ="jdbc:mysql://localhost/bookshop";
public static final String user = "root";
public static final String password = "";
public static final int recordPerPage = 5;
public static String toGBK(String s) {
try {
return new String(s.getBytes("ISO-8859-1"), "gb2312");
}
catch (Exception ex) {
return "";
}
}
public static String getSelfURL(HttpServletRequest req) {
String s1 = req.getRequestURI();
String s2 = req.getQueryString();
if (s2 != null) {
s1 = s1 + "?" + s2;
}
return s1;
}
public static void setParam(String param, String value, java.util.HashMap map) {
String[] p = param.split(";");
String[] v = value.split(";");
for (int i = 0; i < p.length; i++) {
map.put(p[i], v[i]);
}
}
}
/**/
package com.bookshop.util;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
public class IsLastTag
extends TagSupport {
private String page = "";
public IsLastTag() {
}
public void setPage(String page) {
this.page = page;
}
public String getPage() {
return this.page;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -