javaee之信息后台管理实战

学习了servlet和jsp还有数据库的技术后,便自己写了一个信息后台管理的项目,其中包括的功能有:

(一)、用户的登录、注销还有修改密码

(二)、分类的管理以及对分类的增删改查

(三)、产品的管理以及对产品的增删改查和所属的分类管理


在做项目前,先进行准备工作和分析,包括设计层和导入包等,在此我采用的是三层架构的分层模式对这个项目进行分层来设计,

并且对此来个步骤:

一、系统数据库设计

  三种表

二、项目基础环境搭建

  2.1目录规范

  |-shopsys 

         |-images: 存放项目设计的页面图片

                                    |-css: 存放所有css文件

                                    |-js: 存放javascript文件 

         |-views: 存放所有项目的页面(jsp)

                 login.jsp   登录页面

                          |-main  

                                                             index.jsp   系统的主页

     |-types : 存放所有分类相关的页面

                                                                     add.jsp

              list.jsp

              edit.jsp

                                   |-products :存放产品相关的页面

                                                             xxxxxxxxx

            |-src

                                              |-gz.itcast.shopsys

                                                     |-entity   :  存放所有实体对象

                                                     |-dao:  存放dao接口

                                                           |-impl:  存放dao实现

                            |-service: 存放service接口

                                                           |-impl:  存放service实现

                                                      |-web:  表现层类

   |-servlet:  servlet类

   |-action :struts2类 

                            |-util  :  工具类

                           |-exception: 自定义业务类

                           xxxxxxxxxx     

                           

                                           2.2 导入项目导入jar包

  1)c3p0连接池

  2)dbutils

  3)数据库驱动程序

  4)common-fileUplaod  

                 2.3 准备项目使用到的工具类 

  1)C3P0Util   : 获取连接池

  2)BaseServlet: 通用的servlet类(抽取了每个模块通用的方法)

                                                        3)EncodingFilter:过滤所有请求中文数据

                              4)ValidateCodeUtil: 生成验证码(用servlet生成) 

  5)MD5Util:密码加密工具


采用的模式:

javaee之信息后台管理实战_第1张图片

先设计了各个实体类:

Admins:

package gz.itcast.shopsys.entiry;

public class Admins {

	private int id;
	private String name;
	private String password;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
Product:

package gz.itcast.shopsys.entiry;

import java.util.Date;

public class Products {

	private int id;
	private String name;
	private double price;
	private String descr;
	private String img;
	private Date addtime;
	
	private Types types;
	private Admins admins;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getDescr() {
		return descr;
	}
	public void setDescr(String descr) {
		this.descr = descr;
	}
	public String getImg() {
		return img;
	}
	public void setImg(String img) {
		this.img = img;
	}
	public Date getAddtime() {
		return addtime;
	}
	public void setAddtime(Date addtime) {
		this.addtime = addtime;
	}
	public Types getTypes() {
		return types;
	}
	public void setTypes(Types types) {
		this.types = types;
	}
	public Admins getAdmins() {
		return admins;
	}
	public void setAdmins(Admins admins) {
		this.admins = admins;
	}
	
	
}
Types:

package gz.itcast.shopsys.entiry;

import java.util.ArrayList;
import java.util.List;

public class Types {

	private int id;
	private String name;
	private String descr;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescr() {
		return descr;
	}
	public void setDescr(String descr) {
		this.descr = descr;
	}
	
	
}


同时还涉及了一个关于分页的PageBean实体类:

PageBean:

package gz.itcast.shopsys.entiry;

import java.util.List;

public class PageBean {

	private List data;  //页面数据
	private int firstPage = 1; //首页
	private int prePage;  //上一页
	private int nextPage;  //下一页
	private int totalPage;  //总页数,末页
	private int curPage;  //当前页
	private int pageSize = 5;  //每页显示的数据
	private int totalCount;  //总记录数
	
	public List getData() {
		return data;
	}
	public void setData(List data) {
		this.data = data;
	}
	public int getFirstPage() {
		return firstPage;
	}
	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}
	
