溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

一文教你如何在java中使用SpringMVC

發(fā)布時間:2020-11-17 15:17:27 來源:億速云 閱讀:122 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)一文教你如何在java中使用SpringMVC,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、簡介

SpringMVC 中,控制器Controller 負(fù)責(zé)處理由DispatcherServlet 分發(fā)的請求,它把用戶請求的數(shù)據(jù)經(jīng)過業(yè)務(wù)處理層處理之后封裝成一個Model ,然后再把該Model 返回給對應(yīng)的View 進行展示。在SpringMVC 中提供了一個非常簡便的定義Controller 的方法,你無需繼承特定的類或?qū)崿F(xiàn)特定的接口,只需使用@Controller 標(biāo)記一個類是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定義URL 請求和Controller 方法之間的映射,這樣的Controller 就能被外界訪問到。此外Controller 不會直接依賴于HttpServletRequestHttpServletResponse 等HttpServlet 對象,它們可以通過Controller 的方法參數(shù)靈活的獲取到。為了先對Controller 有一個初步的印象,以下先定義一個簡單的Controller:

// *java代碼*

@Controller 
public class MyController {  
  @RequestMapping ( "/showView" ) 
  public ModelAndView showView() { 
    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName( "viewName" ); 
    modelAndView.addObject( " 需要放到 model 中的屬性名稱 " , " 對應(yīng)的屬性值,它是一個對象 " ); 
    return modelAndView; 
  } 
 }

在上面的示例中,@Controller 是標(biāo)記在類MyController 上面的,所以類MyController 就是一個SpringMVC Controller 對象了,然后使用@RequestMapping(“/showView”) 標(biāo)記在Controller 方法上,表示當(dāng)請求/showView.do 的時候訪問的是MyController 的showView 方法,該方法返回了一個包括Model 和View 的ModelAndView 對象。這些在后續(xù)都將會詳細介紹。

二、使用@Controller 定義一個Controller控制器

@Controller 用于標(biāo)記在一個類上,使用它標(biāo)記的類就是一個SpringMVC Controller 對象。分發(fā)處理器將會掃描使用了該注解的類的方法,并檢測該方法是否使用了@RequestMapping 注解。@Controller 只是定義了一個控制器類,而使用@RequestMapping 注解的方法才是真正處理請求的處理器,這個接下來就會講到。

單單使用@Controller 標(biāo)記在一個類上還不能真正意義上的說它就是SpringMVC 的一個控制器類,因為這個時候Spring 還不認(rèn)識它。那么要如何做Spring 才能認(rèn)識它呢?這個時候就需要我們把這個控制器類交給Spring 來管理。拿MyController 來舉一個例子:

//java代碼
@Controller 
public class MyController { 
  @RequestMapping ( "/showView" ) 
  public ModelAndView showView() { 
    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName( "viewName" ); 
    modelAndView.addObject( " 需要放到 model 中的屬性名稱 " , " 對應(yīng)的屬性值,它是一個對象 " ); 
    return modelAndView; 
  } 
 
}

這個時候有兩種方式可以把MyController 交給Spring 管理,好讓它能夠識別我們標(biāo)記的@Controller 。

第一種方式是在SpringMVC 的配置文件中定義MyController 的bean對象

<bean class="com.host.app.web.controller.MyController"></bean>

第二種方式是在SpringMVC 的配置文件中告訴Spring 該到哪里去找標(biāo)記為@Controller 的Controller 控制器。

<!-- html 代碼-->
< context:component-scan base-package = "com.host.app.web.controller" > 
  < context:exclude-filter type = "annotation" 
    expression = "org.springframework.stereotype.Service" /> 
</ context:component-scan >

三、使用 @RequestMapping 來映射 Request 請求與處理器

可以使用@RequestMapping 來映射URL 到控制器類,或者是到Controller 控制器的處理方法上。當(dāng)@RequestMapping 標(biāo)記在Controller 類上的時候,里面使用@RequestMapping 標(biāo)記的方法的請求地址都是相對于類上的@RequestMapping 而言的;當(dāng)Controller 類上沒有標(biāo)記@RequestMapping 注解時,方法上的@RequestMapping 都是絕對路徑。這種絕對路徑和相對路徑所組合成的最終路徑都是相對于根路徑“/ ”而言的。

//java代碼
@Controller 
public class MyController { 
  @RequestMapping ( "/showView" ) 
  public ModelAndView showView() { 
    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName( "viewName" ); 
    modelAndView.addObject( " 需要放到 model 中的屬性名稱 " , " 對應(yīng)的屬性值,它是一個對象 " ); 
    return modelAndView; 
  } 
 
}

在這個控制器中,因為MyController 沒有被@RequestMapping 標(biāo)記,所以當(dāng)需要訪問到里面使用了@RequestMapping 標(biāo)記的showView 方法時,就是使用的絕對路徑/showView.do 請求就可以了。

