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

SpringMVC使用注解實現(xiàn)登錄功能

 更新時間:2022年09月06日 11:25:21   作者:凌冰_  
這篇文章主要為大家詳細(xì)介紹了SpringMVC使用注解實現(xiàn)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了SpringMVC使用注解實現(xiàn)登錄的具體代碼,供大家參考,具體內(nèi)容如下

一、使用Component\Controller\Service\Repository四大注解類:

  • @Component 是通用標(biāo)注
  • @Controller 標(biāo)注 web 控制器
  • @Service 標(biāo)注 Servicec 層的服務(wù)
  • @Respository 標(biāo)注 DAO層的數(shù)據(jù)訪問

這些注解都是類級別的,可以不帶任何參數(shù),也可以帶一個參數(shù),代表bean名字,在進行注入的時候就可以通過名字進行注入了。

二、@Resource和@Autowired注解的異同

  • @Autowired默認(rèn)按類型裝配,默認(rèn)情況下必須要求依賴對象必須存在,如果要允許null值,可以設(shè)置它的required屬性為false

例如:@Autowired(required=false),如果我們想使用名稱裝配可以結(jié)合@Qualifier注解進行使用

  • @Resource,默認(rèn)安裝名稱進行裝配,名稱可以通過name屬性進行指定,如果沒有指定name屬性,當(dāng)注解寫在字段上時,默認(rèn)取字段名進行安裝名稱查找 ,如果注解寫在setter方法上默認(rèn)取屬性名進行裝配 。當(dāng)找不到與名稱匹配的bean時才按照類型進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。

三、其他注解類 

(1) PathVariable注解類

@RequestMapping注解中使用占位符的情況下,需要使用@PathVariable注解指定占位符參數(shù)

(2) RequestParam注解類

表單提交數(shù)據(jù)時,可以通過這個注解來解決request.getParameter("uname")

(3) CookieValue注解類

如果要保存字符串?dāng)?shù)據(jù),可以使用這個注解

(4) SessionAttributes注解類 

如果希望在多個請求之間公用某個模型屬性數(shù)據(jù),則可以在控制器類標(biāo)注一個@SessionAttributes,SpringMVC會將模型中對應(yīng)的屬性暫存到HttpSerssion中除了SessionAttributes,還可以直接用原生態(tài)的request.getSession()來處理

(5) ResponseBody注解類

用于將Controller的方法返回的對象,通過適當(dāng)?shù)腍ttpMessageConverter(轉(zhuǎn)換器)轉(zhuǎn)換為指定格式后,寫入到Response對象的body數(shù)據(jù)區(qū);  返回如json、xml等時使用

(6)RequestHeader注解類

@RequestHeader注解,可以把Request請求header部分的值綁定到方法的參數(shù)上

四、應(yīng)用注解來實現(xiàn)登錄

 (1)、創(chuàng)建工程如下:

 (2)、配置文件pom.xml(主要是修改:dependencies的內(nèi)容)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
