亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring mvc4中相關注解的詳細講解教程

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

前言

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

1.@Controller

Controller控制器是通過服務接口定義的提供訪問應用程序的一種行為,它解釋用戶的輸入,將其轉(zhuǎ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的訪問地址應該是:localhost:8080/proj/admin/login.html

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

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

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

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

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

     C) 可以指定為含正則表達式的一類值( 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”請求頭和對應值為“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模板變量的值上,之前示例中也有相關體現(xiàn)。

4.@RequestParam

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

5.@RequestBody

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

6.@SessionAttibutes

@SessionAttibutes可以通過ModelMap對象的put操作設置相關的session同時在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ù)時會用到。

示例如下:

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)常見到一些控制器實現(xiàn)了REST的API,只為服務于json,xml或其它自定義的類型內(nèi)容。@RestController用來創(chuàng)建REST類型的控制器,與@Controller類型。@RestController就是這樣一種類型,它避免了你重復的寫@RequestMapping與@ResponseBody

9.@ModelAttribute

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

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)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • 一分鐘掌握Java?Quartz定時任務

    一分鐘掌握Java?Quartz定時任務

    這篇文章主要為大家介紹了Java?Quartz定時任務一分鐘掌握教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • Spring中的@Conditional注解實現(xiàn)分析

    Spring中的@Conditional注解實現(xiàn)分析

    這篇文章主要介紹了Spring中的@Conditional注解實現(xiàn)分析,  @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價值的是Spring Boot的擴展@ConditionalOnBean等,需要的朋友可以參考下
    2023-12-12
  • Java RPC框架過濾器機制原理解析

    Java RPC框架過濾器機制原理解析

    這篇文章主要介紹了Java RPC框架過濾器機制原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java中的布隆過濾器原理實現(xiàn)和應用

    Java中的布隆過濾器原理實現(xiàn)和應用

    Java中的布隆過濾器是一種基于哈希函數(shù)的數(shù)據(jù)結(jié)構(gòu),能夠高效地判斷元素是否存在于一個集合中。它廣泛應用于緩存、網(wǎng)絡協(xié)議、數(shù)據(jù)查詢等領域,在提高程序性能和減少資源消耗方面具有顯著優(yōu)勢
    2023-04-04
  • Java開發(fā)完整短信驗證碼功能的全過程

    Java開發(fā)完整短信驗證碼功能的全過程

    利用短信驗證碼進行身份驗證是目前互聯(lián)網(wǎng)眾多產(chǎn)品常用的一種方式,那么這種短信驗證功能是如何實現(xiàn)的呢,下面這篇文章主要給大家介紹了關于Java開發(fā)完整短信驗證碼功能的相關資料,需要的朋友可以參考下
    2021-10-10
  • Java中隨機函數(shù)變換的示例詳解

    Java中隨機函數(shù)變換的示例詳解

    這篇文章主要為大家詳細介紹了Java中隨機函數(shù)的變換,文中的示例代碼講解詳細,對我們學習Java有一定的幫助,感興趣的可以了解一下
    2022-08-08
  • Java并發(fā)編程之Semaphore詳解

    Java并發(fā)編程之Semaphore詳解

    這篇文章主要介紹了Java并發(fā)編程之concurrent包中的Semaphore詳解,信號量Semaphore一般用來表示可用資源的個數(shù),相當于一個計數(shù)器,可類比生活中停車場牌子上面顯示的停車場剩余車位數(shù)量,需要的朋友可以參考下
    2023-12-12
  • springboot websocket集群(stomp協(xié)議)連接時候傳遞參數(shù)

    springboot websocket集群(stomp協(xié)議)連接時候傳遞參數(shù)

    這篇文章主要介紹了springboot websocket集群(stomp協(xié)議)連接時候傳遞參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • springboot2+es7使用RestHighLevelClient的示例代碼

    springboot2+es7使用RestHighLevelClient的示例代碼

    本文主要介紹了springboot2+es7使用RestHighLevelClient的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Java排序算法總結(jié)之希爾排序

    Java排序算法總結(jié)之希爾排序

    這篇文章主要介紹了Java排序算法總結(jié)之希爾排序,較為詳細的分析了希爾排序的原理與java的實現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05

最新評論