SSM购物车之用户模块day03
1.SQL
1 create table user( 2 id int, 3 name varchar(50), 4 password varchar(50) 5 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 6 7 insert into user values(1,'tom','123');
2.实体类
1 package cn.gb.pojo; 2 3 public class User { 4 private int id; 5 private String name; 6 private String password; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public String getPassword() { 25 return password; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 }
3.Mapper层(数据访问层)
1 package cn.gb.mapper; 2 3 import cn.gb.pojo.User; 4 5 public interface UserMapper { 6 7 User login(User user); 8 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="cn.gb.mapper.UserMapper"> 6 <!--User login(User user);--> 7 <select id="login" parameterType="User" resultType="User"> 8 SELECT * FROM USER WHERE name=#{name} AND password=#{password} 9 </select> 10 </mapper>
4.Service层(业务实现层)
1 package cn.gb.service; 2 3 import cn.gb.mapper.UserMapper; 4 import cn.gb.pojo.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 @Service 10 @Transactional 11 public class UserService { 12 @Autowired 13 private UserMapper userMapper; 14 public User login(User user){ 15 return userMapper.login(user); 16 } 17 18 19 }
5.Controller层()
如果对象不为空,就表示账号密码正确,跳转到产品显示界面 /listProduct
如果对象为空,就跳转到登陆界面,重新登陆
1 package cn.gb.controller; 2 3 import cn.gb.pojo.User; 4 import cn.gb.service.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import javax.servlet.http.HttpSession; 11 12 @Controller 13 @RequestMapping("user") 14 public class UserController { 15 @Autowired 16 private UserService userService; 17 @RequestMapping("login") 18 public String login(User user, Model model, HttpSession session){ 19 User luser=userService.login(user); 20 if (null!=luser){ 21 session.setAttribute("user",luser); 22 return "redirect:/product/info.action"; 23 } 24 else { 25 model.addAttribute("lm","用户名或密码错误"); 26 return "/login.jsp"; 27 } 28 } 29 }
6.jsp页面
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2021/2/2 0002 5 Time: 14:40 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page language="java" contentType="text/html; charset=UTF-8" 9 pageEncoding="UTF-8" %> 10 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 11 <html> 12 <head> 13 <title>Title</title> 14 <script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/> 15 <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script> 16 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> 17 </head> 18 <body> 19 <div class="col-md-7"> 20 <img src="${pageContext.request.contextPath}/image/3.jpg" alt=""> 21 <div style="color: red"> 22 ${lm} 23 </div> 24 </div> 25 <div class="col-md-5"> 26 <div 27 style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;"> 28 <form class="form-horizontal" action="${pageContext.request.contextPath}/user/login.action" method="post"> 29 <div class="form-group"> 30 <label for="name" class="col-sm-2 control-label">用户名</label> 31 <div class="col-sm-6"> 32 <input type="text" class="form-control" id="name" name="name" 33 placeholder="请输入用户名"> 34 </div> 35 </div> 36 <div class="form-group"> 37 <label for="input3" class="col-sm-2 control-label">用户名</label> 38 <div class="col-sm-6"> 39 <input type="password" class="form-control" id="input3" name="password" 40 placeholder="请输入密码"> 41 </div> 42 </div> 43 <div class="form-group"> 44 <div class="col-sm-offset-2 col-sm-6"> 45 <input type="submit" value="登录"> 46 </div> 47 </div> 48 49 </form> 50 </div> 51 </body> 52 </html>
在本次页面中我加入了图片,因为我没有在springmvc.xml和web.xml配置相关的静态资源,所以springmvc拦截了静态资源文件导致加载不出来。
所以我在web.xml进行了修改,只对.action进行过滤即 <url-pattern>*.action</url-pattern>