【JAVA】ssm框架搭建登录实现
2020-07-16 06:08
204 查看
【JAVA】ssm框架搭建登录实现
- bean下的User.java
- controller下的UserController.java
- dao下的UserDao.java
- service
- UserService.java
- Impl下的UserServiceImpl.java
前言
Java项目实习第五次课后作业:整理上课内容,实现登录功能。
介绍Spring
前期准备
载入Tomcat
设置Maven
Project整体
Java包
bean下的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 + '\'' + '}'; } }
controller下的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; } }
dao下的UserDao.java
package com.zr0701.dao; import com.zr0701.bean.User; public interface UserDao { User findUserByName(String name); }
service
UserService.java
package com.zr0701.service; public interface UserService { boolean login(String name,String password); }
Impl下的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; } }
resources包
mapper下的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>
webapp包
WEB-INF下的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>/*</url-pattern> </filter-mapping> <!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!-- 服务器启动的时候,让DispatcherServlet对象创建 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
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>
效果图
数据库user表数据
登录界面
输入已有账户即可,成功出现成功网页,反之为失败页面。
相关文章推荐
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- myeclipse+mybatis-generator-gui-0.6.1快速搭建ssm框架并且实现登录
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- ssm框架搭建登录实现
- ssm框架搭建登录实现
- MyEclipse环境下搭建SSM框架实现登录功能
- IntelliJ IDEA搭建SSM框架并实现页面登录功能连接数据库
- 【JAVA】ssm框架搭建修改、定向查询实现
- 使用SSM(Spring+SpringMVC+Mybatis)框架搭建服务器实现登录验证
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 菜鸟学SSM框架搭建----Spring+SpringMVC+MyBatis整合,实现简单的登录功能
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- Java SSM框架之 springMVC 框架实现用户登录
- 自己写的JAVA实现登录限制原理(SSM框架)
- 看别人SSM框架搭建Web服务器实现登录功能的改进后实现
- JAVA中SSM框架的搭建实现CRUD的方法
- NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证
- 搭建开发框架Express,实现Web网站登录验证