菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)
昨天介绍了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">返回</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">返回</a>
</body>
</html>
OK,已经大功告成,跑一遍看看能不能使用吧
若我输入用户名:1234 密码:1234 则会提示登录失败,如下图所示:
到这里,本文已经全部结束,希望能对在整合springmvc,spring,my baits框架时有困惑的同学有所帮助,本文的代码已经上传github,地址为:http://git@github.com:alvin-mei/testssm.git 以后我也会慢慢的增加功能,也会上传相关代码,希望大家能够共同进步!
菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)的更多相关文章
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(上)
由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教. 一 ...
- Springmvc+Spring+Mybatis整合开发(架构搭建)
Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- SpringMVC+Spring+Mybatis整合
SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)
在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,以下就仅仅贴出各层实现功能的代码: Jsp页面实现功能的js代码例如以下: <script ...
- mybatis学习(十一)——springmvc++spring+mybatis整合
做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...
- 2.springMVC+spring+Mybatis整合
前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...
- JavaWeb_(SpringMVC框架)SpringMVC&Spring&MyBatis整合
JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合 传送门 1.整合ssm 3大框架 过程 a)导包 -> spring_Ja ...
随机推荐
- java学习——入门扫盲篇
概要 近期这几天開始进入java的学习,接触到了好多不是非常了解的概念,像JDK.JRE.JVM.GC等等这些,放到这里来进行下扫盲. java java是一种面向对象程序设计语言和java平台的总称 ...
- 外设:K9F2G08 nandflash 底层读写、控制驱动程序,可随机读写
/****************************************************************************** Copyright (C), 2001- ...
- .net 弹窗方式
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请输入 ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )
因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...
- .NET通过PowerShell操作ExChange为用户开通邮箱账号
最近工作中一个web项目需要集成exchange邮箱服务,注册用户时需要动态创建邮箱用户,终于在http://www.cnblogs.com/gongguo/archive/2012/03/12/23 ...
- vmware时间不同步的问题
请以管理员身份运行(root)
- django-crispy-forms入门指南
django-crispy-forms 是对django form在html页面呈现方式进行管理的一个第三方插件. 配置: 在INSTALLED_APPS中加入'crispy_forms' djang ...
- .NET Core & ASP.NET Core 1.0
.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布 众所周知,Red Hat和微软正在努力使.NET Core成为Red Hat企业版Linux (RHEL) ...
- linux查看端口和进程
查看进程 ps -aux | grep appname 杀死进程 kill pid 查看端口: netstat -ap | grep 端口号 netstat -ap | grep 进程名字 lsof ...