?? ?<modelVersion>4.0.0</modelVersion>
?? ?<groupId>springmvc</groupId>
?? ?<artifactId>springmvc</artifactId>
?? ?<version>0.0.1-SNAPSHOT</version>
?? ?<packaging>war</packaging>
?? ?<name />
?? ?<description />
?? ?<properties>
?? ??? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
?? ?</properties>
?? ?<dependencies>
?
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework</groupId>
?? ??? ??? ?<artifactId>spring-webmvc</artifactId>
?? ??? ??? ?<version>4.1.6.RELEASE</version>
?? ??? ?</dependency>
?
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework</groupId>
?? ??? ??? ?<artifactId>spring-tx</artifactId>
?? ??? ??? ?<version>4.1.6.RELEASE</version>
?? ??? ?</dependency>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>javax.servlet</groupId>
?? ??? ??? ?<artifactId>javax.servlet-api</artifactId>
?? ??? ??? ?<version>3.1-b05</version>
?? ??? ?</dependency>
?
?? ??? ?<!-- jackson start -->
?
?? ??? ?<dependency>
?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId>
?? ??? ??? ?<artifactId>jackson-core</artifactId>
?? ??? ??? ?<version>2.1.0</version>
?? ??? ?</dependency>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId>
?? ??? ??? ?<artifactId>jackson-databind</artifactId>
?? ??? ??? ?<version>2.1.0</version>
?? ??? ?</dependency>
?? ??? ?<dependency>
?? ??? ??? ?<groupId>com.fasterxml.jackson.core</groupId>
?? ??? ??? ?<artifactId>jackson-annotations</artifactId>
?? ??? ??? ?<version>2.1.0</version>
?? ??? ?</dependency>
?? ??? ?<!-- jackson end -->
?? ?</dependencies>
?? ?<build>
?? ??? ?<finalName>springmvc</finalName>
?? ??? ?<sourceDirectory>${basedir}/src</sourceDirectory>
?? ??? ?<outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
?? ??? ?<resources>
?? ??? ??? ?<resource>
?? ??? ??? ??? ?<directory>${basedir}/src</directory>
?? ??? ??? ??? ?<excludes>
?? ??? ??? ??? ??? ?<exclude>**/*.java</exclude>
?? ??? ??? ??? ?</excludes>
?? ??? ??? ?</resource>
?? ??? ?</resources>
?? ??? ?<plugins>
?? ??? ??? ?<plugin>
?? ??? ??? ??? ?<artifactId>maven-war-plugin</artifactId>
?? ??? ??? ??? ?<configuration>
?? ??? ??? ??? ??? ?<webappDirectory>${basedir}/WebRoot</webappDirectory>
?? ??? ??? ??? ??? ?<warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
?? ??? ??? ??? ?</configuration>
?? ??? ??? ?</plugin>
?? ??? ??? ?<plugin>
?? ??? ??? ??? ?<artifactId>maven-compiler-plugin</artifactId>
?? ??? ??? ??? ?<configuration>
?? ??? ??? ??? ??? ?<source>1.6</source>
?? ??? ??? ??? ??? ?<target>1.6</target>
?? ??? ??? ??? ?</configuration>
?? ??? ??? ?</plugin>
?? ??? ?</plugins>
?? ?</build>
</project>

(3)spring-mvc.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
? ? xmlns:util="http://www.springframework.org/schema/util"
? ? xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
?? ??? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
?? ??? ?http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
?? ??? ?http://www.springframework.org/schema/util ?http://www.springframework.org/schema/util/spring-util-3.0.xsd
?? ??? ?">
?? ?<!-- 自動掃描 com.hlx 包下面的所有組件(使用了springmvc注解)-->
?? ?<context:component-scan base-package="com.hlx.*" />
?
? ?<bean
? ? ? ? class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
? ? ? ? <property name="messageConverters">
? ? ? ? ? ? <util:list >
? ? ? ? ? ? ?? ?<!-- 配置處理JSON數(shù)據(jù) -->
? ? ? ? ? ? ? ? <ref bean="mappingJackson2HttpMessageConverter" />
? ? ? ? ? ? </util:list>
?
? ? ? ? </property>
? ? </bean>
?
?<!-- 讀寫JSON對象 -->
? <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
? ? <property name="supportedMediaTypes">
? ? ?<list>
? ? ? <value>text/html;charset=UTF-8</value>
? ? ?</list>
? ? </property>
? </bean>
??
</beans>

(4)web.xml文件同前一個springmvc中的文件相同

(5)JSP頁面(省略) 

(6)創(chuàng)建src/main/java 下 bean,dao.service,controller 

package com.hlx.bean;
?
public class User {
?? ?private String uname;
?? ?private String upass;
?
?? ?public String getUname() {
?? ??? ?return uname;
?? ?}
?
?? ?public void setUname(String uname) {
?? ??? ?this.uname = uname;
?? ?}
?
?? ?public String getUpass() {
?? ??? ?return upass;
?? ?}
?
?? ?public void setUpass(String upass) {
?? ??? ?this.upass = upass;
?? ?}?
?? ?public User() {
?? ??? ?super();
?? ?}
?
?? ?public User(String uname, String upass) {
?? ??? ?super();
?? ??? ?this.uname = uname;
?? ??? ?this.upass = upass;
?? ?}
?
?? ?@Override
?? ?public String toString() {
?? ??? ?return "User [uname=" + uname + ", upass=" + upass + "]";
?? ?}
?
?? ?
}
package com.hlx.dao;
?
import org.springframework.stereotype.Repository;
?
import com.hlx.bean.User;
?
@Repository
public class UserDao {
?
?? ?/**
?? ? * 根據(jù)用戶名來查詢
?? ? * @param uname
?? ? * @return
?? ? */
?? ?public User selectByName(String uname) {
?? ??? ?if ("admin".equals(uname)) {
?? ??? ??? ?User user = new User("admin", "aaa");
?? ??? ??? ?return user;
?? ??? ?}
?? ??? ?return null;
?? ?}
?
}
package com.hlx.service;
?
import javax.annotation.Resource;
?
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
?
import com.hlx.bean.User;
import com.hlx.dao.UserDao;
?
@Service
public class UserService {
?
?? ?@Resource
?? ?private UserDao userDao;
?
?? ?/**
?? ? * 登錄處理
?? ? * @param uname
?? ? * @param upass
?? ? * @return
?? ? * @throws Exception
?? ? */
?? ?public User doLogin(String uname, String upass) throws Exception {
?? ??? ?// 判斷
?? ??? ?if (uname == null || "".equals(uname)) {
?? ??? ??? ?throw new Exception("用戶名不能為空!");
?? ??? ?}
?? ??? ?if (upass == null || "".equals(upass)) {
?? ??? ??? ?throw new Exception("密碼不能為空!");
?? ??? ?}
?
?? ??? ?User user = userDao.selectByName(uname);
?
?? ??? ?if (user == null) {
?? ??? ??? ?throw new Exception("用戶名不存在!");
?? ??? ?}
?
?? ??? ?if (!user.getUpass().equals(upass)) {
?? ??? ??? ?throw new Exception("密碼不正確!");
?? ??? ?}
?
?? ??? ?return user;
?
?? ?}
}
package com.hlx.controller;
?
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
?
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
?
?
import com.hlx.bean.User;
import com.hlx.service.UserService;
?
@Controller
@SessionAttributes("user") //保存user對象
public class UserController {
?
?? ?//業(yè)務(wù)層
?? ?@Resource
?? ?private UserService userService;
?? ?
?? ?@RequestMapping("login")
?? ?public String toLoginPage(){
?? ??? ?return "jsp/login.jsp";
?? ?}
?? ?/**
?? ? *?
?? ? * @param uname 表單傳過來的參數(shù)
?? ? * @param upass?
?? ? * @param request ?請求對象
?? ? * @return
?? ? */
?? ?@RequestMapping(value="dologin",method=RequestMethod.POST)
?? ?public String doLogin(@RequestParam String uname,@RequestParam String upass,HttpServletRequest request,ModelMap map){
?? ??? ?try {
?? ??? ??? ?//調(diào)用方法
?? ??? ? User user=userService.doLogin(uname,upass);
?? ??? ??
?? ??? ? //存入user
?? ??? ? map.put("user", user);
?? ??? ? ///
?? ??? ??
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?request.setAttribute("error", e.getMessage()); ?//提示錯誤消息
?? ??? ??? ?return "jsp/login.jsp"; ?//返回登錄頁面
?? ??? ?}
?? ??? ?return "jsp/success.jsp";
?? ?}
?? ?
?? ?/**
?? ? * 退出
?? ? * @return
?? ? */
?? ?@RequestMapping("dologout")
?? ?public String doLogout(SessionStatus status){
?? ??? ?//清空session
?? ??? ?status.setComplete();
?? ??? ?//返回登錄頁面
?? ??? ?return "jsp/login.jsp";
?? ?}
?? ?
}

