欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Spring IOC技術(shù)實(shí)現(xiàn)用戶登錄驗(yàn)證機(jī)制

 更新時(shí)間:2016年10月18日 13:53:02   作者:Hosens  
這篇文章主要為大家詳細(xì)介紹了Spring IOC技術(shù)實(shí)現(xiàn)用戶登錄驗(yàn)證機(jī)制的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用 Spring IOC 技術(shù)實(shí)現(xiàn)用戶登錄的驗(yàn)證機(jī)制,對(duì)用戶進(jìn)行登錄驗(yàn)證。

首先利用 Spring 的自動(dòng)裝配模式將 User 對(duì)象注入到控制器中,然后將用戶輸入的用戶名和密碼與系統(tǒng)中限定的合法用戶的用戶名和密碼進(jìn)行匹配。

當(dāng)用戶名與密碼匹配成功時(shí),跳轉(zhuǎn)到登錄成功頁(yè)面;當(dāng)用戶名與密碼不匹配時(shí),跳轉(zhuǎn)到登錄失敗的頁(yè)面。

1.創(chuàng)建 User 對(duì)象,定義用戶名和密碼屬性,代碼如下:

package com.importnew;

public class User {
  private String username;
  private String password;
  
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
}

2.創(chuàng)建控制器 TestUtil ,注入 User 對(duì)象并進(jìn)行登錄驗(yàn)證。代碼如下:

package com.importnew;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class TestUtil extends AbstractController{
  
  private User user;

  public User getUser() {
    return user;
  }
  public void setUser(User user) {
    this.user = user;
  }
  @Override 
  protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
      HttpServletResponse arg1) throws Exception {
    String username = arg0.getParameter("username");
    String password = arg0.getParameter("password");
    if(username.equals(user.getUsername()) && password.equals(user.getPassword())){
      return new ModelAndView("yes");
    }else{
      
      return new ModelAndView("Error");
    }
  }
}

3.在 Spring 的配置文件 applicationContext.xml 中為 User 對(duì)象的屬性賦值,并使用自動(dòng)裝配的方式在控制器 TestUtil 中注入 User 對(duì)象。代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
    
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix">
       <value>/</value>
     </property>
     <property name="suffix">
       <value>.jsp</value>
     </property>
   </bean>
   
   <bean id="user" class="com.importnew.User" >
     <property name="username">
       <value>admin</value>
     </property>
     <property name="password">
       <value>123</value>
     </property>
   </bean>
   
   <bean autowire="byName" id="testUtil" class="com.importnew.TestUtil" >
     <property name="user">
       <ref bean="user"/>
     </property>
   </bean>
</beans>

4.在 web.xml 文件中配置 applicationContext.xml 的自動(dòng)加載,當(dāng)項(xiàng)目啟動(dòng)后程序?qū)⒆詣?dòng)加載配置文件中的信息。代碼如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
     <servlet-name>dispatcherServlet</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/applicationContext.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
     <servlet-name>dispatcherServlet</servlet-name>
     <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
</web-app>


備注:

TestUtil 中繼承的類 AbstractController 需要引關(guān)于 spring-web-mvc 的 JAR 包支持。

////end

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

相關(guān)文章

  • 使用Log4j2代碼方式配置實(shí)現(xiàn)線程級(jí)動(dòng)態(tài)控制

    使用Log4j2代碼方式配置實(shí)現(xiàn)線程級(jí)動(dòng)態(tài)控制

    這篇文章主要介紹了使用Log4j2代碼方式配置實(shí)現(xiàn)線程級(jí)動(dòng)態(tài)控制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java計(jì)算圖兩點(diǎn)之間的所有路徑

    java計(jì)算圖兩點(diǎn)之間的所有路徑

    這篇文章主要為大家詳細(xì)介紹了java計(jì)算圖兩點(diǎn)之間的所有路徑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • spring項(xiàng)目中切面及AOP的使用方法

    spring項(xiàng)目中切面及AOP的使用方法

    我們知道,spring兩大核心,IOC(控制反轉(zhuǎn))和AOP(切面),那為什么要使用AOP,AOP是什么呢?帶著這些問題通過本文學(xué)習(xí)下吧
    2021-06-06
  • 解決HttpServletResponse和HttpServletRequest取值的2個(gè)坑

    解決HttpServletResponse和HttpServletRequest取值的2個(gè)坑

    這篇文章主要介紹了解決HttpServletResponse和HttpServletRequest取值的2個(gè)坑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 一文講解如何解決Java中的IllegalArgumentException異常

    一文講解如何解決Java中的IllegalArgumentException異常

    這篇文章主要給大家介紹了關(guān)于如何解決Java中IllegalArgumentException異常的相關(guān)資料,IllegalArgumentException是Java中的一個(gè)標(biāo)準(zhǔn)異常類,通常在方法接收到一個(gè)不合法的參數(shù)時(shí)拋出,需要的朋友可以參考下
    2024-03-03
  • springcloud?eureka切換nacos的配置方法

    springcloud?eureka切換nacos的配置方法

    這篇文章主要介紹了springcloud?eureka切換nacos,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • 解決RestTemplate反序列化嵌套對(duì)象的問題

    解決RestTemplate反序列化嵌套對(duì)象的問題

    這篇文章主要介紹了解決RestTemplate反序列化嵌套對(duì)象的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • ocp開閉原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    ocp開閉原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了ocp開閉原則的相關(guān)資料,ocp開閉原則指導(dǎo)我們?nèi)绾谓⒁粋€(gè)穩(wěn)定的、靈活的系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Mybatis和Mybatis-Plus時(shí)間范圍查詢方式

    Mybatis和Mybatis-Plus時(shí)間范圍查詢方式

    這篇文章主要介紹了Mybatis和Mybatis-Plus時(shí)間范圍查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解在SpringBoot中使用MongoDb做單元測(cè)試的代碼

    詳解在SpringBoot中使用MongoDb做單元測(cè)試的代碼

    這篇文章主要介紹了詳解在SpringBoot中使用MongoDb做單元測(cè)試的代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11

最新評(píng)論