Java项目:基于Jsp实现网上定餐系统

Posted 编程指南针

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java项目:基于Jsp实现网上定餐系统相关的知识,希望对你有一定的参考价值。

项目编号:BS-SC-001

本项目基于JSP+SERVLET+Durid连接池进行开发实现,数据库采用mysql数据库,开发工具为IDEA或ECLIPSE,前端用采用BootStrap开发实现。系统采用三层架构设计,MVC设计模式。系统功能完整,页面简洁大方,维护方便,适合做毕业设计使用。

具体系统功能展示如下:

前台页面功能:

分类显示

餐品详情

添加购物车

个人订单管理

个人资料修改

系统留言

最近浏览功能

后台管理功能:

管理员登陆:  admin / admin

用户管理

分类管理

餐品管理

订单管理

留言管理

新闻管理

本系统是一款优秀的毕业设计系统,完美的实现了基于餐饮业务的网上订餐流程,功能强大,运行稳定,结构清晰,便于修改,适合做毕业设计使用。

部分核心代码:

package cn.jbit.easybuy.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jbit.easybuy.biz.FacilityService;
import cn.jbit.easybuy.biz.OrderService;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.FacilityServiceImpl;
import cn.jbit.easybuy.biz.impl.OrderServiceImpl;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.News;
import cn.jbit.easybuy.entity.Pager;
import cn.jbit.easybuy.entity.Product;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.entity.ShoppingCart;
import cn.jbit.easybuy.entity.User;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;

public class CartServlet extends HttpServlet 
	protected Map<String, ActionResult> viewMapping = new HashMap<String, ActionResult>();
	private ProductService productService;
	private FacilityService facilityService;
	private OrderService orderService;

	public void init() throws ServletException 
		productService = new ProductServiceImpl();
		facilityService = new FacilityServiceImpl();
		orderService = new OrderServiceImpl();
	

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		doPost(req, resp);
	

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		req.setCharacterEncoding("utf-8");
		createViewMapping();
		String actionIndicator = req.getParameter("action");
		String result = "";

		if (actionIndicator == null)
			actionIndicator = "list";
		if ("list".endsWith(actionIndicator)) 
			result = list(req);
		 else if ("add".endsWith(actionIndicator)) 
			result = add(req);
		 else if ("mod".endsWith(actionIndicator)) 
			result = mod(req);
		 else if ("remove".endsWith(actionIndicator)) 
			result = remove(req);
		 else if ("pay".endsWith(actionIndicator)) 
			result = pay(req);
		
		toView(req, resp, result);
	

	private String pay(HttpServletRequest request) 
		ShoppingCart cart = getCartFromSession(request);
		User user = getUserFromSession(request);
		if(user==null)
			return "login";
		orderService.payShoppingCart(cart, user);
		removeCartFromSession(request);
		return "paySuccess";
	

	private void removeCartFromSession(HttpServletRequest request) 
		request.getSession().removeAttribute("cart");
	

	private User getUserFromSession(HttpServletRequest request) 
		HttpSession session = request.getSession();
		return (User) session.getAttribute("loginUser");
	

	private String add(HttpServletRequest request) 
		String id = request.getParameter("entityId");
		String quantityStr = request.getParameter("quantity");
		long quantity = 1;
		if (!Validator.isEmpty(quantityStr))
			quantity = Long.parseLong(quantityStr);

		Product product = productService.findById(id);
		ShoppingCart cart = getCartFromSession(request);
		cart.addItem(product, quantity);
		return "addSuccess";
	

	private String mod(HttpServletRequest request) 
		String id = request.getParameter("entityId");
		String quantityStr = request.getParameter("quantity");
		long quantity = 1;
		if (!Validator.isEmpty(quantityStr))
			quantity = Long.parseLong(quantityStr);
		String indexStr = request.getParameter("index");
		ShoppingCart cart = getCartFromSession(request);
		cart.modifyQuantity(Integer.parseInt(indexStr), quantity);
		return "modSuccess";
	

	private String remove(HttpServletRequest request) 
		String id = request.getParameter("entityId");
		String quantityStr = request.getParameter("quantity");
		long quantity = 1;
		if (!Validator.isEmpty(quantityStr))
			quantity = Long.parseLong(quantityStr);
		String indexStr = request.getParameter("index");
		ShoppingCart cart = getCartFromSession(request);
		cart.getItems().remove(Integer.parseInt(indexStr));
		return "removeSuccess";
	

	private ShoppingCart getCartFromSession(HttpServletRequest request) 
		HttpSession session = request.getSession();
		ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
		if (cart == null) 
			cart = new ShoppingCart();
			session.setAttribute("cart", cart);
		
		//取出当前用户的订单列表

		return cart;
	

	private String list(HttpServletRequest request) 
		getCartFromSession(request);
		return "listSuccess";
	

	private void prepareCategories(HttpServletRequest request) 
		List<ProductCategory> categories = productService
				.getProductCategories(null);
		request.setAttribute("categories", categories);
	

	private void prepareNews(HttpServletRequest request) 
		List<News> allNews = facilityService.getAllNews(new Pager(10, 1));
		request.setAttribute("allNews", allNews);
	

	protected void createViewMapping() 
		this.addMapping("listSuccess", "shopping.jsp");
		this.addMapping("paySuccess", "shopping-result.jsp");
		this.addMapping("addSuccess", "Cart", true);
		this.addMapping("removeSuccess", "Cart", true);
		this.addMapping("modSuccess", "Cart", true);
		this.addMapping("login", "login.jsp");
	

	private void toView(HttpServletRequest req, HttpServletResponse resp,
			String result) throws IOException, ServletException 
		ActionResult dest = this.viewMapping.get(result);
		if (dest.isRedirect()) 
			resp.sendRedirect(dest.getViewName());
		 else 
			req.getRequestDispatcher(dest.getViewName()).forward(req, resp);
		
	

	protected void addMapping(String viewName, String url) 
		this.viewMapping.put(viewName, new ActionResult(url));
	

	protected void addMapping(String viewName, String url, boolean isDirect) 
		this.viewMapping.put(viewName, new ActionResult(url, isDirect));
	

