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

spring mvc4中相關(guān)注解的詳細(xì)講解教程

 更新時(shí)間:2017年06月22日 10:58:07   作者:chen2013  
這篇文章主要給大家介紹了關(guān)于spring mvc4中相關(guān)注解的相關(guān)資料,其中詳細(xì)介紹了關(guān)于@Controller、@RequestMapping、@RathVariable、@RequestParam及@RequestBody等等注解的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

在開始本文之前要說明以下,首先我是一個初學(xué)springmvc,抱著去加深印象的目的去整理相關(guān)springmvc4的相關(guān)注解,同時(shí)也希望給需要相關(guān)查閱的讀者帶來幫助,好了,下面話就不多說了,一起來看看詳細(xì)的介紹吧。

1.@Controller

Controller控制器是通過服務(wù)接口定義的提供訪問應(yīng)用程序的一種行為,它解釋用戶的輸入,將其轉(zhuǎn)換成一個模型然后將試圖呈獻(xiàn)給用戶。Spring MVC 使用 @Controller 定義控制器,它還允許自動檢測定義在類路徑下的組件并自動注冊。如想自動檢測生效,需在xml頭文件下引入 spring-context:

<?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"
 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">
 <context:component-scan base-package="com.chen" />
</beans>

2.@RequestMapping

RequestMapping 注解將類似 "/admin"這樣的URL映射到整個類或特定的處理方法上。一般來說,類級別的注解映射特定的請求路徑到表單控制器上,而方法級別的注解只是映射 為一個特定的HTTP方法請求("GET","POST"等)或HTTP請求參數(shù)。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("admin")
public class LoginController {
 
 @RequestMapping(value = "login" , method = RequestMethod.GET , consumes = "text/html")
 public String toLoginPage(){
 return "/WEB-INF/jsp/login.jsp";
 }
}

上述url的訪問地址應(yīng)該是:localhost:8080/proj/admin/login.html

consumes-指定處理請求的提交內(nèi)容類型Content-Type,例如 application/json,text/html。

produces-指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回。

value-指定請求的實(shí)際地址,指定的地址可以是URI Template 模式

     A) 可以指定為普通的具體值;

     B) 可以指定為含有某變量的一類值(URI Template Patterns with Path Variables);

     C) 可以指定為含正則表達(dá)式的一類值( URI Template Patterns with Regular Expressions);

如下示例:

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.RequestMethod;

@Controller
public class BlogController {
 
 @RequestMapping(value = "blog/{nick}/{year:20\\d{2}}/{month:1|1[0-2]}/{day:[12][0-9]|30|[1-9]}" , method = RequestMethod.GET)
 public String toBlogPage(@PathVariable String nick,
  @PathVariable Integer year,@PathVariable Integer month,@PathVariable Integer day){
 return "/WEB-INF/jsp/blog.jsp";
 }
}

params-指定request中必須包含某些參數(shù)值是,才讓該方法處理。

headers-指定request中必須包含某些指定的header值,才能讓該方法處理請求。

如下示例:

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.RequestMethod;

@Controller
public class BlogController {
 
 //僅處理request的header中包含了指定“Refer”請求頭和對應(yīng)值為“http://www.ttyouni.com/”的請求
 @RequestMapping(value = "image", headers="Referer=http://www.ttyouni.com/" )
 public String getImage(){
 return "/WEB-INF/jsp/image.jsp";
 }
}

3.@RathVariable

在Spring MVC中,可以使用 @PathVariable 注解方法參數(shù)并將其綁定到URI模板變量的值上,之前示例中也有相關(guān)體現(xiàn)。

4.@RequestParam

@RequestParam將請求的參數(shù)綁定到方法中的參數(shù)上。其實(shí)即使不配置該參數(shù),注解也會默認(rèn)使用該參數(shù)。如果想自定義指定參數(shù)的話,可以將@RequestParam的 required 屬性設(shè)置為false。

5.@RequestBody

@RequestBody是指方法參數(shù)應(yīng)該被綁定到HTTP請求Body上。

6.@SessionAttibutes

@SessionAttibutes可以通過ModelMap對象的put操作設(shè)置相關(guān)的session同時(shí)在attibute對象也會有該對象。

示例如下:

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 com.chen.proj.service.UserService;

@Controller
@RequestMapping("admin")
@SessionAttributes("user")
public class LoginController {
 
 @Resource
 UserService service;
 
 @RequestMapping(value = "doLogin", method = RequestMethod.POST)
 public String doLogin(@RequestParam String username , @RequestParam String password, HttpServletRequest request, 
            ModelMap map ){ 
 try {
  User user = service.doLogin(username, password); 
  map.put("user", user);
 } catch (Exception e) {
  request.setAttribute("error", e.getMessage());
  return "/WEB-INF/jsp/login.jsp";
 } 
 return "/WEB-INF/jsp/loginsuccess.jsp";
 }
 
}

7.@ResponseBody

@ResponseBody與@RequestBody類似,它的作用是將返回類型直接輸入到HTTP response body中。@ResponseBody在輸出JSON格式的數(shù)據(jù)時(shí)會用到。

示例如下:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chen.proj.bean.User;

@Controller
public class JsonController {

 @ResponseBody
 @RequestMapping("/getJson")
 public User getUserInfo(){
 User user = new User();
 user.setPassword("1234");
 user.setUsername("jsontest");
 return user;
 } 
}

8.@RestController

 我們經(jīng)常見到一些控制器實(shí)現(xiàn)了REST的API,只為服務(wù)于json,xml或其它自定義的類型內(nèi)容。@RestController用來創(chuàng)建REST類型的控制器,與@Controller類型。@RestController就是這樣一種類型,它避免了你重復(fù)的寫@RequestMapping與@ResponseBody