//java代碼
@Controller 
@RequestMapping ( "/test" ) 
public class MyController { 
  @RequestMapping ( "/showView" ) 
  public ModelAndView showView() { 
    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName( "viewName" ); 
    modelAndView.addObject( " 需要放到 model 中的屬性名稱 " , " 對應(yīng)的屬性值,它是一個對象 " ); 
    return modelAndView; 
  } 
 
}

這種情況是在控制器上加了@RequestMapping 注解,所以當(dāng)需要訪問到里面使用了@RequestMapping 標(biāo)記的方法showView() 的時候就需要使用showView 方法上@RequestMapping 相對于控制器MyController 上@RequestMapping 的地址,即/test/showView.do 。

(一)使用URI模板

URI 模板就是在URI 中給定一個變量,然后在映射的時候動態(tài)的給該變量賦值。在SpringMVC 中,這種取代模板中定義的變量的值也可以給處理器方法使用,這樣我們就可以非常方便的實現(xiàn)URL 的RestFul 風(fēng)格。這個變量在SpringMVC 中是使用@PathVariable 來標(biāo)記的。

在SpringMVC 中,我們可以使用@PathVariable 來標(biāo)記一個Controller 的處理方法參數(shù),表示該參數(shù)的值將使用URI 模板中對應(yīng)的變量的值來賦值。

//java代碼
@Controller 
@RequestMapping ( "/test/{variable1}" ) 
public class MyController { 
 
  @RequestMapping ( "/showView/{variable2}" ) 
  public ModelAndView showView( @PathVariable String variable1, @PathVariable ( "variable2" ) int variable2) { 
    ModelAndView modelAndView = new ModelAndView(); 
    modelAndView.setViewName( "viewName" ); 
    modelAndView.addObject( " 需要放到 model 中的屬性名稱 " , " 對應(yīng)的屬性值,它是一個對象 " ); 
    return modelAndView; 
  } 
}

在上面的代碼中我們定義了兩個URI 變量,一個是控制器類上的variable1 ,一個是showView 方法上的variable2 ,然后在showView 方法的參數(shù)里面使用@PathVariable 標(biāo)記使用了這兩個變量。所以當(dāng)我們使用/test/hello/showView/2.do 來請求的時候就可以訪問到MyController 的showView 方法,這個時候variable1 就被賦予值hello ,variable2 就被賦予值2 ,然后我們在showView 方法參數(shù)里面標(biāo)注了參數(shù)variable1 和variable2 是來自訪問路徑的path 變量,這樣方法參數(shù)variable1 和variable2 就被分別賦予hello 和2 。方法參數(shù)variable1 是定義為String 類型,variable2 是定義為int 類型,像這種簡單類型在進行賦值的時候Spring 是會幫我們自動轉(zhuǎn)換的,關(guān)于復(fù)雜類型該如何來轉(zhuǎn)換在后續(xù)內(nèi)容中將會講到。

在上面的代碼中我們可以看到在標(biāo)記variable1 為path 變量的時候我們使用的是@PathVariable ,而在標(biāo)記variable2 的時候使用的是@PathVariable(“variable2”) 。這兩者有什么區(qū)別呢?第一種情況就默認(rèn)去URI 模板中找跟參數(shù)名相同的變量,但是這種情況只有在使用debug 模式進行編譯的時候才可以,而第二種情況是明確規(guī)定使用的就是URI 模板中的variable2 變量。當(dāng)不是使用debug 模式進行編譯,或者是所需要使用的變量名跟參數(shù)名不相同的時候,就要使用第二種方式明確指出使用的是URI 模板中的哪個變量。

除了在請求路徑中使用URI 模板,定義變量之外,@RequestMapping 中還支持通配符“* ”。如下面的代碼我就可以使用/myTest/whatever/wildcard.do 訪問到Controller 的testWildcard 方法。

@Controller 
@RequestMapping ( "/myTest" ) 
public class MyController { 
  @RequestMapping ( "*/wildcard" ) 
  public String testWildcard() { 
    System. out .println( "wildcard------------" ); 
    return "wildcard" ; 
  }  
}

(二)使用 @RequestParam 綁定 HttpServletRequest 請求參數(shù)到控制器方法參數(shù)

//Java代碼
@RequestMapping ( "requestParam" ) 
ublic String testRequestParam( @RequestParam(required=false) String name, @RequestParam ( "age" ) int age) { 
  return "requestParam" ; 
}

