java查询收藏的商品_JavaWeb_(MVC)管理员后台商品查询demo

该博客介绍了使用Java MVC架构实现管理员后台商品查询的示例。通过Model、View、Controller三层架构,详细展示了如何处理用户请求、执行业务逻辑以及展示商品列表。在模型层处理业务逻辑,DAO层负责数据库操作,而控制器层则协调视图和模型。示例中使用了模拟数据库的DBUtil类,以及Goods和User实体类,实现商品管理和用户登录功能。
摘要由CSDN通过智能技术生成

MVC分层实现管理员后台商品查询

MVC层即model view controller

Model(模型):模型代表着核心的业务逻辑和数据(不要理解成Model只是实体类)

View(视图):视图应该关注与如何展示数据,而不应该包含任何业务逻辑(业务逻辑应写在Model中)

Controller(控制器):控制器控制着程序的逻辑,并充当着视图和模型之间的协调角色。控制器从视图层接收用户输入的信息,然后使用模型来执行特定的操作,并把最终的结果回传给视图

model层:存放业务逻辑处理,不直接和数据库打交道但存放关于数据库处理的操作

Dao层:对数据库进行数据持久化操作,操作直接针对数据库

index.jsp进行首页的展示,用户可以在首页进行普通用户的注册和登录,当使用管理员账号登录时候可以进入商品后台对其进行进一步管理(LoginServlet.java中实现)

ba242ddce1e3aa8ea2191d59228e42bf.gif

项目结构

92c170065f16366f258c71c4b64f04f4.png

DBUtil.java模拟数据库,已存在的管理员用户和商品信息列表如下,注册时只能对普通用户进行注册,Gary和Gary2的isAdmin为True

public static Map userMap = new HashMap();public static List goodsList = new ArrayList();static{

addUser(new User("Gary","123",10,"男",true));

addUser(new User("Gary2","456",20,"男",true));

goodsList.add(new Goods("香蕉","这个是香蕉",100,110));

goodsList.add(new Goods("苹果","这个是苹果",200,120));

goodsList.add(new Goods("西瓜","这个是西瓜",300,130));

goodsList.add(new Goods("桃子","这个是桃子",400,140));

}

逻辑层:处理代码请求

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.controller;importjava.io.IOException;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.Gary.model.Goods;importcom.Gary.service.GoodsService;

@WebServlet("/admin/goods_list")public class GoodsListServlet extendsHttpServlet {

@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

doGet(req, resp);

}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

List list = newGoodsService().selectAllGoods();

request.setAttribute("list", list);

request.getRequestDispatcher("/admin/goods_list.jsp").forward(request, response);

}

}

GoodsListServlet.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.controller;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.Gary.model.User;importcom.Gary.service.UserService;

@WebServlet("/login_do")public class LoginServlet extendsHttpServlet {

@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

String username= request.getParameter("username");

String password= request.getParameter("password");

UserService service= newUserService();

User user=service.login(username, password);

System.out.println(user);if(user!=null) {

request.getSession().setAttribute("user", user);//System.out.println(user);//TODO

if(user.isAdmin()) {//管理员//response.sendRedirect(request.getContextPath()+"/admin/goods_list.jsp");

request.getRequestDispatcher("admin/goods_list").forward(request, response);//不需要传递数据时可以使用重定向//response.sendRedirect(request.getContextPath()+"/admin/good_list");

}else{//普通用户//System.out.println("完成");

request.getRequestDispatcher("/").forward(request, response);

}

}else{

request.setAttribute("msg", "用户名或密码错误,请重新登录!");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}

}

}

LoginServlet.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.controller;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.Gary.service.UserService;/*** Servlet implementation class RegisterService*/@WebServlet("/register_do")public class RegisterService extendsHttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//设置中文编码

request.setCharacterEncoding("utf-8");

String username= request.getParameter("username");

String password= request.getParameter("password");int age = Integer.parseInt(request.getParameter("age"));

String sex= request.getParameter("sex");

