搜尋
首頁Javajava教程springmvc常用註解

springmvc常用註解

Jul 20, 2019 pm 04:18 PM
javaspring

springmvc常用註解

推薦教學:Spring教學

##一、元件型註解:

1、@Component 在類別定義之前加上@Component註解,他會被spring容器識別,並轉為bean。

2、@Repository 對Dao實作類別進行註解(特殊的@Component)

3、@Service 用於註解, (特殊的@Component)

springmvc常用註解

4、@Controller 用於控制層註解 , (特殊的@Component)

以上四種註解都是註解在類別上的,被註解的類別將被spring初始話為一個bean,然後統一管理。

二、請求與參數型註解:

1、@RequestMapping:用於處理請求位址映射,可以作用於類別和方法上。

  ●value:定義request請求的對應位址

##  ●method:定義地request址請求的方式,包括【GET , POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.】預設接受get請求,如果請求方式和定義的方式不一樣則請求無法成功。

  ●params:定義request請求中必須包含的參數值。

  ●headers:定義request請求中必須包含某些指定的請求頭,如:RequestMapping(value = "/something", headers = "content-type=text/*" )說明請求中必須包含"text/html", "text/plain"這中類型的Content-type頭,才是符合的請求。

  ●consumes:定義請求提交內容的類型。

springmvc常用註解  ●produces:指定傳回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才傳回

@RequestMapping(value="/requestTest.do",params = {"name=sdf"},headers = {"Accept-Encoding=gzip, deflate, br"},method = RequestMethod.GET)
     public String getIndex(){
         System.out.println("请求成功");
         return "index";
     }

  上面程式碼表示請求的方式為GET請求,請求參數必須包含name=sdf此參數,然後請求頭中必須有 Accept-Encoding=gzip, deflate, br這個型別頭。

 這樣透過註解就能對一個請求進行約束了。

2.@RequestParam:用於取得傳入參數的值

  ●value:參數的名稱

  ●required:定義此傳入參數是否必須,預設為true,(和@RequestMapping的params屬性有點類似)

@RequestMapping("/requestParams1.do")
    public String requestParams1(@RequestParam(required = false) String name){
        System.out.println("name = "+name);
        return "index";
    }
    @RequestMapping("/requestParams2.do")
    public String requestParams2(@RequestParam(value = "name",required = false) String names){
        System.out.println("name = "+names);
        return "index";
    }
  兩種請入參方式是一樣的,顯示宣告value的名稱時,入參參數名和value一樣,沒有顯示宣告的話,像第一種方式宣告的,入參參數名和函數參數變數名一樣。

3.@PathViriable:用於定義路徑參數值
●value:參數的名稱

  ●required:定義傳入參數是否為必須值

@RequestMapping("/{myname}/pathVariable2.do")    public String pathVariable2(@PathVariable(value = "myname") String name){
        System.out.println("myname = "+name);        return "index";
    }
 這個路徑宣告了{myname}作為路徑參數,那麼這段路徑將為任意值,@PathVariable將可以根據value取得路徑的值。

springmvc常用註解

4.@ResponseBody:作用於方法上,可以將整個返回結果以某種格式傳回,如json或xml格式。

 @RequestMapping("/{myname}/pathVariable2.do")
      @ResponseBody
      public String pathVariable2(@PathVariable(value = "myname") String name){
          System.out.println("myname = "+name);
          return "index";
      }

######################  它傳回的不是一個頁面,而是把字串“ index」直接在頁面印出來了,這其實和如下程式碼時類似的。 ######
PrintWriter out = resp.getWriter();
 out.print("index");
 out.flush();

5、@CookieValue:用于获取请求的Cookie值

 @RequestMapping("/requestParams.do")
      public String requestParams(@CookieValue("JSESSIONID") String cookie){
          return "index";
      }

6、@ModelAttribute:

  用于把参数保存到model中,可以注解方法或参数,注解在方法上的时候,该方法将在处理器方法执行之前执行,然后把返回的对象存放在 session(前提时要有@SessionAttributes注解) 或模型属性中,@ModelAttribute(“attributeName”) 在标记方法的时候指定,若未指定,则使用返回类型的类名称(首字母小写)作为属性名称。 

@ModelAttribute("user")
    public UserEntity getUser(){
        UserEntity userEntityr = new UserEntity();
        userEntityr.setUsername("asdf");
        return userEntityr;
    }

    @RequestMapping("/modelTest.do")
    public String getUsers(@ModelAttribute("user") UserEntity user){
        System.out.println(user.getUsername());
        return "/index";
    }

  如上代码中,使用了@ModelAttribute("user")注解,在执行控制器前执行,然后将生成一个名称为user的model数据,在控制器中我们通过注解在参数上的@ModelAttribute获取参数,然后将model应用到控制器中,在jsp页面中我们同样可以使用它,

 <body>
      ${user.username}
 </body>

7、@SessionAttributes

  默认情况下Spring MVC将模型中的数据存储到request域中。当一个请求结束后,数据就失效了。如果要跨页面使用。那么需要使用到session。而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中。配合@ModelAttribute("user")使用的时候,会将对应的名称的model值存到session中,

@Controller
@RequestMapping("/test")
@SessionAttributes(value = {"user","test1"})
public class LoginController{
    @ModelAttribute("user")
    public UserEntity getUser(){
        UserEntity userEntityr = new UserEntity();
        userEntityr.setUsername("asdf");
        return userEntityr;
    }

    @RequestMapping("/modelTest.do")
    public String getUsers(@ModelAttribute("user") UserEntity user ,HttpSession session){
        System.out.println(user.getUsername());
        System.out.println(session.getAttribute("user"));
        return "/index";
    }
}

  结合上一个例子的代码,加了@SessionAttributes注解,然后请求了两次,第一次session中不存在属性名为user的值,第二次请求的时候发现session中又有了,这是因为,这是因为第一次请求时,model数据还未保存到session中请求结束返回的时候才保存,在第二次请求的时候已经可以获取上一次的model了

1068779-20171027175359008-1504110330 (1).png

注意:@ModelAttribute("user") UserEntity user获取注解内容的时候,会先查询session中是否有对应的属性值,没有才去查询Model。

以上是springmvc常用註解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具