⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookaction.java

📁 有简单的网上书店需求及设计流程
💻 JAVA
字号:
package org.wiely.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.wiely.commons.Pager;
import org.wiely.service.AdminService;
import org.wiely.service.BookService;
import org.wiely.service.BulletionService;
import org.wiely.vo.Admin;
import org.wiely.vo.Book;

import com.opensymphony.xwork2.ActionSupport;

/**
 * the action is bookAction extends ActionSupport implements SessionAware the
 * most book function is provides.
 * 
 * @prama bookService
 * @prama adminService
 * @param bulletionService
 * @prama bookname
 * @prama map session
 * @prama isbn
 * @prama bookauthor
 * @prama publisher
 * @prama introduction
 * @prama price
 * @prama picture
 * @prama isnew
 * @param bookid
 */
@SuppressWarnings("serial")
public class BookAction extends ActionSupport implements SessionAware {

	protected BookService bookService;
	protected AdminService adminService;
	protected BulletionService bulletionService;
	protected Pager pager;
	@SuppressWarnings("unchecked")
	Map session;
	String bookname;
	String isbn;
	String bookauthor;
	String publisher;
	String introduction;
	Double price;
	String picture;
	String isnew;
	int adminid;
	int currentPage = 1;
	List<File> file;
	List<String> fileFileName;
	List<String> fileContentType;

	int bookid;

	@SuppressWarnings("unchecked")
	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public BookService getBookService() {
		return bookService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public BulletionService getBulletionService() {
		return bulletionService;
	}

	public void setBulletionService(BulletionService bulletionService) {
		this.bulletionService = bulletionService;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getBookname() {
		return bookname;
	}

	public void setBookname(String bookname) {
		this.bookname = bookname;
	}

	public int getBookid() {
		return bookid;
	}

	public void setBookid(int bookid) {
		this.bookid = bookid;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getBookauthor() {
		return bookauthor;
	}

	public void setBookauthor(String bookauthor) {
		this.bookauthor = bookauthor;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getIsnew() {
		return isnew;
	}

	public void setIsnew(String isnew) {
		this.isnew = isnew;
	}
	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see show all books by name and set request Attribute is books
	 */
	@SuppressWarnings("unchecked")
	public String showByName() throws Exception {
		int totalSize = bookService.getTotalRecordsBybookname(bookname);
		pager = new Pager(currentPage, totalSize);
		List books = bookService.queryBooksByName(bookname, currentPage, pager
				.getPageSize());
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("books", books);
		request.setAttribute("pager", pager);
		
		return SUCCESS;

	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see show book by isbn and set request Attribute is books
	 */
	public String showByIsbn() throws Exception {
		Book book = bookService.queryBookByBookisbn(isbn);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("book", book);
		return SUCCESS;
	}

	/**
	 * @see show the book by adminid
	 * @return String
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String showByAdminid() throws Exception {		
		int totalSize = bookService.getTotalRecordsByadminid(adminid);
		pager = new Pager(currentPage, totalSize);

		List list = bookService.queryBooksByAdminid(adminid, currentPage, pager
				.getPageSize());
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("books", list);
		request.setAttribute("pager", pager);

		return "bookadmins";
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see update the book
	 */
	public String update() throws Exception {
		Book book = new Book();
		book.setBookauthor(bookauthor);
		book.setBookname(bookname);
		book.setIntroduction(introduction);
		book.setIsbn(isbn);
		book.setIsnew(isnew);
		book.setPublisher(publisher);
		book.setPrice(price);
		book.setBookid(bookid);
		bookService.updateBook(book);
		return SUCCESS;
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see delete book by bookid
	 */
	@SuppressWarnings("unchecked")
	public String delete() throws Exception {

		bookService.delBook(bookid);
		int adminid = (Integer) session.get("adminid");
		int totalSize = bookService.getTotalRecordsByadminid(adminid);
		pager = new Pager(currentPage, totalSize);
		List list = bookService.queryBooksByAdminid(adminid, currentPage, pager
				.getPageSize());
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("books", list);
		request.setAttribute("pager", pager);
		return SUCCESS;
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see save the new book by book param
	 */
	public String save() throws Exception {
		Book book = new Book();
		Admin admin = new Admin();
		int adminid = (Integer) session.get("adminid");
		admin.setAdminid(adminid);
		book.setBookauthor(bookauthor);
		book.setBookname(bookname);
		book.setIntroduction(introduction);
		book.setIsbn(isbn);
		book.setIsnew(isnew);
		book.setPicture(picture);
		book.setPublisher(publisher);
		book.setPrice(price);
		book.setAdmin(admin);
		bookService.insertBook(book);
		return SUCCESS;
	}

	/**
	 * 
	 * @return String
	 * @throws Exception
	 * @see upload book to server
	 */
	@SuppressWarnings("deprecation")
	public String uploadBook() throws Exception {
		int score = 0;    

		for (int i = 0; i < file.size(); i++) {
			InputStream is = new FileInputStream(file.get(i));

			String root = ServletActionContext.getRequest().getRealPath(
					"/upload");

			File desFile = new File(root, getFileFileName().get(i));

			OutputStream os = new FileOutputStream(desFile);

			byte[] buffer = new byte[400];

			int length = 0;

			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			score += 1;

			is.close();
			os.close();

		}

		/*
		 * @see add score for admin of book.
		 */
		Admin admin = new Admin();
		int adminid = (Integer) session.get("adminid");
		int score1 = adminService.score(adminid);
		score += score1;
		admin.setAdminid(adminid);
		admin.setScore(score);
		adminService.addScore(admin);

		return SUCCESS;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getAdminid() {
		return adminid;
	}

	public void setAdminid(int adminid) {
		this.adminid = adminid;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -