单例会话Bean

时间:2017-05-15 19:50:08

标签: java ejb

我正在尝试为单个会话bean设置一个基本模板。然后,我采用了#34;计数器架构中的示例":

https://docs.oracle.com/javaee/7/tutorial/ejb-basicexamples002.htm

但是,在运行示例时,我无法打印count.hitCount的值。

我发现访问变量时没有getter的常见问题。例如,在 index.html

Module Module1


    Sub Main()
        Dim denary As Integer
        Dim binary As String = " "
        Dim x As Integer
        Dim y As Integer = 0
        Console.WriteLine("What is the denary number?") 'Asks the user what number denary number they want converted
        denary = Console.Read()

        While denary > 0 'Calculates the binary number, but reversed
            If denary Mod 2 = 0 Then
                binary = binary + "0"
                denary = denary / 2
            Else
                binary = binary + "1"
                denary = denary / 2
            End If
        End While

        Console.WriteLine("The binary equivalent is:" & binary) 'Prints the binary number in reverse
        Console.ReadLine()
        x = Len(binary)
        While x > 0
            Console.WriteLine(binary(x)) 'Print the correct binary equivalent (Not working)
            Console.ReadLine()
            x = x - 1
        End While


    End Sub

End Module

但是,在 Count.java 中包含了getter getHitCount():

<ui:define name="title">
        This page has been accessed #{count.hitCount} time(s).
</ui:define>

最后, CounterBean.java 会增加变量:

@Named
@SessionScoped
public class Count implements Serializable {

    @EJB
    private CounterBean counterBean;

    private int hitCount;

    public Count() {
        this.hitCount = 0;
    }

    public int getHitCount() {
        hitCount = counterBean.getHits();
        return hitCount;
    }
    public void setHitCount(int newHits) {
        this.hitCount = newHits;
    }

感谢您的帮助和评论,我们非常感谢。

1 个答案:

答案 0 :(得分:1)

带有计时器显示的facelet:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
  <h:head>
    <title>Facelet Title</title>
</h:head>
  <h:body>
    This page displayed #{counterController.hitCount} times.
  </h:body>
</html>

CounterController@RequestScoped bean,用CounterBean.hitCount方法增加@PostConstruct

package x;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@Named( value = "counterController" )
@RequestScoped
public class counterController
{

  @EJB
  private CounterBean counterBean;

  @PostConstruct
  public void initialize()
  {
    counterBean.incHitCount();  
  }

  public int getHitCount()
  {
    return counterBean.getHitCount();
  }

  public CounterController()
  {
  }

}

CounterBean是用于存储hitCount的@Singleton EJB。 @StartUp注释是一个热切构造的bean(在应用程序启动时创建,在接受任何客户端请求之前):

package x;

import javax.ejb.Singleton;
import javax.ejb.Startup;
import lombok.Data;

@Singleton
@Startup
public class CounterBean
{
  private int hitCount;

  public int getHitCount()
  {
    return hitCount;
  }

  public void incHitCount()
  {
    hitCount++;
  }

  public CounterBean()
  {}
}