在上面代碼中利用@RequestParam 從HttpServletRequest 中綁定了參數(shù)name 到控制器方法參數(shù)name ,綁定了參數(shù)age 到控制器方法參數(shù)age 。值得注意的是和@PathVariable 一樣,當(dāng)你沒有明確指定從request 中取哪個參數(shù)時,Spring 在代碼是debug 編譯的情況下會默認(rèn)取更方法參數(shù)同名的參數(shù),如果不是debug 編譯的就會報錯。此外,當(dāng)需要從request 中綁定的參數(shù)和方法的參數(shù)名不相同的時候,也需要在@RequestParam 中明確指出是要綁定哪個參數(shù)。在上面的代碼中如果我訪問/requestParam.do&#63;name=hello&age=1 則Spring 將會把request 請求參數(shù)name 的值hello 賦給對應(yīng)的處理方法參數(shù)name ,把參數(shù)age 的值1 賦給對應(yīng)的處理方法參數(shù)age 。

@RequestParam 中除了指定綁定哪個參數(shù)的屬性value 之外,還有一個屬性required ,它表示所指定的參數(shù)是否必須在request 屬性中存在,默認(rèn)是true ,表示必須存在,當(dāng)不存在時就會報錯。在上面代碼中我們指定了參數(shù)name 的required 的屬性為false ,而沒有指定age 的required 屬性,這時候如果我們訪問/requestParam.do 而沒有傳遞參數(shù)的時候,系統(tǒng)就會拋出異常,因為age 參數(shù)是必須存在的,而我們沒有指定。而如果我們訪問/requestParam.do&#63;age=1 的時候就可以正常訪問,因為我們傳遞了必須的參數(shù)age ,而參數(shù)name 是非必須的,不傳遞也可以。

(三)使用 @CookieValue 綁定 cookie 的值到 Controller 方法參數(shù)

//java代碼
@RequestMapping ( "cookieValue" ) 
public String testCookieValue( @CookieValue ( "hello" ) String cookieValue, @CookieValue String hello) { 
  System. out .println(cookieValue + "-----------" + hello); 
  return "cookieValue" ; 
}

在上面的代碼中我們使用@CookieValue 綁定了cookie 的值到方法參數(shù)上。上面一共綁定了兩個參數(shù),一個是明確指定要綁定的是名稱為hello 的cookie 的值,一個是沒有指定。使用沒有指定的形式的規(guī)則和@PathVariable 、@RequestParam 的規(guī)則是一樣的,即在debug 編譯模式下將自動獲取跟方法參數(shù)名同名的cookie 值。

(四)使用 @RequestHeader 注解綁定 HttpServletRequest 頭信息到 Controller 方法參數(shù)

//java代碼
@RequestMapping ( "testRequestHeader" ) 
public String testRequestHeader( @RequestHeader ( "Host" ) String hostAddr, @RequestHeader String Host, @RequestHeader String host ) { 
  System. out .println(hostAddr + "-----" + Host + "-----" + host ); 
  return "requestHeader" ; 
}

在上面的代碼中我們使用了 @RequestHeader 綁定了 HttpServletRequest 請求頭 host 到 Controller 的方法參數(shù)。上面方法的三個參數(shù)都將會賦予同一個值,由此我們可以知道在綁定請求頭參數(shù)到方法參數(shù)的時候規(guī)則和 @PathVariable 、 @RequestParam 以及 @CookieValue 是一樣的,即沒有指定綁定哪個參數(shù)到方法參數(shù)的時候,在 debug 編譯模式下將使用方法參數(shù)名作為需要綁定的參數(shù)。但是有一點 @RequestHeader 跟另外三種綁定方式是不一樣的,那就是在使用 @RequestHeader 的時候是大小寫不敏感的,即 @RequestHeader(“Host”) 和 @RequestHeader(“host”) 綁定的都是 Host 頭信息。記住在 @PathVariable 、 @RequestParam 和 @CookieValue 中都是大小寫敏感的。

(五) @RequestMapping 的一些高級應(yīng)用

在RequestMapping 中除了指定請求路徑value 屬性外,還有其他的屬性可以指定,如params 、method 和headers 。這樣屬性都可以用于縮小請求的映射范圍。

params屬性

params 屬性用于指定請求參數(shù)的,先看以下代碼。

//java代碼
@RequestMapping (value= "testParams" , params={ "param1=value1" , "param2" , "!param3" }) 
public String testParams() { 
  System. out .println( "test Params..........." ); 
  return "testParams" ; 
}

在上面的代碼中我們用@RequestMapping 的params 屬性指定了三個參數(shù),這些參數(shù)都是針對請求參數(shù)而言的,它們分別表示參數(shù)param1 的值必須等于value1 ,參數(shù)param2 必須存在,值無所謂,參數(shù)param3 必須不存在,只有當(dāng)請求/testParams.do 并且滿足指定的三個參數(shù)條件的時候才能訪問到該方法。所以當(dāng)請求/testParams.do&#63;param1=value1&para;m2=value2 的時候能夠正確訪問到該testParams 方法,當(dāng)請求/testParams.do&#63;param1=value1&para;m2=value2&para;m3=value3 的時候就不能夠正常的訪問到該方法,因為在@RequestMapping 的params 參數(shù)里面指定了參數(shù)param3 是不能存在的。

