springmvc+spring+mybatis实现用户登录功能(下)

发布时间 - 2026-01-11 02:16:13    点击率:

昨天介绍了mybatis与spring的整合,今天我们完成剩下的springmvc的整合工作。

要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherServlet,它是springmvc的核心,为springmvc提供集中访问点,springmvc对页面的分派与调度功能主要靠它完成。

在我们之前配置的web.xml中加入以下springmvc的配置:

web.xml

<!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <!--用于标明spring-mvc.xml配置的位置,我是存放在config文件夹下-->
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:config/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <!-- 拦截所有*.do 的请求,交给DispatcherServlet处理,性能最好 -->
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
  <!--用于设定默认首页-->
 <welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>

配置完后,我们需要在对springmvc框架进行配置,配置文件名为spring-mvc.xml,也是存放在config文件夹下:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <!--扫描控制器,当配置了它后,Spring会自动的到com.mjl.controller
 下扫描带有@controller @service @component等注解等类,将他们自动实例化-->
 <context:component-scan base-package="com.mjl.controller" />

 <!--<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与
 AnnotationMethodHandlerAdapter 两个bean,它解决了一些@controllerz注解使用时的提前配置-->
 <mvc:annotation-driven />

 <!--配置 页面控制器-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"/>
  <property name="suffix" value=".jsp" />

 </bean>

</beans>

当springmvc配置完成后,就需要编写业务啦,也就是service包下的东西,首先编写一个接口类userservice,里面存放了我们抽象出来的登录方法login

package com.mjl.service;

import org.springframework.ui.Model;

/**
 * Created by alvin on 15/9/7.
 */
public interface UserService {
 public boolean login(String username,String password);
}

然后在创建一个userservice的实现类userserviceimpl用于实现我们所抽象出来的登录方法

package com.mjl.service;

import com.mjl.dao.IUserDao;
import com.mjl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

/**
 * Created by alvin on 15/9/7.
 */
//@Service("UserService") 注解用于标示此类为业务层组件,在使用时会被注解的类会自动由
 //spring进行注入,无需我们创建实例
@Service("UserService")
public class UserServiceImpl implements UserService {
 //自动注入iuserdao 用于访问数据库
 @Autowired
 IUserDao Mapper;

 //登录方法的实现,从jsp页面获取username与password
 public boolean login(String username, String password) {
//  System.out.println("输入的账号:" + username + "输入的密码:" + password);
  //对输入账号进行查询,取出数据库中保存对信息
  User user = Mapper.selectByName(username);
  if (user != null) {
//   System.out.println("查询出来的账号:" + user.getUsername() + "密码:" + user.getPassword());
//   System.out.println("---------");
   if (user.getUsername().equals(username) && user.getPassword().equals(password))
    return true;

  }
  return false;

 }
}

编写完业务层代码后,我们就可以写控制层代码啦,控制层的代码用于处理页面提交的业务

package com.mjl.controller;

import com.mjl.model.User;
import com.mjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by alvin on 15/9/7.
 */

//@Controller注解用于标示本类为web层控制组件
@Controller
//@RequestMapping("/user")用于标定访问时对url位置
@RequestMapping("/user")
//在默认情况下springmvc的实例都是单例模式,所以使用scope域将其注解为每次都创建一个新的实例
@Scope("prototype")
public class UserController {
 //自动注入业务层的userService类
 @Autowired
  UserService userService;

 //login业务的访问位置为/user/login
 @RequestMapping("/login")
  public String login(User user,HttpServletRequest request){
  //调用login方法来验证是否是注册用户
  boolean loginType = userService.login(user.getUsername(),user.getPassword());
  if(loginType){
   //如果验证通过,则将用户信息传到前台
   request.setAttribute("user",user);
   //并跳转到success.jsp页面
   return "success";
  }else{
   //若不对,则将错误信息显示到错误页面
   request.setAttribute("message","用户名密码错误");
   return "error";
  }
 }

}

控制层代码写完后,就可以进行前端页面代码编写了,登录代码

