1. 程式人生 > >J2EE 第四周(03.26-04.01)

J2EE 第四周(03.26-04.01)

ava 4.0 exc session 單個 you web 組成 serve

1.分析hello.java

/**
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
 *
 * You may not modify, use, reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * https://github.com/javaee/tutorial-examples/LICENSE.txt
 */
package javaeetutorial.hello1;


import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named @RequestScoped public class Hello { private String name; public Hello() { } public String getName() { return name; } public void setName(String user_name) { this.name = user_name; } }

  代碼具體地址:https://github.com/javaee/tutorial-examples/blob/master/web/jsf/hello1/src/main/java/javaeetutorial/hello1/Hello.java

Hello類叫做管理bean類,它為facelets頁面表達式所使用的name屬性提供了getter和setter方法,默認該facelets頁面表達式引用的是Hello類的名字,不過第一個字母是小寫字母(例如:hello.name)。

如果你使用的是默認的bean類的類名,你註解可以用@Model來替代@Named和@RequestScoped。@Model註釋稱為原型,是一個包含其他註釋的註釋的術語。

在 Hello.java類中,註解javax.inject.Named和javax.enterprise.context.RequestScoped使用請求scope來標識Hello類為管理bean類。scope定義應用程序數據是如何保存和共享的。

在JSF中最常用的scope如下:

Request(@RequestScoped):請求scope在Web應用程序中的單個HTTP請求期間仍然存在。像hello1應用,該應用由單個請求和響應組成,bean使用請求scope。

Session (@SessionScoped):會話scope持續存在於Web應用程序中的多個HTTP請求中。當應用程序包含需要維護數據的多個請求和響應時,bean使用會話scope。 

Application (@ApplicationScoped):應用程序scope在所有用戶與Web應用程序的交互中持久存在。

文章來源:http://www.cnblogs.com/zgq0/p/8685612.html

2.

J2EE 第四周(03.26-04.01)