2. method屬性

method 屬性主要是用于限制能夠訪問的方法類型的。

//java代碼
@RequestMapping (value= "testMethod" , method={RequestMethod. GET , RequestMethod. DELETE }) 
public String testMethod() { 
  return "method" ; 
}

在上面的代碼中就使用method 參數(shù)限制了以GET 或DELETE 方法請求/testMethod.do 的時候才能訪問到該Controller 的testMethod 方法。

3. headers屬性

使用headers 屬性可以通過請求頭信息來縮小@RequestMapping 的映射范圍。

//java 代碼
@RequestMapping (value= "testHeaders" , headers={ "host=localhost" , "Accept" }) 
public String testHeaders() { 
  return "headers" ; 
}

headers 屬性的用法和功能與params 屬性相似。在上面的代碼中當(dāng)請求/testHeaders.do 的時候只有當(dāng)請求頭包含Accept 信息,且請求的host 為localhost 的時候才能正確的訪問到testHeaders 方法。

(六)以 @RequestMapping 標(biāo)記的處理器方法支持的方法參數(shù)和返回類型

1.支持的方法參數(shù)類型

(1 )HttpServlet 對象,主要包括HttpServletRequest 、HttpServletResponse 和HttpSession 對象。 這些參數(shù)Spring 在調(diào)用處理器方法的時候會自動給它們賦值,所以當(dāng)在處理器方法中需要使用到這些對象的時候,可以直接在方法上給定一個方法參數(shù)的申明,然后在方法體里面直接用就可以了。但是有一點需要注意的是在使用HttpSession 對象的時候,如果此時HttpSession 對象還沒有建立起來的話就會有問題。

(2 )Spring 自己的WebRequest 對象。 使用該對象可以訪問到存放在HttpServletRequest 和HttpSession 中的屬性值。

(3 )InputStream 、OutputStream 、Reader 和Writer 。 InputStream 和Reader 是針對HttpServletRequest 而言的,可以從里面取數(shù)據(jù);OutputStream 和Writer 是針對HttpServletResponse 而言的,可以往里面寫數(shù)據(jù)。

(4 )使用@PathVariable 、@RequestParam 、@CookieValue 和@RequestHeader 標(biāo)記的參數(shù)。

(5 )使用@ModelAttribute 標(biāo)記的參數(shù)。

(6 )java.util.Map 、Spring 封裝的Model 和ModelMap 。 這些都可以用來封裝模型數(shù)據(jù),用來給視圖做展示。

(7 )實體類。 可以用來接收上傳的參數(shù)。

(8 )Spring 封裝的MultipartFile 。 用來接收上傳文件的。

(9 )Spring 封裝的Errors 和BindingResult 對象。 這兩個對象參數(shù)必須緊接在需要驗證的實體對象參數(shù)之后,它里面包含了實體對象的驗證結(jié)果。

2. 支持的返回類型

(1 )一個包含模型和視圖的ModelAndView 對象。

(2 )一個模型對象,這主要包括Spring 封裝好的Model 和ModelMap ,以及java.util.Map ,當(dāng)沒有視圖返回的時候視圖名稱將由RequestToViewNameTranslator 來決定。

(3 )一個View 對象。這個時候如果在渲染視圖的過程中模型的話就可以給處理器方法定義一個模型參數(shù),然后在方法體里面往模型中添加值。

(4 )一個String 字符串。這往往代表的是一個視圖名稱。這個時候如果需要在渲染視圖的過程中需要模型的話就可以給處理器方法一個模型參數(shù),然后在方法體里面往模型中添加值就可以了。

(5 )返回值是void 。這種情況一般是我們直接把返回結(jié)果寫到HttpServletResponse 中了,如果沒有寫的話,那么Spring 將會利用RequestToViewNameTranslator 來返回一個對應(yīng)的視圖名稱。如果視圖中需要模型的話,處理方法與返回字符串的情況相同。

(6 )如果處理器方法被注解@ResponseBody 標(biāo)記的話,那么處理器方法的任何返回類型都會通過HttpMessageConverters 轉(zhuǎn)換之后寫到HttpServletResponse 中,而不會像上面的那些情況一樣當(dāng)做視圖或者模型來處理。

(7 )除以上幾種情況之外的其他任何返回類型都會被當(dāng)做模型中的一個屬性來處理,而返回的視圖還是由RequestToViewNameTranslator 來決定,添加到模型中的屬性名稱可以在該方法上用@ModelAttribute(“attributeName”) 來定義,否則將使用返回類型的類名稱的首字母小寫形式來表示。使用@ModelAttribute 標(biāo)記的方法會在@RequestMapping 標(biāo)記的方法執(zhí)行之前執(zhí)行。

(七)使用 @ModelAttribute 和 @SessionAttributes 傳遞和保存數(shù)據(jù)

