User.java
package com. zr0701. 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.java
package com. zr0701. controller;
import com. zr0701. bean. User;
import com. zr0701. 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.java
package com. zr0701. dao;
import com. zr0701. bean. User;
public interface UserDao {
User findUserByName ( String name) ;
}
UserService.java
package com. zr0701. service;
public interface UserService {
boolean login ( String name, String password) ;
}
UserServiceImpl.java
package com. zr0701. service. Impl;
import com. zr0701. bean. User;
import com. zr0701. dao. UserDao;
import com. zr0701. 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 ;
}
}
UserMapper.xml
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "com.zr0701.dao.UserDao" >
< select id= "findUserByName" parameterType= "String" resultType= "User" >
select * from user where name = #{ name} ;
< / select>
< / mapper>
web.xml
< ? xml version= "1.0" encoding= "UTF-8" ? >
< web- app xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns= "http://xmlns.jcp.org/xml/ns/javaee"
xsi: schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version= "3.1" >
< ! -- 配置加载类路径的配置文件 -- >
< context- param>
< param- name> contextConfigLocation< / param- name>
< param- value> classpath* : applicationContext. xml< / param- value>
< / context- param>
< ! -- 配置监听器 -- >
< listener>
< listener- class > org. springframework. web. context. ContextLoaderListener< / listener- class >
< / listener>
< listener>
< listener- class > org. springframework. web. context. request. RequestContextListener< / listener- class >
< / listener>
< ! -- 解决中文乱码过滤器 -- >
< filter>
< filter- name> characterEncodingFilter< / filter- name>
< filter- class > org. springframework. web. filter. CharacterEncodingFilter< / filter- class >
< init- param>
< param- name> encoding< / param- name>
< param- value> UTF - 8 < / param- value>
< / init- param>
< / filter>
< filter- mapping>
< filter- name> characterEncodingFilter< / filter- name>
< url- pattern>
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>