package cn.jbit.easybuy.web;

import java.io.IOException;
import java.util.List;

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

import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;

public class CategoryServlet extends HttpServlet 
	private ProductService productService;

	public void init() throws ServletException 
		productService = new ProductServiceImpl();
	

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		doPost(req, resp);
	

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		req.setCharacterEncoding("utf-8");
		String actionIndicator = req.getParameter("action");
		ActionResult result = new ActionResult("error");
		Validator validator = new Validator(Validator.toSingleParameters(req));

		if (actionIndicator == null)
			actionIndicator = "list";
		if ("read".endsWith(actionIndicator)) 
			result = read(req, validator);
		 else if ("list".endsWith(actionIndicator)) 
			result = list(req, validator);
		 else if ("create".endsWith(actionIndicator)) 
			result = create(req, validator);
		 else if ("delete".endsWith(actionIndicator)) 
			result = delete(req, validator);
		 else if ("save".endsWith(actionIndicator)) 
			boolean isEdit = true;
			String editIndicator = req.getParameter("entityId");
			if (Validator.isEmpty(editIndicator))
				isEdit = false;
			result = save(req, validator, isEdit);
		

		if (!validator.hasErrors() && result.isRedirect()) 
			resp.sendRedirect(result.getViewName());
		 else 
			req.setAttribute("errors", validator.getErrors());
			req.getRequestDispatcher(result.getViewName()).forward(req, resp);
		
	

	public ActionResult read(HttpServletRequest request, Validator validator) 
		ProductCategory category = productService.findCategoryById(request
				.getParameter("entityId"));
		pupulateRequest(request, category);
		List<ProductCategory> categories = productService.getRootCategories();
		request.setAttribute("categories", categories);
		return new ActionResult("productClass-modify.jsp");
	

	public ActionResult save(HttpServletRequest request, Validator validator,
			boolean isEdit) 
		String entityId = request.getParameter("entityId");
		checkInputErrors(request, validator);
		saveToDatabase(request, validator, isEdit);

		return new ActionResult("Category", true);
	

	public ActionResult create(HttpServletRequest request, Validator validator) 
		List<ProductCategory> categories = productService.getRootCategories();
		request.setAttribute("categories", categories);
		request.setAttribute("parentId", 0);
		return new ActionResult("productClass-modify.jsp");
	

	public ActionResult delete(HttpServletRequest request, Validator validator) 
		productService.deleteCategory(request.getParameter("entityId"));
		return new ActionResult("Category", true);
	

	public ActionResult list(HttpServletRequest request, Validator validator) 
		List<ProductCategory> categories = productService
				.getProductCategories(null);
		request.setAttribute("categories", categories);
		return new ActionResult("productClass.jsp");
	

	private void saveToDatabase(HttpServletRequest request,
			Validator validator, boolean isEdit) 
		if (!validator.hasErrors()) 
			ProductCategory productCategory;
			if (!isEdit) 
				productCategory = new ProductCategory();
				populateEntity(request, productCategory);
				productCategory.setParentId(Long.parseLong(request
						.getParameter("parentId")));
				productService.saveCategory(productCategory);
			 else 
				productCategory = productService.findCategoryById(request
						.getParameter("entityId"));
				Long parentId = Long
						.parseLong(request.getParameter("parentId"));
				populateEntity(request, productCategory);

				if (parentId == 0) 
					if (productCategory.getId().equals(
							productCategory.getParentId())) 
						// 说明是一级分类,父分类不能修改,只能改名字
						productService.updateCategoryName(productCategory);
					 else 
						// 二级分类修改为一级分类了,需要额外更新:
						// Product原先属于该二级分类的,全部更新一级为它,二级为空
						productCategory.setParentId(productCategory.getId());
						productService.updateCategory(productCategory,
								"Level2To1");
					
				 else 
					if (!parentId.equals(productCategory.getParentId())) 
						// 二级分类修改了父分类,需要额外更新:
						// Product原先属于该二级分类的,全部更新一级为新的父分类
						productCategory.setParentId(parentId);
						productService.updateCategory(productCategory,
								"ModifyParent");
					 else 
						// 二级分类修改了名字
						productService.updateCategoryName(productCategory);
					
				
			
		
	

	private void pupulateRequest(HttpServletRequest request,
			ProductCategory productCategory) 
		request
				.setAttribute("entityId", Long
						.toString(productCategory.getId()));
		request.setAttribute("name", productCategory.getName());
		request.setAttribute("parentId", (productCategory.getParentId()
				.equals(productCategory.getId())) ? 0 : productCategory
				.getParentId());
	

	private void checkInputErrors(HttpServletRequest request,
			Validator validator) 
		validator.checkRequiredError(new String[]  "name" );
	

	private void populateEntity(HttpServletRequest request,
			ProductCategory productCategory) 
		productCategory.setName(request.getParameter("name"));
	