SpringMVC 支持使用 @ModelAttribute 和 @SessionAttributes 在不同的模型和控制器之間共享數(shù)據(jù)。 @ModelAttribute 主要有兩種使用方式,一種是標(biāo)注在方法上,一種是標(biāo)注在 Controller 方法參數(shù)上。

當(dāng) @ModelAttribute 標(biāo)記在方法上的時候,該方法將在處理器方法執(zhí)行之前執(zhí)行,然后把返回的對象存放在 session 或模型屬性中,屬性名稱可以使用 @ModelAttribute(“attributeName”) 在標(biāo)記方法的時候指定,若未指定,則使用返回類型的類名稱(首字母小寫)作為屬性名稱。關(guān)于 @ModelAttribute 標(biāo)記在方法上時對應(yīng)的屬性是存放在 session 中還是存放在模型中,我們來做一個實驗,看下面一段代碼。

//java代碼
@Controller
@RequestMapping ( "/myTest" )
public class MyController {
  @ModelAttribute ( "hello" )
  public String getModel() {
    System. out .println( "-------------Hello---------" );
    return "world" ;
  } 
  @ModelAttribute ( "intValue" )
  public int getInteger() {
    System. out .println( "-------------intValue---------------" );
    return 10;
  }
  @RequestMapping ( "sayHello" )
  public void sayHello( @ModelAttribute ( "hello" ) String hello, @ModelAttribute ( "intValue" ) int num, @ModelAttribute ( "user2" ) User user, Writer writer, HttpSession session) throws IOException {
    writer.write( "Hello " + hello + " , Hello " + user.getUsername() + num);
    writer.write( "\r" );
    Enumeration enume = session.getAttributeNames();
    while (enume.hasMoreElements())
      writer.write(enume.nextElement() + "\r" );
  }
  @ModelAttribute ( "user2" )
  public User getUser() {
    System. out .println( "---------getUser-------------" );
    return new User(3, "user2" );
  }
}

當(dāng)我們請求 /myTest/sayHello.do 的時候使用 @ModelAttribute 標(biāo)記的方法會先執(zhí)行,然后把它們返回的對象存放到模型中。最終訪問到 sayHello 方法的時候,使用 @ModelAttribute 標(biāo)記的方法參數(shù)都能被正確的注入值。執(zhí)行結(jié)果如下圖所示:

Hello world,Hello user210

由執(zhí)行結(jié)果我們可以看出來,此時 session 中沒有包含任何屬性,也就是說上面的那些對象都是存放在模型屬性中,而不是存放在 session 屬性中。那要如何才能存放在 session 屬性中呢?這個時候我們先引入一個新的概念 @SessionAttributes ,它的用法會在講完 @ModelAttribute 之后介紹,這里我們就先拿來用一下。我們在 MyController 類上加上 @SessionAttributes 屬性標(biāo)記哪些是需要存放到 session 中的??聪旅娴拇a:

//java代碼
@Controller
@RequestMapping ( "/myTest" )
@SessionAttributes (value={ "intValue" , "stringValue" }, types={User. class })
public class MyController {
  @ModelAttribute ( "hello" )
  public String getModel() {
    System. out .println( "-------------Hello---------" );
    return "world" ;
  }
  @ModelAttribute ( "intValue" )
  public int getInteger() {
    System. out .println( "-------------intValue---------------" );
    return 10;
  }
  @RequestMapping ( "sayHello" )
  public void sayHello(Map<string, object=""> map, @ModelAttribute ( "hello" ) String hello, @ModelAttribute ( "intValue" ) int num, @ModelAttribute ( "user2" ) User user, Writer writer, HttpServletRequest request) throws IOException {
    map.put( "stringValue" , "String" );
    writer.write( "Hello " + hello + " , Hello " + user.getUsername() + num);
    writer.write( "\r" );
    HttpSession session = request.getSession();
    Enumeration enume = session.getAttributeNames();
    while (enume.hasMoreElements())
      writer.write(enume.nextElement() + "\r" );
    System. out .println(session);
  }
  @ModelAttribute ( "user2" )
  public User getUser() {
    System. out .println( "---------getUser-------------" );
    return new User(3, "user2" );
  }
}

在上面代碼中我們指定了屬性為 intValue 或 stringValue 或者類型為 User 的都會放到 Session 中,利用上面的代碼當(dāng)我們訪問 /myTest/sayHello.do 的時候,結(jié)果如下:

Hello world,Hello user210

仍然沒有打印出任何 session 屬性,這是怎么回事呢?怎么定義了把模型中屬性名為 intValue 的對象和類型為 User 的對象存到 session 中,而實際上沒有加進去呢?難道我們錯啦?我們當(dāng)然沒有錯,只是在第一次訪問 /myTest/sayHello.do 的時候 @SessionAttributes 定義了需要存放到 session 中的屬性,而且這個模型中也有對應(yīng)的屬性,但是這個時候還沒有加到 session 中,所以 session 中不會有任何屬性,等處理器方法執(zhí)行完成后 Spring 才會把模型中對應(yīng)的屬性添加到 session 中。所以當(dāng)請求第二次的時候就會出現(xiàn)如下結(jié)果:

