最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)

 更新時(shí)間:2022年09月06日 09:29:44   作者:alvin-m  
這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

昨天介紹了mybatis與spring的整合,今天我們完成剩下的springmvc的整合工作。

要整合springmvc首先得在web.xml中配置springmvc的前端控制器DispatcherServlet,它是springmvc的核心,為springmvc提供集中訪問(wèn)點(diǎn),springmvc對(duì)頁(yè)面的分派與調(diào)度功能主要靠它完成。

在我們之前配置的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>
   <!--用于標(biāo)明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 的請(qǐng)求,交給DispatcherServlet處理,性能最好 -->
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
  <!--用于設(shè)定默認(rèn)首頁(yè)-->
 <welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>

配置完后,我們需要在對(duì)springmvc框架進(jìn)行配置,配置文件名為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">

 <!--掃描控制器,當(dāng)配置了它后,Spring會(huì)自動(dòng)的到com.mjl.controller
 下掃描帶有@controller @service @component等注解等類,將他們自動(dòng)實(shí)例化-->
 <context:component-scan base-package="com.mjl.controller" />

 <!--<mvc:annotation-driven /> 會(huì)自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping與
 AnnotationMethodHandlerAdapter 兩個(gè)bean,它解決了一些@controllerz注解使用時(shí)的提前配置-->
 <mvc:annotation-driven />

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

 </bean>

</beans>

當(dāng)springmvc配置完成后,就需要編寫業(yè)務(wù)啦,也就是service包下的東西,首先編寫一個(gè)接口類userservice,里面存放了我們抽象出來(lái)的登錄方法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);
}

然后在創(chuàng)建一個(gè)userservice的實(shí)現(xiàn)類userserviceimpl用于實(shí)現(xiàn)我們所抽象出來(lái)的登錄方法

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") 注解用于標(biāo)示此類為業(yè)務(wù)層組件,在使用時(shí)會(huì)被注解的類會(huì)自動(dòng)由
 //spring進(jìn)行注入,無(wú)需我們創(chuàng)建實(shí)例
@Service("UserService")
public class UserServiceImpl implements UserService {
 //自動(dòng)注入iuserdao 用于訪問(wèn)數(shù)據(jù)庫(kù)
 @Autowired
 IUserDao Mapper;

 //登錄方法的實(shí)現(xiàn),從jsp頁(yè)面獲取username與password
 public boolean login(String username, String password) {
//  System.out.println("輸入的賬號(hào):" + username + "輸入的密碼:" + password);
  //對(duì)輸入賬號(hào)進(jìn)行查詢,取出數(shù)據(jù)庫(kù)中保存對(duì)信息
  User user = Mapper.selectByName(username);
  if (user != null) {
//   System.out.println("查詢出來(lái)的賬號(hào):" + user.getUsername() + "密碼:" + user.getPassword());
//   System.out.println("---------");
   if (user.getUsername().equals(username) && user.getPassword().equals(password))
    return true;

  }
  return false;

 }
}

編寫完業(yè)務(wù)層代碼后,我們就可以寫控制層代碼啦,控制層的代碼用于處理頁(yè)面提交的業(yè)務(wù)

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注解用于標(biāo)示本類為web層控制組件
@Controller
//@RequestMapping("/user")用于標(biāo)定訪問(wèn)時(shí)對(duì)url位置
@RequestMapping("/user")
//在默認(rèn)情況下springmvc的實(shí)例都是單例模式,所以使用scope域?qū)⑵渥⒔鉃槊看味紕?chuàng)建一個(gè)新的實(shí)例
@Scope("prototype")
public class UserController {
 //自動(dòng)注入業(yè)務(wù)層的userService類
 @Autowired
  UserService userService;

 //login業(yè)務(wù)的訪問(wèn)位置為/user/login
 @RequestMapping("/login")
  public String login(User user,HttpServletRequest request){
  //調(diào)用login方法來(lái)驗(yàn)證是否是注冊(cè)用戶
  boolean loginType = userService.login(user.getUsername(),user.getPassword());
  if(loginType){
   //如果驗(yàn)證通過(guò),則將用戶信息傳到前臺(tái)
   request.setAttribute("user",user);
   //并跳轉(zhuǎn)到success.jsp頁(yè)面
   return "success";
  }else{
   //若不對(duì),則將錯(cuò)誤信息顯示到錯(cuò)誤頁(yè)面
   request.setAttribute("message","用戶名密碼錯(cuò)誤");
   return "error";
  }
 }

}

