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>