Hello world,Hello user210

user2

intValue

stringValue

當(dāng) @ModelAttribute 標(biāo)記在處理器方法參數(shù)上的時候,表示該參數(shù)的值將從模型或者 Session 中取對應(yīng)名稱的屬性值,該名稱可以通過 @ModelAttribute(“attributeName”) 來指定,若未指定,則使用參數(shù)類型的類名稱(首字母小寫)作為屬性名稱。

//java代碼
@Controller
@RequestMapping ( "/myTest" )
public class MyController {
 
  @ModelAttribute ( "hello" )
  public String getModel() {
    return "world" ;
  }
  @RequestMapping ( "sayHello" )
  public void sayHello( @ModelAttribute ( "hello" ) String hello, Writer writer) throws IOException {
    writer.write( "Hello " + hello);
  }  
}

在上面代碼中,當(dāng)我們請求/myTest/sayHello.do 的時候,由于MyController 中的方法getModel 使用了注解@ModelAttribute 進行標(biāo)記,所以在執(zhí)行請求方法sayHello 之前會先執(zhí)行g(shù)etModel 方法,這個時候getModel 方法返回一個字符串world 并把它以屬性名hello 保存在模型中,接下來訪問請求方法sayHello 的時候,該方法的hello 參數(shù)使用@ModelAttribute(“hello”) 進行標(biāo)記,這意味著將從session 或者模型中取屬性名稱為hello 的屬性值賦給hello 參數(shù),所以這里hello 參數(shù)將被賦予值world ,所以請求完成后將會在頁面上看到Hello world 字符串。

@SessionAttributes 用于標(biāo)記需要在Session 中使用到的數(shù)據(jù),包括從Session 中取數(shù)據(jù)和存數(shù)據(jù)。@SessionAttributes 一般是標(biāo)記在Controller 類上的,可以通過名稱、類型或者名稱加類型的形式來指定哪些屬性是需要存放在session 中的。

//java代碼
@Controller
@RequestMapping ( "/myTest" )
@SessionAttributes (value={ "user1" , "blog1" }, types={User. class , Blog. class })
public class MyController {
 
  @RequestMapping ( "setSessionAttribute" )
  public void setSessionAttribute(Map<string, object=""> map, Writer writer) throws IOException {
    User user = new User(1, "user" );
    User user1 = new User(2, "user1" );
    Blog blog = new Blog(1, "blog" );
    Blog blog1 = new Blog(2, "blog1" );
    map.put( "user" , user);
    map.put( "user1" , user1);
    map.put( "blog" , blog);
    map.put( "blog1" , blog1);
    writer.write( "over." );
  }
  @RequestMapping ( "useSessionAttribute" )
  public void useSessionAttribute(Writer writer, @ModelAttribute ( "user1" ) User user1, @ModelAttribute ( "blog1" ) Blog blog1) throws IOException {
    writer.write(user1.getId() + "--------" + user1.getUsername());
    writer.write( "\r" );
    writer.write(blog1.getId() + "--------" + blog1.getTitle());
  }
  @RequestMapping ( "useSessionAttribute2" )
  public void useSessionAttribute(Writer writer, @ModelAttribute ( "user1" ) User user1, @ModelAttribute ( "blog1" ) Blog blog1, @ModelAttribute User user, HttpSession session) throws IOException {
    writer.write(user1.getId() + "--------" + user1.getUsername());
    writer.write( "\r" );
    writer.write(blog1.getId() + "--------" + blog1.getTitle());
    writer.write( "\r" );
    writer.write(user.getId() + "---------" + user.getUsername());
    writer.write( "\r" );
    Enumeration enume = session.getAttributeNames();
    while (enume.hasMoreElements())
      writer.write(enume.nextElement() + " \r" );
  }
  @RequestMapping ( "useSessionAttribute3" )
  public void useSessionAttribute( @ModelAttribute ( "user2" ) User user) {
 
  }
}

在上面代碼中我們可以看到在MyController 上面使用了@SessionAttributes 標(biāo)記了需要使用到的Session 屬性。

可以通過名稱和類型指定需要存放到Session 中的屬性,對應(yīng)@SessionAttributes 注解的value 和types 屬性。當(dāng)使用的是types 屬性的時候,那么使用的Session 屬性名稱將會是對應(yīng)類型的名稱(首字母小寫)。

當(dāng)value 和types 兩個屬性都使用到了的時候,這時候取的是它們的并集,而不是交集,所以上面代碼中指定要存放在Session 中的屬性有名稱為user1 或blog1 的對象,或類型為User 或Blog 的對象。

