springmvc+spring+mybatis实现用户登录功能(下)
程序员文章站
2023-12-11 08:03:46
昨天介绍了,今天我们完成剩下的springmvc的整合工作。
要整合springmvc首先得在web.xml中配置springmvc的前端控制器dispatcherser...
昨天介绍了,今天我们完成剩下的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,以后我也会慢慢的增加功能,也会上传相关代码,希望大家能够共同进步!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。