util 代码
JdbcUtils
package com.atguigu.utils; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; public class JdbcUtils { private static DruidDataSource dataSource; private static ThreadLocal<Connection> conns=new ThreadLocal<>(); static { try { Properties properties=new Properties(); //读取jdbc.properties属性配置文件 InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); //从流中加载数据 properties.load(inputStream); //创建数据连接池 dataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);//Ctrl+ALT+T } catch (Exception e){ e.printStackTrace(); } } /** * 获取数据库连接池中的连接 * @return 如果返回null,说明获取连接失败<br/> 有值就是获取连接成功 */ public static Connection getConnection(){ Connection conn=conns.get(); if (conn==null){ try { conn= dataSource.getConnection();//从数据库连接池中获取连接 conns.set(conn);//保存到ThreadLocal对象中,供后面的jdbc操作使用 conn.setAutoCommit(false);//设置为手动管理事务 } catch (SQLException throwables) { throwables.printStackTrace(); } } return conn; } /** * 提交事务,并关闭释放连接 */ public static void commitAndClose(){ Connection connection=conns.get(); if (connection!=null){//如果不等于null,说明之前使用过连接,操作过数据库 try { connection.commit();//提交 事务 } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { connection.close();//关闭连接,释放资源 } catch (SQLException throwables) { throwables.printStackTrace(); } } } //一定要执行remove操作,否则就会出错。(因为Tomcat服务器底层使用了线程池技术) conns.remove(); } /** * 回滚事务,并关闭释放连接 */ public static void rollbackAndClose(){ Connection connection=conns.get(); if (connection!=null){//如果不等于null,说明之前使用过连接,操作过数据库 try { connection.rollback();//回滚 事务 } catch (SQLException throwables) { throwables.printStackTrace(); }finally { try { connection.close();//关闭连接,释放资源 } catch (SQLException throwables) { throwables.printStackTrace(); } } } //一定要执行remove操作,否则就会出错。(因为Tomcat服务器底层使用了线程池技术) conns.remove(); } /** * 关闭连接,放回数据库连接池 * @param conn public static void close(Connection conn){ if (conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } */ }
WebUtils
package com.atguigu.utils; import org.apache.commons.beanutils.BeanUtils; import java.util.Map; public class WebUtils { /** * 把Map中的值注入到对应的JavaBean属性中 * @param value * @param bean * * HttpServletRequest * Dao层 * Service层 * web层 耦合度高 * */ // public static void copyParamToBean(HttpServletRequest req,Object bean ){ public static <T> T copyParamToBean(Map value, T bean ){ try { System.out.println("注入之前:"+bean); /** * 把所有请求的参数都注入到user对象中 */ BeanUtils.populate(bean,value);//出现NoClassDefFoundError错误导入commons-collections3.x.jar,4.x不行 System.out.println("注入之后:"+bean); } catch (Exception e) { e.printStackTrace(); } return bean; } /** * 将字符串转换成为int类型的数据 * @param strInt * @param defaultValue * @return */ public static int parseInt(String strInt,int defaultValue){ try { return Integer.parseInt(strInt); } catch (Exception e) { // e.printStackTrace(); } return defaultValue; } }
web 框架
web 代码
BaseServlet
package com.atguigu.web; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; public abstract class BaseServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决post请求中文乱码问题 //一定要在获取请求参数之前调有才有效 req.setCharacterEncoding("UTf-8"); //解决响应中文乱码 resp.setContentType("text/html;charset=UTF-8"); String action=req.getParameter("action"); // System.out.println(action); //action的value和调用的方法名是统一的 // if ("login".equals(action)){ System.out.println("处理登录的需求"); // login(req,resp); // }else if ("regist".equals(action)){ System.out.println("处理注册的需求"); // regist(req,resp); // } //反射 try { //获取action业务鉴别字符串,获取相应的业务方法 反射对象 Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class); //调用目标业务方法 method.invoke(this,req,resp); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e);//把异常抛给Filter过滤器 } } }
BookServlet
package com.atguigu.web; import com.atguigu.pojo.Book; import com.atguigu.pojo.Page; import com.atguigu.service.BookService; import com.atguigu.service.impl.BookServiceImpl; import com.atguigu.utils.WebUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; public class BookServlet extends BaseServlet { private BookService bookService=new BookServiceImpl(); /** * 处理分页功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 获取请求的参数 pageNo 和 pageSize int pageNo=WebUtils.parseInt(req.getParameter("pageNo"),1); int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE); //2 调用BookService.page(pageNo, pageSize): Page对象 Page<Book> page=bookService.page(pageNo,pageSize);//创建方法page page.setUrl("manager/bookServlet?action=page"); //3 保存Page对象到Request域中 req.setAttribute("page",page); //4 请求转发到pages/manager/book_manager.jsp页面 req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp); } protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int pageNo=WebUtils.parseInt(req.getParameter("pageNo"),0); pageNo+=1; // 1、获取请求的参数==封装成为Book对象 Book book= WebUtils.copyParamToBean(req.getParameterMap(),new Book()); // 2、调用BookService.addBook0保存图书 bookService.addBook(book); // 3、跳到图书列表页面 // /manager/bookServlet?action=list // req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);//请求转发有bug resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+pageNo);//重定向 } protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、获取请求的参数id,图书编程 int id=WebUtils.parseInt(req.getParameter("id"),0); // 2、调用bookService.deleteBookById();删除图书 bookService.deleteBookById(id); // 3、重定向回图书列表管理页西 // /book/manager/bookServlet?action=list resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo")); } protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、获取请求的参数==封装成为Book对象 Book book=WebUtils.copyParamToBean(req.getParameterMap(),new Book()); // 2、调用BookService.updateBook( book);修改图书 bookService.updateBook(book); // 3、重定向回图书列表管理页面 // 地址:/工程名/manager/bookServlet?action=list resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo")); } protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1获取请求的参数图书编号 int id=WebUtils.parseInt(req.getParameter("id"),0); //2调用bookService.queryBookById查询图书 Book book= bookService.queryBookById(id); //3保存到图书到Request域中 req.setAttribute("book",book); //4请求转发到。pages/manager/book_edit.jsp页面 req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp); } protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 通过BookService 查询全部图书 List<Book> books = bookService.queryBooks(); //2 把全部图书保存到Request域中 req.setAttribute("books",books); //3 请求转发到/pages/manager/book_manager.jsp页面 req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp); } }
CartServlet
package com.atguigu.web; import com.atguigu.pojo.Book; import com.atguigu.pojo.Cart; import com.atguigu.pojo.CartItem; import com.atguigu.service.BookService; import com.atguigu.service.impl.BookServiceImpl; import com.atguigu.utils.WebUtils; import com.google.gson.Gson; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class CartServlet extends BaseServlet{ private BookService bookService=new BookServiceImpl(); /** * 加入购物车 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // System.out.println("加入购物车"); // System.out.println("商品编号:"+req.getParameter("id")); //获取请求的参数 商品编号 int id= WebUtils.parseInt(req.getParameter("id"),0); //调用bookService.queryBookById(id):Book 得到图书的信息 Book book=bookService.queryBookById(id); //把图书信息,转换成为CartItem商品项 CartItem cartItem=new CartItem(book.getId(), book.getName(), 1,book.getPrice(),book.getPrice()); //调用Cart.addItem(CartItem):添加商品项 Cart cart= (Cart) req.getSession().getAttribute("cart"); if (cart==null){ cart=new Cart(); req.getSession().setAttribute("cart",cart); } cart.addItem(cartItem); // System.out.println(cart); //重定向回商品列表页面 // resp.sendRedirect(req.getContextPath()); // System.out.println("请求头Referer的值:"+req.getHeader("Referer")); //最后一个添加商品的名称 req.getSession().setAttribute("lastName",cartItem.getName()); //重定向回原来商品所在地址页面 resp.sendRedirect(req.getHeader("Referer")); } protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求的参数 商品编号 int id= WebUtils.parseInt(req.getParameter("id"),0); //调用bookService.queryBookById(id):Book 得到图书的信息 Book book=bookService.queryBookById(id); //把图书信息,转换成为CartItem商品项 CartItem cartItem=new CartItem(book.getId(), book.getName(), 1,book.getPrice(),book.getPrice()); //调用Cart.addItem(CartItem):添加商品项 Cart cart= (Cart) req.getSession().getAttribute("cart"); if (cart==null){ cart=new Cart(); req.getSession().setAttribute("cart",cart); } cart.addItem(cartItem); //最后一个添加商品的名称 req.getSession().setAttribute("lastName",cartItem.getName()); // 6、返回购物车总的商品数量和最后一个添加的商品名称 Map<String,Object> resultMap=new HashMap<>(); resultMap.put("totalCount",cart.getTotalCount()); resultMap.put("lastName",cartItem.getName()); Gson gson=new Gson(); String resultMapJosnString =gson.toJson(resultMap); resp.getWriter().write(resultMapJosnString); } /** * 删除商品项 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求的参数 商品编号 int id= WebUtils.parseInt(req.getParameter("id"),0); //获取购物车对象 Cart cart= (Cart) req.getSession().getAttribute("cart"); if (cart!=null){ //删除了购物车商品项 cart.deleteItem(id); //重定向回原来商品所在地址页面 resp.sendRedirect(req.getHeader("Referer")); } } /** * 清空购物车 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取购物车对象 Cart cart= (Cart) req.getSession().getAttribute("cart"); if (cart!=null){ //清空购物车 cart.clear(); //重定向回原来商品所在地址页面 resp.sendRedirect(req.getHeader("Referer")); } } /** * 修改商品数量 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求的参数 商品编号 ,商品数量 int id=WebUtils.parseInt(req.getParameter("id"),0); int count=WebUtils.parseInt(req.getParameter("count"),1); //获取Cart购物车对象 Cart cart= (Cart) req.getSession().getAttribute("cart"); if (cart!=null){ //修改商品数量 cart.updateCount(id,count); //重定向回原来商品所在地址页面 resp.sendRedirect(req.getHeader("Referer")); } } }
ClientBookServlet
package com.atguigu.web; import com.atguigu.pojo.Book; import com.atguigu.pojo.Page; import com.atguigu.service.BookService; import com.atguigu.service.impl.BookServiceImpl; import com.atguigu.utils.WebUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ClientBookServlet extends BaseServlet{ private BookService bookService=new BookServiceImpl(); /** * 处理分页功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // System.out.println("经过了前台的ClientBookServlet程序"); //1 获取请求的参数 pageNo 和 pageSize int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1); int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE); //2 调用BookService.page(pageNo, pageSize): Page对象 Page<Book> page=bookService.page(pageNo,pageSize);//创建方法page page.setUrl("client/bookServlet?action=page"); //3 保存Page对象到Request域中 req.setAttribute("page",page); //4 请求转发到pages/manager/book_manager.jsp页面 req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp); } /** * 处理分页功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // System.out.println("经过了前台的ClientBookServlet程序"); //1 获取请求的参数 pageNo 和 pageSize int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1); int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE); int min=WebUtils.parseInt(req.getParameter("min"),0); int max=WebUtils.parseInt(req.getParameter("max"),Integer.MAX_VALUE); //2 调用BookService.page(pageNo, pageSize): Page对象 Page<Book> page=bookService.pageByPrice(pageNo,pageSize,min,max);//创建方法pageByPrice StringBuilder sb=new StringBuilder("client/bookServlet?action=pageByPrice"); // 如果有最小价格的参数,追加到分页条的地址参数中 if (req.getParameter("min")!=null){ sb.append("&min=").append(req.getParameter("min")); } if (req.getParameter("max")!=null){ sb.append("&max=").append(req.getParameter("max")); } page.setUrl(sb.toString()); //3 保存Page对象到Request域中 req.setAttribute("page",page); //4 请求转发到pages/manager/book_manager.jsp页面 req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp); } }
LoginServlet (被UserServlet取代)
package com.atguigu.web; import com.atguigu.pojo.User; import com.atguigu.service.UserService; import com.atguigu.service.impl.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class LoginServlet extends HttpServlet { private UserService userService=new UserServiceImpl(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); //2、调用XxxService.xxx()处理业务 User loginUser = userService.login(new User(null, username, password, null)); //如果等于null,说明登录失败 if (loginUser==null){ // 把错误信息,和回显的表单项信息,保存到Request域中 req.setAttribute("msg","用户名或密码错误!"); req.setAttribute("username",username); // 跳回登录页面 req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp); }else { // 成功 // 跳到成功页面login_success.jsp req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp); } } }
OrderServlet
package com.atguigu.web; import com.atguigu.pojo.Cart; import com.atguigu.pojo.Order; import com.atguigu.pojo.OrderItem; import com.atguigu.pojo.User; import com.atguigu.service.OrderService; import com.atguigu.service.impl.OrderServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; public class OrderServlet extends BaseServlet { private OrderService orderService = new OrderServiceImpl(); /** * 生成订单 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先获取Cart购物车对象 Cart cart = (Cart) req.getSession().getAttribute("cart"); //获取Userid User loginUser = (User) req.getSession().getAttribute("user"); if (loginUser == null) { req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp); return; } // System.out.println("OrderServlet 程序在["+Thread.currentThread().getName()+"]中"); Integer userId = loginUser.getId(); //调用orderservice.createorder(cart,userid);生成订单 String orderId = orderService.createOrder(cart, userId); // req.setAttribute("orderId",orderId); //请求转发至pages/cart/checkout.jsp // req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp); req.getSession().setAttribute("orderId", orderId); resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp"); } /** * 查看所有订单 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Order> showAllOrders = orderService.showAllOrders(); req.getSession().setAttribute("showAllOrders", showAllOrders); resp.sendRedirect(req.getContextPath() + "/pages/manager/order_manager.jsp"); } /** * 发货 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取orderId String orderId= req.getParameter("thisOrderId"); //调用 orderService.sendOrder(orderId); orderService.sendOrder(orderId); resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showAllOrder"); } /** * 查看订单详情 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取orderId String orderId= req.getParameter("thisOrderId"); //调用 orderService.showOrderDetail(orderId); List<OrderItem> orderItems = orderService.showOrderDetail(orderId); req.getSession().setAttribute("orderItems", orderItems); resp.sendRedirect(req.getContextPath() +"/pages/orderDetail/orderDetail.jsp"); } /** * 查看我的订单 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void showMyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取Userid User loginUser = (User) req.getSession().getAttribute("user"); if (loginUser == null) { req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp); return; } Integer userId = loginUser.getId(); //调用orderService.showMyOrders(userId); List<Order> showMyOrders = orderService.showMyOrders(userId); req.getSession().setAttribute("showMyOrders", showMyOrders); resp.sendRedirect(req.getContextPath() + "/pages/order/order.jsp"); } /** * 签收订单/确认收货 * * @param req * @param resp * @throws ServletException * @throws IOException */ public void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String orderId= req.getParameter("thisOrderId"); orderService.receiverOrder(orderId); resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showMyOrder"); } }
RegistServlet (被UserServlet取代)
package com.atguigu.web; import com.atguigu.pojo.User; import com.atguigu.service.UserService; import com.atguigu.service.impl.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RegistServlet extends HttpServlet { private UserService userService=new UserServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); // 2、检查验证码是否正确 ===写死,要求验证码为:abcde if ("abcde".equalsIgnoreCase(code)){ // 3、检查用户名是否可用 if (userService.existsUsername(username)){ System.out.println("用户名["+username+"]已存在!"); // 把回显信息保存到Request域中 req.setAttribute("msg","用户名已存在!!"); req.setAttribute("username",username); req.setAttribute("email",email); // 跳回注册页面 req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); }else { // 可用 调用Service保存到数据库 userService.registUser(new User(null,username,password,email)); // 跳到注册成功束面regist_success.jsp req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp); } }else { // 把回显信息保存到Request域中 req.setAttribute("msg","验证码错误!!"); req.setAttribute("username",username); req.setAttribute("email",email); System.out.println("验证码["+code+"]错误,"); req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); } } }
UserServlet
package com.atguigu.web; import com.atguigu.pojo.User; import com.atguigu.service.UserService; import com.atguigu.service.impl.UserServiceImpl; import com.atguigu.utils.WebUtils; import com.google.gson.Gson; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY; public class UserServlet extends BaseServlet { private UserService userService = new UserServiceImpl(); /** * 登出(注销) * @param req * @param resp * @throws ServletException * @throws IOException */ protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1、销毁Session中用户登录的信息(或者销毁Session) req.getSession().invalidate(); // 2、重定向到首页(或登录页面)。 resp.sendRedirect(req.getContextPath()); } /** * 处理登录的功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); //2、调用XxxService.xxx()处理业务 User loginUser = userService.login(new User(null, username, password, null)); //如果等于null,说明登录失败 if (loginUser==null){ // 把错误信息,和回显的表单项信息,保存到Request域中 req.setAttribute("msg","用户名或密码错误!"); req.setAttribute("username",username); // 跳回登录页面 req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp); }else { // 成功 //保存用户登录的信息 req.getSession().setAttribute("user",loginUser); // 跳到成功页面login_success.jsp req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp); } } /** * 处理注册的功能 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取Session中的验证码 String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY); //删除Session中的验证码 req.getSession().removeAttribute(KAPTCHA_SESSION_KEY); // 1、获取请求的参数 String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); // Map<String, String[]> parameterMap = req.getParameterMap(); // for (Map.Entry<String, String[]> entry:parameterMap.entrySet()) { // System.out.println(entry.getKey()+"="+ Arrays.asList(entry.getValue())); // } // User user=new User(); // WebUtils.copyParamToBean(req,user);//注入赋值,与User类中setXxx对应 User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());//注入赋值,与User类中setXxx对应 // 2、检查验证码是否正确 ===写死,要求验证码为:abcde if (token!=null&&token.equalsIgnoreCase(code)){ // 3、检查用户名是否可用 if (userService.existsUsername(username)){ System.out.println("用户名["+username+"]已存在!"); // 把回显信息保存到Request域中 req.setAttribute("msg","用户名已存在!!"); req.setAttribute("username",username); req.setAttribute("email",email); // 跳回注册页面 req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); }else { // 可用 调用Service保存到数据库 userService.registUser(new User(null,username,password,email)); // 跳到注册成功束面regist_success.jsp req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp); } }else { // 把回显信息保存到Request域中 req.setAttribute("msg","验证码错误!!"); req.setAttribute("username",username); req.setAttribute("email",email); System.out.println("验证码["+code+"]错误,"); req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp); } } protected void ajaxExistsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取请求的参数username String username=req.getParameter("username"); //调用userService.existsUsername(); boolean existsUsername = userService.existsUsername(username);//这里重命名一下原来的existUsername--》existsUsername //把返回的结果封装为map对象 Map<String,Object> resultMap=new HashMap<>(); resultMap.put("existsUsername",existsUsername); Gson gson=new Gson(); String json = gson.toJson(resultMap); resp.getWriter().write(json); } }
web
pages 框架
cart 框架
cart 代码
cart.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <script type="text/javascript"> $(function (){ //给删除绑定单击事件 $("a.deleteItem").click(function (){ return confirm("你确定要删除吗【"+$(this).parent().parent().find("td:first").text()+"】?"); }); //给清空购物车绑定单击事件 $("#clearCart").click(function (){ return confirm("你确定要清空购物车吗?"); }); //给输入框失去焦点事件 === onchange 内容发生改变事件 $(".updateCount").change(function (){ //获取商品名称 var name=$(this).parent().parent().find("td:first").text(); //获取商品id var id=$(this).attr("bookId"); //获取商品数量 var count=this.value; if (confirm("你确定要将【"+name+"】商品修改数量为:"+count+"吗?")){ //发起请求。给服务器保存修改 location.href="http://localhost:8080/book/cartServlet?action=updateCount&count="+count+"&id="+id; }else { //defaultValue属性是表单项Dom对象的属性。它表示默认的value属性值。 this.value=this.defaultValue; } }); }); </script> </head> <body> <div id="header"> <img class="logo_img" alt="" src="static/img/logo.gif"> <span class="wel_word">购物车</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>商品名称</td> <td>数量</td> <td>单价</td> <td>金额</td> <td>操作</td> </tr> <c:if test="${empty sessionScope.cart.items}"> <%-- 如果购物车为空的情况 --%> <td colspan="5"><a href="index.jsp"> 亲,当前购物车为空!快跟小伙伴们去浏览商品吧!!!</a></td> </c:if> <c:if test="${not empty sessionScope.cart.items}"> <%-- 如果购物车非空的情况 --%> <c:forEach items="${sessionScope.cart.items}" var="entry"> <tr> <td>${entry.value.name}</td> <td> <input class="updateCount" style="width: 80px" bookId="${entry.value.id}" type="text" value="${entry.value.count}"> </td> <td>${entry.value.price}</td> <td>${entry.value.totalPrice}</td> <td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${entry.value.id}">删除</a></td> </tr> </c:forEach> </c:if> </table> <c:if test="${not empty sessionScope.cart.items}"> <div class="cart_info"> <span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span> <span class="cart_span">总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span> <span class="cart_span"><a id="clearCart" href="cartServlet?action=clear">清空购物车</a></span> <span class="cart span"><a href="orderServlet?action=createOrder">去结账</a></span> </div> </c:if> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
checkout.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>结算页面</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">结算</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <div id="main"> <%-- <h1>你的订单已结算,订单号为:${requestScope.orderId}</h1>--%> <h1>你的订单已结算,订单号为:${sessionScope.orderId}</h1> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
client 框架
client 代码
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>书城首页</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <script type="text/javascript"> $(function (){ //给加入购物车按钮绑定单击事件 $("button.addToCart").click(function (){ /** * 在事件响应的function函数中,有一个this对象,这个this对象,是当前正在响应事件的dom对象 * @type {*|jQuery} */ var bookId= $(this).attr("bookId"); // location.href="http://localhost:8080/book/cartServlet?action=addItem&id="+bookId; //发AJAX请求,添加商品到购物车 $.getJSON("http://localhost:8080/book/cartServlet","action=ajaxAddItem&id="+bookId,function (data){ // console.log(data); $("#cartTotalCount").text("您的购物车中有"+data.totalCount+"件商品"); $("#cartLastName").text(data.lastName); }); }); }); </script> </head> <body> <div id="header"> <img class="logo_ing" alt="" src="static/img/logo.gif"> <span class= "wel_word">网上书城</span> <div> <%-- 如果用户还没有登录,显示【登录和注册的菜单】--%> <c:if test="${empty sessionScope.user}"> <a href="pages/user/login.jsp">登录</a> <a href="pages/user/regist.jsp">注册</a> </c:if> <%-- 如果已经登录,显示登录之后的用户信息--%> <c:if test="${not empty sessionScope.user}"> <span>欢迎<span class="um_span">${sessionScope.user.username}</span>光临尚硅谷书城</span> <a href="pages/order/order.jsp">我的订单</a> <a href="userServlet?action=logout">注销</a> </c:if> <a href="pages/cart/cart.jsp">购物车</a> <a href="pages/manager/manager.jsp">后台管理</a> </div> <div id="main"> <div id="book"> <div class="book_cond"> <form action="client/bookServlet" method="get"> <input type="hidden" name="action" value="pageByPrice"> 价格:<input id="min" type="text" name="min" value="${param.min}">元 - <input id="max" type="text" name="max" value="${param.max}">元 <input type="submit" value="查询"/> </form> </div> <div style="..."> <c:if test="${empty sessionScope.cart.items}"> <%-- 购物车为空的输出 --%> <span id="cartTotalCount"></span> <div> <span style="..." id="cartLastName">当前购物车为空</span> </div> </c:if> <c:if test="${not empty sessionScope.cart.items}"> <%-- 购物车非空的输出 --%> <span id="cartTotalCount">您的购物车中有${sessionScope.cart.totalCount}件商品</span> <div> 您刚刚将<span style="..." id="cartLastName">${sessionScope.lastName}</span>加入到了购物车中 </div> </c:if> </div> <div> <c:forEach items="${requestScope.page.items}" var="book"> <div class="b_list"> <div class="img_div"> <img class="book_img" alt="${book.imgPath}"/> </div> <div class="book_info"> <div class="book_name"> <span class="sp1">书名:</span> <span class="sp2">${book.name}</span> </div> <div class="book_author"> <span class="sp1">作者:</span> <span class="sp2">${book.author}</span> </div> <div class= "book_price"> <span class="sp1">价格:</span> <span class="sp2">¥${book.price}</span> </div> <div class="book_sales"> <span class="sp1">销量:</span> <span class="sp2">${book.sales}</span> </div> <div class="book_amount"> <span class="sp1">库存:</span> <span class="sp2">${book.stock}</span> </div> <div class="book_add"> <button bookId="${book.id}" class="addToCart">加入购物车:</button> </div> </div> </div> </c:forEach> </div> </div> <%-- 静态包含分页条 --%> <%@include file="/pages/common/page_nav.jsp"%> </div> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html> </body> </html>
common 框架
common 代码
footer.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 04:37 To change this template use File | Settings | File Template. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div id="bottom"> <span> 尚硅谷书城.Copyright ©2015 </span> </div>
head.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 03:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String basePath=request.getScheme() +"://" +request.getServerName() +":" +request.getServerPort() +request.getContextPath() +"/"; pageContext.setAttribute("basePath",basePath); %> <!--写base标签,永远固定相对路径跳转的结果--> <base href="<%=basePath%>"> <link type="text/css" rel="stylesheet" href="static/css/style.css"> <script type="text/javascript" src="static/script/jquery.js"></script>
login_sucess_menu.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 03:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div> <span>欢迎<span class="um_span">${sessionScope.user.username}</span>光临尚硅谷书城</span> <a href="pages/order/order.jsp">我的订单</a> <a href="userServlet?action=logout">注销</a> <a href="index.jsp">返回</a> </div>
manager_menu.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 05:38 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <div> <a href="manager/bookServlet?action=page">图书管理</a> <a href="orderServlet?action=showAllOrders">订单管理</a> <a href="index.jsp">返回商城</a> </div>
page_nav.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/24 Time: 上午 10:16 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%-- 分页条的开始--%> <div id="page_nav"> <%-- 大于首页,才显示--%> <c:if test="${requestScope.page.pageNo >1 }"> <a href="${requestScope.page.url}&pageNo=1">首页</a> <a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo-1}">上一页</a> </c:if> <%-- 页码输出的开始 --%> <c:choose> <%-- 情况1:如果总页码小于等于5的情况,页码的范围是:1-总页码--%> <c:when test="${requestScope.page.pageTotal <=5}"> <c:set var="begin" value="1"/> <c:set var="end" value="${requestScope.page.pageTotal}"/> <%-- <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i"> <c:if test="${i==requestScope.page.pageNo}"> 【${i}】 </c:if> <c:if test="${i!=requestScope.page.pageNo}"> <a href="${requestScope.page.Url}&pageNo=i">${i} </c:if> </c:forEach> --%> </c:when> <%-- 情况2:如果总页码大于5的情况。--%> <c:when test="${requestScope.page.pageTotal >5}"> <c:choose> <%-- 小情况1:当前页码为前面3个:1,2,3的情况,页码范围是:1-5.--%> <c:when test="${requestScope.page.pageNo <=3}"> <c:set var="begin" value="1"/> <c:set var="end" value="5"/> <%-- <c:forEach begin="1" end="${requestScope.page.pageTotal}" var="i"> <c:if test="${i==requestScope.page.pageNo}"> 【${i}】 </c:if> <c:if test="${i!=requestScope.page.pageNo}"> <a href="manager/bookServlet?action=page&pageNo=i">${i} </c:if> </c:forEach> --%> </c:when> <%-- 小情况2:当前页码为最后3个,8,9,10,页码范围是:总页码减4-总页码.--%> <c:when test="${requestScope.page.pageNo > requestScope.page.pageTotal-3}"> <c:set var="begin" value="${requestScope.page.pageTotal-4}"/> <c:set var="end" value="${requestScope.page.pageTotal}"/> <%-- <c:forEach begin="${requestScope.page.pageTotal-4}" end="${requestScope.page.pageTotal}" var="i"> <c:if test="${i==requestScope.page.pageNo}"> 【${i}】 </c:if> <c:if test="${i!=requestScope.page.pageNo}"> <a href="manager/bookServlet?action=page&pageNo=i">${i} </c:if> </c:forEach> --%> </c:when> <%-- 小情况3:4,5,6,7,页码范围是:当前页码减2-当前页码加2 --%> <c:otherwise> <c:set var="begin" value="${requestScope.page.pageNo-2}"/> <c:set var="end" value="${requestScope.page.pageNo+2}"/> <%-- <c:forEach begin="${requestScope.page.pageNo-2}" end="${requestScope.page.pageNo+2}" var="i"> <c:if test="${i==requestScope.page.pageNo}"> 【${i}】 </c:if> <c:if test="${i!=requestScope.page.pageNo}"> <a href="manager/bookServlet?action=page&pageNo=i">${i} </c:if> </c:forEach> --%> </c:otherwise> </c:choose> </c:when> </c:choose> <%-- 页码输出的结束 --%> <c:forEach begin="${begin}" end="${end}" var="i"> <c:if test="${i==requestScope.page.pageNo}"> 【${i}】 </c:if> <c:if test="${i!=requestScope.page.pageNo}"> <a href="${requestScope.page.url}&pageNo=${i}">${i} </c:if> </c:forEach> <%-- 如果已经是末页,不显示下一页,末页--%> <c:if test="${requestScope.page.pageNo < requestScope.page.pageTotal}"> <a href="${requestScope.page.url}&pageNo=${requestScope.page.pageNo+1}">下一页</a> <a href="${requestScope.page.url}&pageNo=${requestScope.page.pageTotal}">末页</a> </c:if> 共${requestScope.page.pageTotal}页,${requestScope.page.pageTotalCount}条记录 到第<input value="${param.pageNo}" name="pn" id="pn_input"/>页 <input id="searchPageBtn" type="button" value="确定"> <script type="text/javascript"> $(function (){ //跳到指定的页码 $("#searchPageBtn").click(function (){ var pageNo = $("#pn_input").val(); var pageTotal=${requestScope.page.pageTotal}; if(pageNo<1||pageNo>pageTotal){ alert("请输入正确的页码"); }else { //javaScript语言中提供了一个location地址栏对象 //它有一个属性叫href ,它可以获取浏览器地址栏中的地址 //href属性可读,可写 location.href="${pageScope.basePath}${requestScope.page.url}&pageNo="+pageNo; } }); }); </script> </div> <%-- 分页条的开始 --%>
error 框架
error 代码
error404.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误404</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> </head> <body> 很抱歉,您访问的页面不存在,或已经被删除!!!<br> <a href="index.jsp">返回首页</a> </body> </html>
error500.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>错误500</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> </head> <body> 很抱歉,您访问的后台程序出现了错误,程序员小哥,正在努力的为您抢修!!!<br> <a href="index.jsp">返回首页</a> </body> </html>
manager 框架
manager 代码
book_edit.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:46 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>编辑图书</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } h1 a{ color:red; } input { text-align: center; } </style> <body> <%-- 是否添加操作:${empty param.id?"add":"update"} <!---方案二 ---> --%> <%-- 是否添加操作:${empty requestScope.book?"add":"update"} <!---方案三 ---> --%> <div id="header"> <img class="logo_ing" alt="" arc="../../static/1mg/logo.gif"> <span class="wel_word">编辑图书</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <form action="manager/bookServlet" method="get"> <input type="hidden" name="pageNo" value="${param.pageNo}"> <%-- <input type="hidden" name="action" value="add"/> --%> <%-- <input type="hidden" name="action" value="${param.method}"/> <!---方案一 ---> --%> <%-- <input type="hidden" name="action" value="${empty param.id?"add":"update"}"/> <!---方案二 ---> --%> <input type="hidden" name="action" value="${empty requestScope.book?"add":"update"}"/><!---方案三 ---> <input type="hidden" name="id" value="${requestScope.book.id}"/> <table> <tr> <td>名称</td> <td>价格</td> <td>作者</td> <td>销量</td> <td>库存</td> <td colspan="2">操作</td> </tr> <tr> <td><input name="name" type="text" value="${requestScope.book.name}"/></td> <td><input name="price" type="text" value="${requestScope.book.price}"/></td> <td><input name="author" type="text" value="${requestScope.book.author}"/></td> <td><input name="sales" type="text" value="${requestScope.book.sales}"/></td> <td><input name="stock" type="text" value="${requestScope.book.stock}"/></td> <td><input type= "submit" value="提交"/></td> </tr> </table> </form> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
book_manager.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <title>图书管理</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <script type="text/javascript"> //给删除的a标签绑定单击事件,用于删除的确认提示操作 $(function (){ $("a.deleteClass").click(function (){ // 在事件的fuction函数中,有一个this对象。这个this对象,是当前正在响应事件的dom对象。 /** * confirm是确认提示框函数 * 参数是它的提示内容 * 他有两个按钮,一个确认,一个是取消。 * 返回true表示点击了,确认,返回false表示点击取消。 */ return confirm("你确定要删除【"+$(this).parent().parent().find("td:first").text()+"】?"); // return false;//阻止元素的默认行为===不提交请求 }); }); </script> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">图书管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>名称</td> <td>价格</td> <td>作者</td> <td>销量</td> <td>库存</td> <td colspan="2">操作</td> </tr> <c:forEach items="${requestScope.page.items}" var="book"> <tr> <td>${book.name}</td> <td>${book.price}</td> <td>${book.author}</td> <td>${book.sales}</td> <td>${book.stock}</td> <td><a href="manager/bookServlet?action=getBook&id=${book.id}&pageNo=${requestScope.page.pageNo}">修改</a> </td> <%-- <td><a href="manager/bookServlet?action=getBook&id=${book.id}&method=update">修改</a> </td> <!---方案一 ---> --%> <td><a class="deleteClass" href="manager/bookServlet?action=delete&id=${book.id}&pageNo=${requestScope.page.pageNo}">删除</a> </td> </tr> </c:forEach> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td><a href="pages/manager/book_edit.jsp?pageNo=${requestScope.page.pageNo}">添加图书</a> </td> <%-- <td><a href="pages/manager/book_edit.jsp?method=add">添加图书</a> </td> <!---方案一 ---> --%> </tr> </table> <%-- 静态包含分页条 --%> <%@include file="/pages/common/page_nav.jsp"%> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
manager.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>后台管理</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1{ text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">后台管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div> <h1>欢迎管理员进入后台管理系统</h1> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
order_manager.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <title>订单管理</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">订单管理系统</span> <%-- 静态包含manager 管理模块的菜单 --%> <%@ include file="/pages/common/manager_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>日期</td> <td>金额</td> <td>详情</td> <td>状态</td> </tr> <%-- ${sessionScope.showAllOrders}--%> <c:if test="${empty sessionScope.showAllOrders}"> <%-- 如果订单为空的情况 --%> <td colspan="4"> 亲,当前订单为空!</td> </c:if> <c:if test="${not empty sessionScope.showAllOrders}"> <c:forEach items="${sessionScope.showAllOrders}" var="order"> <tr> <td>${order.createTime}</td> <td>${order.price}</td> <td><a href="orderServlet?action=showOrderDetail&thisOrderId=${order.orderId}">查看详情</a></td> <c:if test="${order.status==0}"> <td>未发货 <a href="orderServlet?action=sendOrder&thisOrderId=${order.orderId}">发货</a> </td> </c:if> <c:if test="${order.status==1}"> <td>已发货</td> </c:if> <c:if test="${order.status==2}"> <td>已签收</td> </c:if> </tr> </c:forEach> </c:if> </table> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>
order 框架
order 代码
order.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/8/21 Time: 下午 02:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>我的订单</title> <%--静态包含 base标签,css样式,jquery文件 --%> <%@ include file="/pages/common/head.jsp"%> <style type="text/css"> h1 { text-align: center; margin-top: 200px; } </style> </head> <body> <div id="header"> <img class="logo_img" alt="" src="../../static/img/logo.gif"> <span class="wel_word">我的订单</span> <%-- 静态包含登录成功之后的菜单--%> <%@ include file="/pages/common/login_sucess_menu.jsp"%> </div> <div id="main"> <table> <tr> <td>日期</td> <td>金额</td> <td>状态</td> <td>详情</td> </tr> <%-- ${sessionScope.showMyOrders}--%> <c:if test="${empty sessionScope.showMyOrders}"> <%-- 如果订单为空的情况 --%> <td colspan="4"><a href="index.jsp"> 亲,当前订单为空!去首页看看吧!!!</a></td> </c:if> <c:if test="${not empty sessionScope.showMyOrders}"> <%-- 如果订单非空的情况 --%> <c:forEach items="${sessionScope.showMyOrders}" var="showMyOrder"> <tr> <td>${showMyOrder.createTime}</td> <td>${showMyOrder.price}</td> <c:if test="${showMyOrder.status==0}"> <td> 未发货 </td> </c:if> <c:if test="${showMyOrder.status==1}"> <td> 已发货 <a href="orderServlet?action=receiverOrder&thisOrderId=${showMyOrder.orderId}">收货</td> </td> </c:if> <c:if test="${showMyOrder.status==2}"> <td>已签收</td> </c:if> <td><a href="orderServlet?action=showOrderDetail&thisOrderId=${showMyOrder.orderId}">查看详情</a> </td> </tr> </c:forEach> </c:if> </table> </div> <%--静态包含页脚内容--%> <%@include file="/pages/common/footer.jsp"%> </body> </html>