效果:

package com.hlx.controller;
?
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
?
import com.hlx.bean.User;
?
@Controller
public class JSONController {
?
?? ?@ResponseBody
?? ?@RequestMapping("/getJson")
?? ?public User getUserinfo(){
?? ??? ?User user = new User("mike", "123456");
?? ??? ?return user;
?? ?}
}

效果:

package com.hlx.controller;
?
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
?
import com.hlx.bean.User;
?
@Controller
public class PathVariableControler {
?? ?
?? ?@ResponseBody
?? ?@RequestMapping("/pathvariable/{uname}")
?? ?public User test1(@PathVariable String uname){
?? ??? ?User user = new User();
?? ??? ?user.setUname("john");
?? ??? ?return user;
?? ?}
?? ?@ResponseBody
?? ?@RequestMapping("/pathvariable2/{intval}")
?? ?public User test1(@PathVariable Integer intval){
?? ??? ?User user = new User();
?? ??? ?user.setUname(intval + "");
?? ??? ?return user;
?? ?}
?
}

效果:

注意:這里是數(shù)字

注意:這里只能是數(shù)字,不能字符串,否則找不到頁面!

總結(jié):  

應(yīng)用了四大注解類Component、Controller、Service、Repository;掌握PathVariable,RequestParam注解類

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