<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/7
 Time: 下午10:05
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<form name="form1" action="/user/login.do" method="post" >
 <table width="300" border="1" align="center">
 <tr>
 <td colspan="2">登入窗口</td>
 </tr>
 <tr>
  <td>用户名:</td>
  <td><input type="text" name="username">
  </td>
 </tr>
 <tr>
  <td>密码:</td>
  <td><input type="password" name="password"/>
  </td>
 </tr>
 <tr>
 <td colspan="2">
  <input type="submit" name="submit" value="登录"/>
 </td>


 </tr>
 </table>
</form>
</body>
</html>

登入成功代码

<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:21
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>

登入成功!
<br>
您好!${user.username}
<br>
<a href="/login.jsp" rel="external nofollow" >返回</a>
</body>
</html>

登入失败代码

 
<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:22
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
 <title></title>
</head>
<body>
登入失败!
${message}
<br>
<a href="<%=path%>/login.jsp" rel="external nofollow" >返回</a>
</body>
</html>

OK,已经大功告成,跑一遍看看能不能使用吧

若我输入用户名:1234 密码:1234 则会提示登录失败,如下图所示:

到这里,本文已经全部结束,希望能对在整合springmvc,spring,my baits框架时有困惑的同学有所帮助,本文的代码已经上传github,以后我也会慢慢的增加功能,也会上传相关代码,希望大家能够共同进步!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# springmvc  # spring  # mybatis  # 登录  # springsecurity实现用户登录认证快速使用示例代码(前后端分离项目)  # Springboot+Shiro记录用户登录信息并获取当前登录用户信息的实现代码  # Spring mvc 实现用户登录的方法(拦截器)  # spring aop action中验证用户登录状态的实例代码  # springmvc+spring+mybatis实现用户登录功能(上)  # SpringMvc实现简易计算器功能  # Spring实现加法计算器和用户登录功能  # 登入  # 放在  # 下午  # 完后  # 创建一个  # 则将  # 就可以  # 都是  # 我是  # 上传  # 也会  # 将其  # 它是  # 本类  # 一遍  # 注册用户  # 此类  # 大功告成  # 写了  # 希望大家 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何实现用户密码重置功能?(完整流程代码)  高防服务器如何保障网站安全无虞?  Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用  如何在万网自助建站平台快速创建网站?  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  如何将凡科建站内容保存为本地文件?  佛山网站制作系统,佛山企业变更地址网上办理步骤?  如何在万网自助建站中设置域名及备案?  微信公众帐号开发教程之图文消息全攻略  Laravel怎么为数据库表字段添加索引以优化查询  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  Win11怎么设置虚拟桌面 Win11新建多桌面切换操作【技巧】  详解CentOS6.5 安装 MySQL5.1.71的方法  瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口  Laravel Fortify是什么,和Jetstream有什么关系  Python文件流缓冲机制_IO性能解析【教程】  制作电商网页,电商供应链怎么做?  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  北京的网站制作公司有哪些,哪个视频网站最好?  如何在香港服务器上快速搭建免备案网站?  韩国服务器如何优化跨境访问实现高效连接?  Win11怎样安装网易有道词典_Win11安装词典教程【步骤】  Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧  高端建站三要素:定制模板、企业官网与响应式设计优化  javascript中的数组方法有哪些_如何利用数组方法简化数据处理  想要更高端的建设网站,这些原则一定要坚持!  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  微信小程序 wx.uploadFile无法上传解决办法  Claude怎样写约束型提示词_Claude约束提示词写法【教程】  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  Laravel如何生成API文档?(Swagger/OpenAPI教程)  Laravel如何使用Livewire构建动态组件?(入门代码)  Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】  Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】  如何在阿里云服务器自主搭建网站?  Laravel中间件起什么作用_Laravel Middleware请求生命周期与自定义详解  PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑  javascript事件捕获机制【深入分析IE和DOM中的事件模型】  Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  Laravel如何配置任务调度?(Cron Job示例)  Laravel如何生成PDF或Excel文件_Laravel文档导出工具与使用教程  微信h5制作网站有哪些,免费微信H5页面制作工具?  PHP正则匹配日期和时间(时间戳转换)的实例代码  深圳网站制作的公司有哪些,dido官方网站?  昵图网官网入口 昵图网素材平台官方入口  CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】  电商网站制作价格怎么算,网上拍卖流程以及规则?  创业网站制作流程,创业网站可靠吗?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?