SpringMVC+Spring+Mybatis实现最简单的登录验证(3)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.cn.ft.dao.AccountDao">
<select parameterType="int" resultMap="accountMap">
select * from account where id = #{id}
</select>
<select parameterType="string" resultMap="accountMap">
select * from account where acc = #{name}
</select>
<resultMap type="com.cn.ft.entity.Account">
<id property="id" column="id" />
<result property="user" column="acc" />
<result property="paw" column="password" />
<result property="age" column="age" />
</resultMap>

</mapper>

AccountController.java

SpringMVC+Spring+Mybatis实现最简单的登录验证

1 package com.cn.ft.controller; 2 3 import javax.annotation.Resource; 4 import javax.servlet.http.HttpServletRequest; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.cn.ft.dao.AccountDao; 11 import com.cn.ft.entity.Account; 12 13 @Controller 14 @RequestMapping("/account") 15 public class AccountController { 16 @Resource 17 private AccountDao dao; 18 19 @RequestMapping("/showAccount") 20 public String toShowAccount(HttpServletRequest request, Model model) { 21 String user = request.getParameter("user"); 22 String paw = request.getParameter("paw"); 23 if (user == null || user == "") { 24 model.addAttribute("msg", "账号为空"); 25 return "login"; 26 } 27 Account account = dao.getByName(user); 28 if (!account.getPaw().equals(paw)) { 29 model.addAttribute("msg", "密码错误"); 30 return "login"; 31 } 32 model.addAttribute("acc", account); 33 return "showAccount"; 34 } 35 36 @RequestMapping("/toLogin") 37 public String toLogin() { 38 return "login"; 39 } 40 }

AccountController.java

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee "
version="3.0">
<display-name>web</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 添加对SpringMVC的支持 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

4.数据库

数据库字段以及数据信息:

SpringMVC+Spring+Mybatis实现最简单的登录验证

5.页面

login.jsp

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14335.html