Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-34323

Create utility to convert constants class into constants bean at runtime to simplify constant referring in JSP EL



      Generate constants bean class at runtime based on the target constants class. All constants values are hardwired into the new generated class method code to get the best performance.

      For example, for a constants class like this:

      class Constants {
      public static String STRING_VALUE = "Some string content";
      public static int INT_VALUE = 100;

      By doing following method call :

      Object constantsBean = ConstantsBeanUtil.toConstantsBean(Constants.class);

      constantsBean is a instance of the following runtime generated class :

      class ConstantsBean {
      public String getSTRING_VALUE()

      { return Constants.STRING_VALUE; }

      public int getINT_VALUE()

      { return Constants.INT_VALUE; }


      The class generation and instance bean creation are weakly cached, as long as the Constants class is not gcable. The cached instance bean can only be considered as a weak singleton. Under concurrent initialization, different threads may see different instances. But the class cache is a strong singleton per classloader.

      In jsp, the following pattern can be used to simplify EL constants referring.

      // Initialization
      Object constantsBean = ConstantsBeanUtil.toConstantsBean(Constants.class);

      pageContext.setAttribute("constantsBean", constantsBean);

      String value is ${constantsBean.STRING_VALUE}.
      Int value is ${constantsBean.INT_VALUE}.


          Issue Links



              • Assignee:
                justin.choi Justin Choi
                shuyang.zhou Shuyang Zhou
                Recent user:
                Shalaka Tendulkar
                Participants of an Issue:
              • Votes:
                0 Vote for this issue
                0 Start watching this issue


                • Created:


                  Version Package
                  6.1.30 EE GA3
                  6.2.0 CE M5