package cn.jbit.easybuy.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

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

import cn.jbit.easybuy.biz.FacilityService;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.FacilityServiceImpl;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.Comment;
import cn.jbit.easybuy.entity.Pager;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;

public class CommentServlet extends HttpServlet 
	private FacilityService facilityService;
	private ProductService productService;

	public void init() throws ServletException 
		this.facilityService = new FacilityServiceImpl();
		this.productService = new ProductServiceImpl();
	

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		doPost(req, resp);
	

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
		req.setCharacterEncoding("utf-8");
		String actionIndicator = req.getParameter("action");
		ActionResult result = new ActionResult("error");
		Validator validator = new Validator(Validator.toSingleParameters(req));

		if (actionIndicator == null)
			actionIndicator = "list";
		if ("read".endsWith(actionIndicator)) 
			result = read(req, validator);
		 else if ("list".endsWith(actionIndicator)) 
			result = list(req, validator);
		 else if ("delete".endsWith(actionIndicator)) 
			result = delete(req, validator);
		 else if ("save".endsWith(actionIndicator)) 
			boolean isEdit = true;
			String editIndicator = req.getParameter("entityId");
			if (Validator.isEmpty(editIndicator))
				isEdit = false;
			result = save(req, validator, isEdit);
		

		if (!validator.hasErrors() && result.isRedirect()) 
			resp.sendRedirect(result.getViewName());
		 else 
			req.setAttribute("errors", validator.getErrors());
			req.getRequestDispatcher(result.getViewName()).forward(req, resp);
		
	

	public ActionResult read(HttpServletRequest request, Validator validator) 
		Comment comment = facilityService.findCommentById(request
				.getParameter("entityId"));
		pupulateRequest(request, comment);
		return new ActionResult("guestbook-modify.jsp");
	

	public ActionResult save(HttpServletRequest request, Validator validator,
			boolean isEdit) 
		checkInputErrors(request, validator);
		saveToDatabase(request, validator, isEdit);
		return new ActionResult("GuestBook", true);
	

	public ActionResult delete(HttpServletRequest request, Validator validator) 
		facilityService.deleteComment(request.getParameter("entityId"));
		return new ActionResult("GuestBook", true);
	

	public ActionResult list(HttpServletRequest request, Validator validator) 
		String page = request.getParameter("page");
		int pageNo = 1;
		if (!Validator.isEmpty(page))
			pageNo = Integer.parseInt(page);
		long rowCount = facilityService.getCommentRowCount();
		Pager pager = new Pager(rowCount, pageNo);
		List<Comment> comments = facilityService.getComments(pager);
		List<ProductCategory> categories = productService
				.getProductCategories(null);
		request.setAttribute("categories", categories);
		request.setAttribute("comments", comments);
		request.setAttribute("pager", pager);
		request.setAttribute("pageNo", pageNo);
		return new ActionResult("guestbook.jsp");
	

	private void pupulateRequest(HttpServletRequest request, Comment comment) 
		request.setAttribute("entityId", Long.toString(comment.getId()));
		request.setAttribute("reply", comment.getReply());
		request.setAttribute("content", comment.getContent());
		request.setAttribute("nickName", comment.getNickName());
		request.setAttribute("replayTime", Validator.dateToString(comment
				.getReplyTime()));
	

	private void saveToDatabase(HttpServletRequest request,
			Validator validator, boolean isEdit) 
		if (!validator.hasErrors()) 
			Comment comment;
			if (!isEdit) 
				comment = new Comment();
				comment.setCreateTime(new Date());
				populateEntity(request, comment);
				facilityService.saveComment(comment);
			 else 
				comment = facilityService.findCommentById(request
						.getParameter("entityId"));
				if (!Validator.isEmpty(request.getParameter("reply"))) 
					comment.setReply(request.getParameter("reply"));
					comment.setReplyTime(new Date());
				
				facilityService.updateComment(comment);
			
		
	

	private void checkInputErrors(HttpServletRequest request,
			Validator validator) 
		validator.checkRequiredError(new String[]  "content", "nickName" );
	

	private void populateEntity(HttpServletRequest request, Comment comment) 
		comment.setContent(request.getParameter("content"));
		comment.setNickName(request.getParameter("nickName"));
	

以上是关于Java项目:基于Jsp实现网上定餐系统的主要内容,如果未能解决你的问题,请参考以下文章

Java+JSP+MySQL基于SSM的排序算法网上在线阅读系统

Java项目:JavaWeb实现网上图书商城系统

Java+JSP基于ssm校园二手交易系统

基于jsp+mysql的JSP在线水果销售商城系统设计实现

基于jsp+mysql的JSP在线水果销售商城系统设计实现

基于jsp+java+ssm的网上图书商城