UserService userService= newUserService();boolean isSuccess =userService.register(username,password,age,sex);if(isSuccess) {

request.setAttribute("msg", "注册成功,请登录!");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}else{

request.setAttribute("msg", "用户名重复,请重新填写");

request.getRequestDispatcher("/register.jsp").forward(request, response);

}

}

}

RegisterService.java

DAO层:将数据存到数据库中

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.dao;importjava.util.List;importcom.Gary.model.Goods;importcom.Gary.util.DBUtil;public classGoodsDao {public ListgetAllGoods(){returnDBUtil.goodsList;

}

}

GoodsDao.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.dao;importcom.Gary.model.User;importcom.Gary.util.DBUtil;public classUserDao {public booleanisExist(String username) {returnDBUtil.userMap.containsKey(username);

}public boolean addUser(String username,String password,intage,String sex) {if(isExist(username)) return false;

User u= new User(username,password,age,sex,false);

DBUtil.userMap.put(u.getUsername(),u);return true;

}publicUser getUserByUP(String username,String password) {if(isExist(username)==false) return null;

User u=DBUtil.userMap.get(username);if(u.getPassword().equals(password))returnu;else return null;

}

}

UserDao.java

model层:存放我们的实体类,与数据库中的属性值基本保持一致

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.model;public classGoods {privateString name;privateString des;private intprice;private intinventory;public Goods(String name, String des, int price, intinventory) {super();this.name =name;this.des =des;this.price =price;this.inventory =inventory;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getDes() {returndes;

}public voidsetDes(String des) {this.des =des;

}public intgetPrice() {returnprice;

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

}public intgetInventory() {returninventory;

}public void setInventory(intinventory) {this.inventory =inventory;

}

}

Goods.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.model;public classUser {privateString username;privateString password;private intage;privateString sex;private boolean isAdmin=false; //判断是否是管理员账号

public User(String username, String password, int age, String sex, booleanisAdmin) {super();this.username =username;this.password =password;this.age =age;this.sex =sex;this.isAdmin =isAdmin;

}publicString getUsername() {returnusername;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}publicString getSex() {returnsex;

}public voidsetSex(String sex) {this.sex =sex;

}public booleanisAdmin() {returnisAdmin;

}public void setAdmin(booleanisAdmin) {this.isAdmin =isAdmin;

}

}

User.java

service层:存放业务逻辑处理

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.service;importjava.util.List;importcom.Gary.dao.GoodsDao;importcom.Gary.model.Goods;public classGoodsService {public ListselectAllGoods(){return newGoodsDao().getAllGoods();

}

}

GoodsService.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.service;importcom.Gary.dao.UserDao;importcom.Gary.model.User;public classUserService {//处理注册请求

public boolean register(String username,String password,intage ,String sex) {

UserDao userDao= newUserDao();boolean isExist =userDao.isExist(username);if(isExist)return false;elseuserDao.addUser(username, password, age, sex);return true;

}publicUser login(String username,String password) {return newUserDao().getUserByUP(username, password);

}

}

UserService.java

util层:辅助作用  【我用来做数据库的模拟】

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.Gary.util;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importcom.Gary.model.Goods;importcom.Gary.model.User;//模拟数据库

public classDBUtil {public static Map userMap = new HashMap();public static List goodsList = new ArrayList();static{

addUser(new User("Gary","123",10,"男",true));

addUser(new User("Gary2","456",20,"男",true));

goodsList.add(new Goods("香蕉","这个是香蕉",100,110));

goodsList.add(new Goods("苹果","这个是苹果",200,120));

goodsList.add(new Goods("西瓜","这个是西瓜",300,130));

goodsList.add(new Goods("桃子","这个是桃子",400,140));

}private static voidaddUser(User user) {

userMap.put(user.getUsername(), user);

}

}

DBUtil.java

商品列表页面

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Insert title here

*{font-size = 20px;}

商品管理

添加

商品名描述价格库存操作

list=(List)request.getAttribute("list");for(Goods goods : list){%>

删除 修改

goods_list.jsp

首页面

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Insert title here

Objectuser=session.getAttribute("user");if(user==null){%>

/login.jsp">登录

/register.jsp">注册

out.println("当前登录用户:"+((User)user).getUsername());

}%>

各种商品的展示

index.jsp

用户登录页面

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Insert title here

if(request.getAttribute("msg")!=null)

out.println(request.getAttribute("msg")+"
");%>登录


/login_do"method="post">用户名:
密码:

login.jsp

用户注册页面

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

Insert title here

if(request.getAttribute("msg")!=null)

out.println(request.getAttribute("msg")+"
");%>注册


/register_do"method="post">用户名:
密码:
年龄:
性别: 男女

register.jsp

实现过程

index.jsp判断有无用户登录,有用户登录时展示用户信息,若无用户登录则显示用户注册登录

Objectuser=session.getAttribute("user");if(user==null){%>

/login.jsp">登录

/register.jsp">注册

out.println("当前登录用户:"+((User)user).getUsername());

}%>

用户注册后将表单提交给register_do

/register_do"method="post">用户名:
密码:
年龄:
性别: 男女

创建用户数据模型User,java和Goods.java

privateString username;privateString password;private intage;privateString sex;private boolean isAdmin=false; //判断是否是管理员账号

privateString name;private String des; //商品描述信息

private intprice;private int inventory; //商品库存

DBUtil.java模拟数据库,将用户与商品存放到List<>集合中

public static Map userMap = new HashMap();public static List goodsList = new ArrayList();static{

addUser(new User("Gary","123",10,"男",true));

addUser(new User("Gary2","456",20,"男",true));

goodsList.add(new Goods("香蕉","这个是香蕉",100,110));

goodsList.add(new Goods("苹果","这个是苹果",200,120));

goodsList.add(new Goods("西瓜","这个是西瓜",300,130));

goodsList.add(new Goods("桃子","这个是桃子",400,140));

}private static voidaddUser(User user) {

userMap.put(user.getUsername(), user);

}

UserDao.java和GoodDao.java处理数据库逻辑

//用户是否退出登录

public booleanisExist(String username) {returnDBUtil.userMap.containsKey(username);

}//添加用户

public boolean addUser(String username,String password,intage,String sex) {if(isExist(username)) return false;

User u= new User(username,password,age,sex,false);

DBUtil.userMap.put(u.getUsername(),u);return true;

}//判断用户是否登录成功

publicUser getUserByUP(String username,String password) {if(isExist(username)==false) return null;

User u=DBUtil.userMap.get(username);if(u.getPassword().equals(password))returnu;else return null;

}

public classGoodsDao {//列出所有的商品

public ListgetAllGoods(){returnDBUtil.goodsList;

}

}

RegisterService.java处理用户注册信息,处理用户的注册和登录

@WebServlet("/register_do")public class RegisterService extendsHttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//设置中文编码

request.setCharacterEncoding("utf-8");

String username= request.getParameter("username");

String password= request.getParameter("password");int age = Integer.parseInt(request.getParameter("age"));

String sex= request.getParameter("sex");

UserService userService= newUserService();boolean isSuccess =userService.register(username,password,age,sex);if(isSuccess) {

request.setAttribute("msg", "注册成功,请登录!");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}else{

request.setAttribute("msg", "用户名重复,请重新填写");

request.getRequestDispatcher("/register.jsp").forward(request, response);

}

}

}

LoginServlet.java处理用户登录后分用户和管理员操作,用户跳转到首页,管理员则转发到goods_list对商品后台信息进行处理

GoodsListServlet.java处理转发的goods_list请求

@WebServlet("/admin/goods_list")public class GoodsListServlet extendsHttpServlet {

@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

doGet(req, resp);

}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

List list = newGoodsService().selectAllGoods();

request.setAttribute("list", list);

request.getRequestDispatcher("/admin/goods_list.jsp").forward(request, response);

}

}

goods_list.jsp展示数据库中商品信息列表

list=(List)request.getAttribute("list");for(Goods goods : list){%>

删除 修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值