9.@ModelAttribute

@ModelAttribute可以作用在方法或方法參數(shù)上,當(dāng)它作用在方法上時(shí),標(biāo)明該方法的目的是添加一個或多個模型屬性。
當(dāng)作用在方法參數(shù)上時(shí),表明該參數(shù)可以在方法模型中檢索到。如果該參數(shù)不在當(dāng)前模型中,該參數(shù)先被實(shí)例化然后添加到模型中。一旦模型中有了該參數(shù),該參數(shù)的字段應(yīng)該填充所有請求參數(shù)匹配的名稱中。這是spring mvc中重要的數(shù)據(jù)綁定機(jī)制,它省去了單獨(dú)解析每個表單字段的時(shí)間。

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chen.proj.bean.User;

@Controller
public class UserController {

 @ModelAttribute
 public User addUser(@RequestParam String number) {
 return service.findUser(number);
 }

 @ModelAttribute
 public void populateModel(@RequestParam String number, Model model) {
 model.addAttribute(service.findUser(number)); 
 // add more ...
 }
}

注解的出現(xiàn)終結(jié)了xml配置文件漫天飛的年代,它讓程序擁有更高的可讀性,可配置性與靈活性。給人一種更簡潔明了的感覺。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Java?synchronized同步關(guān)鍵字工作原理

    Java?synchronized同步關(guān)鍵字工作原理

    synchronized作為Java程序員最常用同步工具,很多人卻對它的用法和實(shí)現(xiàn)原理一知半解,以至于還有不少人認(rèn)為synchronized是重量級鎖,性能較差,盡量少用。但不可否認(rèn)的是synchronized依然是并發(fā)首選工具,本文就來詳細(xì)講講
    2023-02-02
  • Java中String、StringBuffer、StringBuilder的區(qū)別介紹

    Java中String、StringBuffer、StringBuilder的區(qū)別介紹

    這篇文章主要介紹了Java中String、StringBuffer、StringBuilder的區(qū)別介紹,本文講解了可變與不可變、是否多線程安全、gBuilder與StringBuffer共同點(diǎn)等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的方法

    Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的方法

    這篇文章主要給大家介紹了關(guān)于Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java中的CyclicBarrier循環(huán)柵欄深入解析

    Java中的CyclicBarrier循環(huán)柵欄深入解析

    這篇文章主要介紹了Java中的CyclicBarrier循環(huán)柵欄深入解析,CycleBarrier 它就相當(dāng)于是一個柵欄,所有線程在到達(dá)柵欄后都需要等待其他線程,等所有線程都到達(dá)后,再一起通過,需要的朋友可以參考下
    2023-12-12
  • java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法

    java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法

    這篇文章主要介紹了java識別一篇文章中某單詞出現(xiàn)個數(shù)的方法,涉及java字符解析操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Java的三種代理模式簡述

    Java的三種代理模式簡述

    這篇文章主要簡述Java的三種代理模式,java的代理模式主要包括靜態(tài)代理、動態(tài)代理、Cglib代理,感興趣的小伙伴可以參考下面文章的具體內(nèi)容
    2021-09-09
  • Java中l(wèi)ist集合的clear方法及空字符串的區(qū)別

    Java中l(wèi)ist集合的clear方法及空字符串的區(qū)別

    這篇文章主要介紹了Java中l(wèi)ist集合的clear方法及空字符串的區(qū)別,在使用list?結(jié)合的時(shí)候習(xí)慣了?list=null?;在創(chuàng)建這樣的方式,但是發(fā)現(xiàn)使用list的clear?方法很不錯,尤其是有大量循環(huán)的時(shí)候<BR>list.clear()與list?=?null?區(qū)別,需要的朋友可以參考下
    2023-08-08
  • Spark-Sql入門程序示例詳解

    Spark-Sql入門程序示例詳解

    Spark?SQL?作為?Spark?四大核心組件之一,主要用于處理結(jié)構(gòu)化數(shù)據(jù)或半結(jié)構(gòu)化數(shù)據(jù),它支持在Spark?中使用?SQL?對數(shù)據(jù)進(jìn)行查詢,本文給大家介紹Spark-Sql入門程序,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • java金額數(shù)字轉(zhuǎn)中文工具類詳解

    java金額數(shù)字轉(zhuǎn)中文工具類詳解

    這篇文章主要為大家詳細(xì)介紹了java金額數(shù)字轉(zhuǎn)中文工具類的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 利用Java代碼實(shí)現(xiàn)區(qū)塊鏈技術(shù)

    利用Java代碼實(shí)現(xiàn)區(qū)塊鏈技術(shù)

    這篇文章主要介紹了利用Java代碼實(shí)現(xiàn)區(qū)塊鏈技術(shù),區(qū)塊鏈的應(yīng)用范圍幾乎無窮無盡,關(guān)于區(qū)塊鏈?zhǔn)侨绾芜\(yùn)作的,下文來看看具體的內(nèi)容介紹吧,需要的朋友可以參考一下
    2022-04-04

最新評論

巴东县| 巩留县| 师宗县| 二手房| 石台县| 西充县| 盐津县| 卫辉市| 庆安县| 侯马市| 本溪| 通化市| 大名县| 林周县| 南康市| 陈巴尔虎旗| 砚山县| 沙田区| 荥阳市| 伊宁市| 于都县| 瑞昌市| 呈贡县| 石狮市| 揭西县| 珲春市| 平果县| 大厂| 石狮市| 穆棱市| 四平市| 五大连池市| 民县| 梁平县| 遂宁市| 安化县| 长顺县| 梁山县| 德清县| 枣庄市| 大姚县|