相關(guān)文章

  • Spring?Session(分布式Session共享)實現(xiàn)示例

    Spring?Session(分布式Session共享)實現(xiàn)示例

    這篇文章主要介紹了Spring?Session(分布式Session共享)實現(xiàn)示例,文章內(nèi)容詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Java中零拷貝和深拷貝的原理及實現(xiàn)探究(代碼示例)

    Java中零拷貝和深拷貝的原理及實現(xiàn)探究(代碼示例)

    深拷貝和零拷貝是兩個在 Java 中廣泛使用的概念,它們分別用于對象復(fù)制和數(shù)據(jù)傳輸優(yōu)化,下面將詳細(xì)介紹這兩個概念的原理,并給出相應(yīng)的 Java 代碼示例,感興趣的朋友一起看看吧
    2023-12-12
  • Java中和隊列相關(guān)的基本操作

    Java中和隊列相關(guān)的基本操作

    在Java中,隊列是一種常用的數(shù)據(jù)結(jié)構(gòu),用于存儲和管理元素。Java提供了Queue接口和其實現(xiàn)類,包括LinkedList和ArrayDeque等。隊列的基本操作包括入隊(enqueue)、出隊(dequeue)、獲取隊首元素(peek)和判斷隊列是否為空(isEmpty)。
    2023-09-09
  • MyBatis-Plus找不到Mapper.xml文件的幾種解決方法

    MyBatis-Plus找不到Mapper.xml文件的幾種解決方法

    mybatis-plus今天遇到一個問題,就是mybatis 沒有讀取到mapper.xml 文件,所以下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus找不到Mapper.xml文件的幾種解決方法,需要的朋友可以參考下
    2022-06-06
  • Mybatis-Plus接口BaseMapper與Services使用詳解

    Mybatis-Plus接口BaseMapper與Services使用詳解

    這篇文章主要為大家介紹了Mybatis-Plus接口BaseMapper與Services使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • intellij idea創(chuàng)建第一個動態(tài)web項目的步驟方法

    intellij idea創(chuàng)建第一個動態(tài)web項目的步驟方法

    這篇文章主要介紹了intellij idea創(chuàng)建第一個動態(tài)web項目的步驟方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java超詳細(xì)介紹抽象類與接口的使用

    Java超詳細(xì)介紹抽象類與接口的使用

    在類中沒有包含足夠的信息來描繪一個具體的對象,這樣的類稱為抽象類,接口是Java中最重要的概念之一,它可以被理解為一種特殊的類,不同的是接口的成員沒有執(zhí)行體,是由全局常量和公共的抽象方法所組成,本文給大家介紹Java抽象類和接口,感興趣的朋友一起看看吧
    2022-05-05
  • mybatis-plus查詢源碼詳解

    mybatis-plus查詢源碼詳解

    這篇文章主要介紹了mybatis-plus查詢源碼解讀,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • SpringBoot使用Flyway進行數(shù)據(jù)庫遷移的實現(xiàn)示例

    SpringBoot使用Flyway進行數(shù)據(jù)庫遷移的實現(xiàn)示例

    Flyway是一個數(shù)據(jù)庫遷移工具,它提供遷移歷史和回滾的功能,本文主要介紹了如何使用Flyway來管理Spring Boot應(yīng)用程序中的SQL數(shù)據(jù)庫架構(gòu),感興趣的可以了解一下
    2023-08-08
  • SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解

    SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解

    這篇文章主要介紹了SpringBoot實戰(zhàn)之高效使用枚舉參數(shù)(原理篇)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評論