ssm框架搭建登录实现

ssm框架搭建登录实现


## User

package com.zp0701.bean;

public class User {

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}


## UserController

package com.zp0701.controller;

import com.zp0701.bean.User;
import com.zp0701.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        boolean flag = userService.login(user.getName(),user.getPassword());
        ModelAndView modelAndView = new ModelAndView();
        if(flag){
            modelAndView.setViewName("../ok");
        }else {
            modelAndView.setViewName("../failure");
        }
        return modelAndView;
    }
}


## UserDao

package com.zp0701.dao;

import com.zp0701.bean.User;

public interface UserDao {
    User findUserByName(String name);
}


## UserServiceImpl

package com.zp0701.service.Impl;

import com.zp0701.bean.User;
import com.zp0701.dao.UserDao;
import com.zp0701.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean login(String name, String password){
        User user = userDao.findUserByName(name);
        if(user != null && user.getPassword().equals(password)){
            return true;
        }
        return false;
    }

}


## UserService

package com.zp0701.service;

public interface UserService {

    boolean login(String name,String password);

}


## jsp index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/user/login.do" method="post">
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    <input type="submit" value="login">
</form>
</body>
</html>

## ok.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功!</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>


## failure.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>失败!</title>
</head>
<body>
<h1>失败</h1>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值