控制層代碼寫完后,就可以進(jìn)行前端頁(yè)面代碼編寫了,登錄代碼

<%--
 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,已經(jīng)大功告成,跑一遍看看能不能使用吧

若我輸入用戶名:1234 密碼:1234 則會(huì)提示登錄失敗,如下圖所示:

到這里,本文已經(jīng)全部結(jié)束,希望能對(duì)在整合springmvc,spring,my baits框架時(shí)有困惑的同學(xué)有所幫助,本文的代碼已經(jīng)上傳github,以后我也會(huì)慢慢的增加功能,也會(huì)上傳相關(guān)代碼,希望大家能夠共同進(jìn)步!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java經(jīng)典排序算法之插入排序代碼實(shí)例

    Java經(jīng)典排序算法之插入排序代碼實(shí)例

    這篇文章主要介紹了Java經(jīng)典排序算法之插入排序代碼實(shí)例,插入排序是一種最簡(jiǎn)單直觀的排序算法,它的工作原理是通過(guò)構(gòu)建有序序列,對(duì)于未排序數(shù)據(jù),在已排序序列中從后向前掃描,找到相應(yīng)位置并插入,需要的朋友可以參考下
    2023-10-10
  • Java學(xué)習(xí)之Lambda表達(dá)式的使用詳解

    Java學(xué)習(xí)之Lambda表達(dá)式的使用詳解

    Lambda表達(dá)式是Java SE 8中一個(gè)重要的新特性,允許通過(guò)表達(dá)式來(lái)代替功能接口。本文將通過(guò)一些簡(jiǎn)單的示例和大家講講Lamda表達(dá)式的使用,感興趣的可以了解一下
    2022-12-12
  • java實(shí)現(xiàn)多人聊天對(duì)話室

    java實(shí)現(xiàn)多人聊天對(duì)話室

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多人聊天對(duì)話室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案

    SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案

    在SpringBoot中靜態(tài)方法調(diào)用Spring容器bean時(shí)出現(xiàn)的null值問(wèn)題,本文就來(lái)介紹一下SpringBoot靜態(tài)方法調(diào)用Spring容器bean的三種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • java.security.egd?作用詳解

    java.security.egd?作用詳解

    這篇文章主要為大家介紹了java.security.egd作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java集合案例之斗地主游戲

    Java集合案例之斗地主游戲

    這篇文章主要為大家詳細(xì)介紹了Java集合案例之斗地主游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java 反射機(jī)制原理與用法詳解

    Java 反射機(jī)制原理與用法詳解

    這篇文章主要介紹了Java 反射機(jī)制原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了java反射機(jī)制的相關(guān)概念、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-12-12
  • SpringBoot中Starter的作用小結(jié)

    SpringBoot中Starter的作用小結(jié)

    這篇文章主要介紹了SpringBoot中Starter的作用小結(jié),Starter其實(shí)就是Spring針對(duì)不用的開(kāi)發(fā)場(chǎng)景,給我們提供的“套餐”。今天就通過(guò)實(shí)例代碼給大家介紹Starter,感興趣的朋友一起看看吧
    2021-10-10
  • 基于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)據(jù)同步組件

    基于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)據(jù)同步組件

    這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)據(jù)同步組件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下
    2023-06-06
  • IDEA SpringBoot:Cannot resolve configuration property配置文件問(wèn)題

    IDEA SpringBoot:Cannot resolve configuration&

    這篇文章主要介紹了IDEA SpringBoot:Cannot resolve configuration property配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

满洲里市| 青阳县| 永登县| 黑龙江省| 大同市| 华池县| 霸州市| 兴和县| 栾川县| 犍为县| 瑞安市| 邳州市| 宝兴县| 鹤岗市| 平定县| 宕昌县| 临颍县| 甘孜县| 安图县| 宁明县| 富源县| 曲周县| 平阳县| 林周县| 酉阳| 定远县| 盐津县| 黄浦区| 定结县| 柳江县| 蓝田县| 南和县| 寿阳县| 搜索| 台中市| 灌南县| 临海市| 思茅市| 循化| 大方县| 清流县|