• SSM登陆


    简单的SSM登陆

    • jsp
     1 <form action="${pageContext.request.contextPath }/user/login.action">
     2 <label>用户名:</label>
     3 <br>
     4 <input type="text" name="username">
     5 <br>
     6 <label>密码:</label>
     7 <br>
     8 <input type="password" name="password">
     9 <br>
    10 <input type="submit" value="登陆">
    11 
    12 </form>
    • 跳转为/user/login.action
    • controller层代码
     1 @Controller
     2     @RequestMapping("/user")
     3     public class UserController {
     4 //跳转登陆界面
     5     @Autowired
     6     private LoginService loginService;
     7     
     8         @RequestMapping("/toLogin")
     9         public String toLogin() {        
    10             return "login";
    11         }
    12 //登陆操作
    13         @RequestMapping("login")
    14         public String login(String username, String password, HttpSession session) {
    15             
    16             //if(username!=null&&!("".equals(username))) {
    17             
    18             // 校验用户登录
    19             System.out.println(username);
    20             System.out.println(password);
    21             // 把用户名放到session中
    22             session.setAttribute("username", username);
    23             Map<String, String> parameters = new HashMap<String, String>();
    24             parameters.put("username", username);
    25             parameters.put("password", password);
    26             Login user = this.loginService.selectUserByUsernameAndPassword(parameters);
    27             if (user != null) {
    28                 System.out.println("你真他妈的是个人才,竟然猜对了!!");
    29                 return "redirect:/item/itemList.action";
    30             } else {
    31                 System.out.println("去你妈的!账号输错了!!");
    32                 return "redirect:/user/toLogin.action";
    33                 
    34             }
    35         
    36             
    37             }
    • Service
    1 public interface LoginService {
    2     Login selectUserByUsernameAndPassword(Map<String, String> parameters);
    3 }
    • ServiceImpl
     1 @Service
     2 public class LoginServiceImpl implements LoginService {
     3     @Autowired
     4     private LoginMapper loginMapper;
     5 
     6     @Override
     7     public Login selectUserByUsernameAndPassword(Map<String, String> parameters) {
     8         // TODO Auto-generated method stub
     9         return this.loginMapper.findWithLoginAndPassword(parameters);
    10     }
    11 
    12     
    13 }
    • Mapper层
    1 Login findWithLoginAndPassword(Map<String, String> parameters);
    • Mapperxml
    1 <select id="findWithLoginAndPassword" resultMap="BaseResultMap" parameterType="cn.itcast.ssm.pojo.LoginExample" >
    2     select * from login where username=#{username} and password=#{password}
    3   </select>
    • 实体类
     1 public class Login {
     2     private Integer id;
     3 
     4     private String username;
     5 
     6     private String password;
     7 
     8     public Integer getId() {
     9         return id;
    10     }
    11 
    12     public void setId(Integer id) {
    13         this.id = id;
    14     }
    15 
    16     public String getUsername() {
    17         return username;
    18     }
    19 
    20     public void setUsername(String username) {
    21         this.username = username == null ? null : username.trim();
    22     }
    23 
    24     public String getPassword() {
    25         return password;
    26     }
    27 
    28     public void setPassword(String password) {
    29         this.password = password == null ? null : password.trim();
    30     }
    31 }
    •  数据库

    • 测试

     

     

     记得写注册和登陆加密呀!!!!!!!

  • 相关阅读:
    jquery 读取 xml 属性等于某值的 方法
    jquery 定时器
    jquery div 滚动条 最底部
    ajax success 不能返回值解决方案 async:false
    wiki 使用说明
    thinkphp 二维码封装函数
    100本书 慢慢来读
    2013 来了
    jquery 解析 xml
    键盘按键 事件
  • 原文地址:https://www.cnblogs.com/pansin/p/11923375.html
Copyright © 2020-2023  润新知