第四周:分析hello.java

 代码分析:

 该段代码声明了一个Hello公有类,该类中定义了一个私有变量name,并对变量name写了对应的get和set方法。类中还有一个空的构造方法hello()。

   注解javax.enterprise.context.RequestScoped和javax.inject.Named使用请求scope来标识Hello类为管理bean类。

 (1)scope定义应用程序数据是如何保存和共享的。

 常见的scope有如下几类: 

1、applicationScoped,就是在整个应用级别存储对应Bean的信息。
2、SessionScoped,就是在Session级别保存这个Bean中的内容。
3、ViewScoped,在这里一个View就是指展现在用户面前的一个页面,只要用户在这个View当中那么对应的manage bean中的数据就是可以被保存的。
4、RequestScoped,在每次请求服务器过程中会保存输入值,点击helloForRequestScoped.xhtml页面对应的提交按钮我们发现新页面当中的提交次数永远是1,也就是说RequestScoped的范围就是在从请求开始到请求结束,如果再次请求那么数据将被更新。
5、NoneScoped就是任何情况下都不存储manage bean中的数据

 (2)@Named

  @Named和Spring的@Component功能相同。可以有值,没有值时生成的Bean名称默认和类名相同。

    例:a、@Named public class Person  

        该bean的名称就是person。

    b、@Named("p") public class Person  

        如果指定名称,那么就是指定的名称“p”。

② 具体代码如下: 

复制代码
 1 /**
 2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
 3  *
 4  * You may not modify, use, reproduce, or distribute this software except in
 5  * compliance with  the terms of the License at:
 6  * https://github.com/javaee/tutorial-examples/LICENSE.txt
 7  */
 8 package javaeetutorial.hello1;
 9 
10 
11 import javax.enterprise.context.RequestScoped;
12 import javax.inject.Named;
13 
14 @Named
15 @RequestScoped
16 public class Hello {
17 
18     private String name;
19 
20     public Hello() {
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String user_name) {
28         this.name = user_name;
29     }
30 }
复制代码

③ 代码下载链接:https://github.com/javaee/tutorial-examples/tree/master/web/jsf/hello1

     

原文地址:https://www.cnblogs.com/MnineJane/p/8690427.html