viewbookaction.java

来自「这是一个网上书店」· Java 代码 · 共 77 行

JAVA
77
字号
package com.ebookstore.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebookstore.common.PagedListHolder;
import com.ebookstore.dto.Bookview;
import com.ebookstore.exception.EBookStoreException;

public class ViewBookAction extends BaseAction {
	/**
	 * 根据图书分类查询图书
	 */
	public ActionForward byCategory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//获得分类的id
		String categoryId = request.getParameter("cid");
		int id = -1;
		try {
			id = Integer.parseInt(categoryId);
		} catch (Exception e) {
			;
		}
		
		if (id > -1) {
			//查询该分类的图书
			List<Bookview> data = getEBookStore().getBookService()
					.findBookByCategory(categoryId);
			//将查询结果放入到分页器中
			PagedListHolder bookList = new PagedListHolder(data);
			//设置每页所显示的数据条数
			bookList.setPageSize(2);
			//放Session中
			request.getSession().setAttribute("ViewBooksByCategory_bookList",
					bookList);
			request.setAttribute("bookList", bookList);
		}
		return mapping.findForward("success");
	}
	/**
	 * 下一页
	 */
	public ActionForward nextPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//取出Session中的分页器
		PagedListHolder bookList = (PagedListHolder) request.getSession()
				.getAttribute("ViewBooksByCategory_bookList");
		//取出下一页数据
		bookList.nextPage();
		request.setAttribute("bookList", bookList);
		return mapping.findForward("success");
	}
	/**
	 * 上一页
	 */
	public ActionForward previousPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws EBookStoreException {
		//取出Session中的分页器
		PagedListHolder bookList = (PagedListHolder) request.getSession()
				.getAttribute("ViewBooksByCategory_bookList");
		//取出上一页数据
		bookList.previousPage();
		request.setAttribute("bookList", bookList);
		return mapping.findForward("success");
	}

}

⌨️ 快捷键说明

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