	//上一页,如果为首页,则不能再减1
	public int getPrePage() {
		return this.getCurPage()==this.getFirstPage()?this.getFirstPage():this.getCurPage()-1;
	}
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}
	
	//下一页,如果为末页则不能在加一
	public int getNextPage() {
		return this.getCurPage()==this.getTotalPage()?this.getTotalPage():this.getCurPage()+1;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	
	//总页,判断是否总记录数和每页记录取余,不整除则加1
	public int getTotalPage() {
		return this.getTotalCount()%this.getPageSize()==0?this.getTotalCount()/this.getPageSize():this.getTotalCount()/this.getPageSize()+1;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	//=========================
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	
}


包括业务逻辑层为Service:

AdminService:

package gz.itcast.shopsys.service.impl;

import gz.itcast.shopsys.dao.AdminDao;
import gz.itcast.shopsys.dao.impl.AdminDaoImpl;
import gz.itcast.shopsys.entiry.Admins;
import gz.itcast.shopsys.exception.LoginErrorException;
import gz.itcast.shopsys.service.AdminService;
import gz.itcast.shopsys.util.MD5Util;

public class AdminServiceImpl implements AdminService {

	private AdminDao admindao = new AdminDaoImpl();
	
	/**
	 * 管理员的登录
	 */
	public Admins Login(String name, String password) throws LoginErrorException {
		
		String newpassword = MD5Util.md5(password);
		
		Admins admins = admindao.login(name, newpassword);
		if(admins==null){
			throw new LoginErrorException("用户或者密码错误!");
		}
		
		return admins;
	}

	public void edit(Admins admins) {
		// TODO Auto-generated method stub
		admindao.save(admins);
	}

	
}

除此还有ProductService、TypesService层....



还有数据访问层Dao:

AdminsDao:

package gz.itcast.shopsys.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import gz.itcast.shopsys.dao.AdminDao;
import gz.itcast.shopsys.entiry.Admins;
import gz.itcast.shopsys.util.C3P0Utils;

public class AdminDaoImpl implements AdminDao {

	QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
	
	
	/**
	 * 后台管理员的登录
	 */
	public Admins login(String name, String password) {
		// TODO Auto-generated method stub
		String sql = "select * from admins where name=? and password =?";
		try {
			return qr.query(sql, new BeanHandler(Admins.class), name,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}


	public void save(Admins admins) {
		// TODO Auto-generated method stub
		String sql ="update admins set password=? where id=?";
		try {
			qr.update(sql, admins.getPassword(),admins.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}


	

	
}

其外还有ProductDAo、TypesDao层....


以及页面访问层Servlet和JSP:

AdminsServlet:

package gz.itcast.shopsys.web;



import java.io.IOException;


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

import gz.itcast.shopsys.entiry.Admins;
import gz.itcast.shopsys.exception.LoginErrorException;
import gz.itcast.shopsys.service.AdminService;
import gz.itcast.shopsys.service.impl.AdminServiceImpl;
import gz.itcast.shopsys.util.BaseServlet;
import gz.itcast.shopsys.util.MD5Util;

public class AdminsServlet extends BaseServlet {

	private AdminService adminService = new AdminServiceImpl();
	
	public void login(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException{
		
		//得到用户名和密码还有验证码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String verifycode = request.getParameter("verifycode");
		
		//验证验证码的正确性
		String code = (String) request.getSession().getAttribute("verifycode");
		if(!verifycode.equalsIgnoreCase(code) || verifycode==null){
			request.setAttribute("msg", "验证码错误");
			request.getRequestDispatcher("/views/login.jsp").forward(request, response);
			return;
		}
		
		try {
			Admins admins = adminService.Login(username, password);
			
			request.getSession().setAttribute("loginInfo", admins);
			request.getRequestDispatcher("/views/main/index.jsp").forward(request, response);
			
		} catch (LoginErrorException e) {
			request.setAttribute("msg", e.getMessage());
			request.getRequestDispatcher( "/views/login.jsp").forward(request, response);
			return;
		}
	}
	
	
	public void logout(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException{
		HttpSession session = request.getSession();
		
		if(session!=null){
			session.invalidate();
			request.getRequestDispatcher("/views/login.jsp").forward(request, response);
		}
	}
	
	
	public void edit(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException{
		
		Admins admins = (Admins) request.getSession().getAttribute("loginInfo");
		
		String newps = request.getParameter("newpassword");
		String newpassword = MD5Util.md5(newps);
		
		admins.setPassword(newpassword);
		
		try{
			adminService.edit(admins);
			
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write("
修改成功!3秒后自动跳转到列表
"); response.setHeader("refresh", "3;url="+request.getContextPath()+"/TypesServlet?action=findByPage"); }catch(Exception e){ } } }

除此还有ProductServlet、TypesServlet层....


在这个项目中还运用到了一些工具类,包括设计了一个运用反射技术来减少Servlet在web.xml中配置的代码。


后面还有附带整个项目的源代码:



你可能感兴趣的:(javaee,java,ee)