在上面代碼中我們首先訪問/myTest/setSessionAttribute.do ,該請求將會請求到MyController 的setSessionAttribute 方法,在該方法中,我們往模型里面添加了user 、user1 、blog 和blog1 四個屬性,因為它們或跟類上的@SessionAttributes 定義的需要存到session 中的屬性名稱相同或類型相同,所以在請求完成后這四個屬性都將添加到session 屬性中。

接下來訪問/myTest/useSessionAttribute.do ,該請求將會請求MyController 的useSessionAttribute(Writer writer, @ModelAttribute(“user1”) User user1, @ModelAttribute(“blog1”) Blog blog) 方法,該方法參數(shù)中用@ModelAttribute 指定了參數(shù)user1 和參數(shù)blog1 是需要從session 或模型中綁定的,恰好這個時候session 中已經(jīng)有了這兩個屬性,所以這個時候在方法執(zhí)行之前會先綁定這兩個參數(shù)。執(zhí)行結(jié)果如下圖所示:

2————-user1

2————-blog1

接下來訪問/myTest/useSessionAttribute2.do ,這個時候請求的是上面代碼中對應(yīng)的第二個useSessionAttribute 方法,方法參數(shù)user 、user1 和blog1 用@ModelAttribute 聲明了需要session 或模型屬性注入,我們知道在請求/myTest/setSessionAttribute.do 的時候這些屬性都已經(jīng)添加到了session 中,所以該請求的結(jié)果會如下圖所示:

2————-user1

2————-blog1

1————-user

blog

user

user1

blog1

接下來訪問/myTest/useSessionAttribute3.do ,這個時候請求的是上面代碼中對應(yīng)的第三個useSessionAttribute 方法,我們可以看到該方法的方法參數(shù)user 使用了@ModelAttribute(“user2”) 進行標(biāo)記,表示user 需要session 中的user2 屬性來注入,但是這個時候我們知道session 中是不存在user2 屬性的,所以這個時候就會報錯了。執(zhí)行結(jié)果如圖所示:

一文教你如何在java中使用SpringMVC

(八)定制自己的類型轉(zhuǎn)換器

在通過處理器方法參數(shù)接收 request 請求參數(shù)綁定數(shù)據(jù)的時候,對于一些簡單的數(shù)據(jù)類型 Spring 會幫我們自動進行類型轉(zhuǎn)換,而對于一些復(fù)雜的類型由于 Spring 沒法識別,所以也就不能幫助我們進行自動轉(zhuǎn)換了,這個時候如果我們需要 Spring 來幫我們自動轉(zhuǎn)換的話就需要我們給 Spring 注冊一個對特定類型的識別轉(zhuǎn)換器。

Spring 允許我們提供兩種類型的識別轉(zhuǎn)換器,一種是注冊在 Controller 中的,一種是注冊在 SpringMVC 的配置文件中。聰明的讀者看到這里應(yīng)該可以想到它們的區(qū)別了,定義在 Controller 中的是局部的,只在當(dāng)前 Controller 中有效,而放在 SpringMVC 配置文件中的是全局的,所有 Controller 都可以拿來使用。

在 @InitBinder 標(biāo)記的方法中定義局部的類型轉(zhuǎn)換器

我們可以使用 @InitBinder 注解標(biāo)注在 Controller 方法上,然后在方法體里面注冊數(shù)據(jù)綁定的轉(zhuǎn)換器,這主要是通過 WebDataBinder 進行的。

我們可以給需要注冊數(shù)據(jù)綁定的轉(zhuǎn)換器的方法一個 WebDataBinder 參數(shù),然后給該方法加上 @InitBinder 注解,這樣當(dāng)該 Controller 中在處理請求方法時如果發(fā)現(xiàn)有不能解析的對象的時候,就會看該類中是否有使用 @InitBinder 標(biāo)記的方法,如果有就會執(zhí)行該方法,然后看里面定義的類型轉(zhuǎn)換器是否與當(dāng)前需要的類型匹配。

//java代碼
@Controller 
@RequestMapping ( "/myTest" ) 
public class MyController { 
 
  @InitBinder 
  public void dataBinder(WebDataBinder binder) { 
    DateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" ); 
    PropertyEditor propertyEditor = new CustomDateEditor(dateFormat, true ); // 第二個參數(shù)表示是否允許為空 
    binder.registerCustomEditor(Date. class , propertyEditor); 
  } 
  @RequestMapping ( "dataBinder/{date}" ) 
  public void testDate( @PathVariable Date date, Writer writer) throws IOException { 
    writer.write(String.valueOf (date.getTime())); 
  } 
}


在上面的代碼中當(dāng)我們請求 /myTest/dataBinder/20121212.do 的時候, Spring 就會利用 @InitBinder 標(biāo)記的方法里面定義的類型轉(zhuǎn)換器把字符串 20121212 轉(zhuǎn)換為一個 Date 對象。

