700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)

菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)

时间:2019-01-31 22:58:47

相关推荐

菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)

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

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

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

web.xml

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

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

1 <beans xmlns="/schema/beans" 2 xmlns:context="/schema/context" 3 xmlns:mvc="/schema/mvc" xmlns:xsi="/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 /schema/beans 6 /schema/beans/spring-beans-3.0.xsd 7 /schema/context 8 /schema/context/spring-context-3.0.xsd 9 /schema/mvc10 /schema/mvc/spring-mvc-3.0.xsd">11 12<!--扫描控制器,当配置了它后,Spring会自动的到com.mjl.controller13下扫描带有@controller @service @component等注解等类,将他们自动实例化-->14<context:component-scan base-package="com.mjl.controller" />15 16<!--<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与17AnnotationMethodHandlerAdapter 两个bean,它解决了一些@controllerz注解使用时的提前配置-->18<mvc:annotation-driven />19 20<!--配置 页面控制器-->21<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">22 <property name="prefix" value="/"/>23 <property name="suffix" value=".jsp" />24 25</bean>26 27 </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用于实现我们所抽象出来的登录方法

1 package com.mjl.service; 2 3 import com.mjl.dao.IUserDao; 4 import com.mjl.model.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.annotation.Scope; 7 import org.springframework.stereotype.Service; 8 import org.springframework.ui.Model; 9 10 /**11 * Created by alvin on 15/9/7.12 */13 //@Service("UserService") 注解用于标示此类为业务层组件,在使用时会被注解的类会自动由14//spring进行注入,无需我们创建实例15 @Service("UserService")16 public class UserServiceImpl implements UserService {17//自动注入iuserdao 用于访问数据库18@Autowired19IUserDao Mapper;20 21//登录方法的实现,从jsp页面获取username与password22public boolean login(String username, String password) {23 // System.out.println("输入的账号:" + username + "输入的密码:" + password);24 //对输入账号进行查询,取出数据库中保存对信息25 User user = Mapper.selectByName(username);26 if (user != null) {27 // System.out.println("查询出来的账号:" + user.getUsername() + "密码:" + user.getPassword());28 // System.out.println("---------");29 if (user.getUsername().equals(username) && user.getPassword().equals(password))30 return true;31 32 }33 return false;34 35}36 }

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

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类 @AutowiredUserService 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";}}}

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

1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/7 5 Time: 下午10:05 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%10String path = request.getContextPath();11String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";12 %>13 <html>14 <head>15<title></title>16 </head>17 <body>18 <br>19 <br>20 <br>21 <br>22 <br>23 <form name="form1" action="/user/login.do" method="post" >24 <table width="300" border="1" align="center">25 <tr>26<td colspan="2">登入窗口</td>27 </tr>28 <tr>29 <td>用户名:</td>30 <td><input type="text" name="username">31 </td>32 </tr>33 <tr>34 <td>密码:</td>35 <td><input type="password" name="password"/>36 </td>37 </tr>38 <tr>39<td colspan="2">40 <input type="submit" name="submit" value="登录"/>41</td>42 43 44 </tr>45 </table>46 </form>47 </body>48 </html>

登入成功代码

1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/8 5 Time: 下午6:21 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%10 String path = request.getContextPath();11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";12 %>13 <html>14 <head>15<title></title>16 </head>17 <body>18 19 登入成功!20 <br>21 您好!${user.username}22 <br>23 <a href="/login.jsp">返回</a>24 </body>25 </html>

登入失败代码

1 <%-- 2 Created by IntelliJ IDEA. 3 User: alvin 4 Date: 15/9/8 5 Time: 下午6:22 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%10 String path = request.getContextPath();11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";12 %>13 <html>14 <head>15<title></title>16 </head>17 <body>18 登入失败!19 ${message}20 <br>21 <a href="<%=path%>/login.jsp">返回</a>22 </body>23 </html>

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。