這樣定義的類型轉(zhuǎn)換器是局部的類型轉(zhuǎn)換器,一旦出了這個 Controller 就不會再起作用。類型轉(zhuǎn)換器是通過 WebDataBinder 對象的 registerCustomEditor 方法來注冊的,要實現(xiàn)自己的類型轉(zhuǎn)換器就要實現(xiàn)自己的 PropertyEditor 對象。 Spring 已經(jīng)給我們提供了一些常用的屬性編輯器,如 CustomDateEditor 、 CustomBooleanEditor 等。

PropertyEditor 是一個接口,要實現(xiàn)自己的 PropertyEditor 類我們可以實現(xiàn)這個接口,然后實現(xiàn)里面的方法。

但是 PropertyEditor 里面定義的方法太多了,這樣做比較麻煩。在 java 中有一個封裝類是實現(xiàn)了 PropertyEditor 接口的,它是 PropertyEditorSupport 類。所以如果需要實現(xiàn)自己的 PropertyEditor 的時候只需要繼承 PropertyEditorSupport 類,然后重寫其中的一些方法。

一般就是重寫 setAsText 和 getAsText 方法就可以了, setAsText 方法是用于把字符串類型的值轉(zhuǎn)換為對應(yīng)的對象的,而 getAsText 方法是用于把對象當(dāng)做字符串來返回的。在 setAsText 中我們一般先把字符串類型的對象轉(zhuǎn)為特定的對象,然后利用 PropertyEditor 的 setValue 方法設(shè)定轉(zhuǎn)換后的值。在 getAsText 方法中一般先使用 getValue 方法取代當(dāng)前的對象,然后把它轉(zhuǎn)換為字符串后再返回給 getAsText 方法。下面是一個示例:

//java代碼
@InitBinder 
public void dataBinder(WebDataBinder binder) { 
  // 定義一個 User 屬性編輯器 
  PropertyEditor userEditor = new PropertyEditorSupport() { 
    @Override 
    public String getAsText() { 
     // TODO Auto-generated method stub 
     User user = (User) getValue(); 
     return user.getUsername(); 
    } 
    @Override 
    public void setAsText(String userStr) throws IllegalArgumentException { 
     // TODO Auto-generated method stub 
     User user = new User(1, userStr); 
     setValue(user); 
    } 
  }; 
  // 使用 WebDataBinder 注冊 User 類型的屬性編輯器 
  binder.registerCustomEditor(User. class , userEditor); 
}

實現(xiàn) WebBindingInitializer 接口定義全局的類型轉(zhuǎn)換器

如果需要定義全局的類型轉(zhuǎn)換器就需要實現(xiàn)自己的 WebBindingInitializer 對象,然后把該對象注入到

AnnotationMethodHandlerAdapter 中,這樣 Spring 在遇到自己不能解析的對象的時候就會到全局的 WebBindingInitializer 的 initBinder 方法中去找,每次遇到不認(rèn)識的對象時, initBinder 方法都會被執(zhí)行一遍。

//java代碼
public class MyWebBindingInitializer implements WebBindingInitializer { 
 
  @Override 
  public void initBinder(WebDataBinder binder, WebRequest request) { 
    // TODO Auto-generated method stub 
    DateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" ); 
    PropertyEditor propertyEditor = new CustomDateEditor(dateFormat, true ); 
    binder.registerCustomEditor(Date. class , propertyEditor); 
  } 
}

定義了這么一個 WebBindingInitializer 對象之后 Spring 還是不能識別其中指定的對象,這是因為我們只是定義了 WebBindingInitializer 對象,還沒有把它交給 Spring , Spring 不知道該去哪里找解析器。要讓 Spring 能夠識別還需要我們在 SpringMVC 的配置文件中定義一個 AnnotationMethodHandlerAdapter 類型的 bean 對象,然后利用自己定義的 WebBindingInitializer 覆蓋它的默認(rèn)屬性 webBindingInitializer 。

<!--html代碼-->
< bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > 
  < property name = "webBindingInitializer" > 
    < bean class = "com.host.app.web.util.MyWebBindingInitializer" /> 
  </ property > 
</ bean >

觸發(fā)數(shù)據(jù)綁定方法的時間

當(dāng)Controller處理器方法參數(shù)使用@RequestParam、@PathVariable、@RequestHeader、@CookieValue和@ModelAttribute標(biāo)記的時候都會觸發(fā)initBinder方法的執(zhí)行,這包括使用WebBindingInitializer定義的全局方法和在Controller中使用@InitBinder標(biāo)記的局部方法。而且每個使用了這幾個注解標(biāo)記的參數(shù)都會觸發(fā)一次initBinder方法的執(zhí)行,這也意味著有幾個參數(shù)使用了上述注解就會觸發(fā)幾次initBinder方法的執(zhí)行。

上述就是小編為大家分享的一文教你如何在java中使用SpringMVC了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI