5

I've spent a day looking for the right solution, but no luck! The question is that how to call java constant variables from jsp with el ${bean.objectName} for example. What is the best practice?

I wonder if this is doable, im quite new to Spring and jsp.

Constant class:

public class RNConstant {
     public static final String HELLO_WORLD = "Hello World again!";
     public static final String DEFAULT_LOCALE = "id_ID";
     public static final String CONTEXT_PATH_SOAP_SR = "soap.sr";
}

Expectation in jsp to be called with EL

 <p>${RNConstant.HELLO_WORLD}</p>

I could do this with scriptlet as below, but i could not get this working if it runs in weblogic. This works in apache tomcat v7 or v8

<%@ page import="static id.co.telkom.common.RNConstant.*" %>
 ...
 ...
<%= HELLO_WORLD %>

Error in weblogic

home.jsp:2:18: Syntax error on token "static", Identifier expected after this token
<%@ page import="static id.co.telkom.common.RNConstant.*" %>
             ^-------------------------------------^
home.jsp:11:19: HELLO_WORLD cannot be resolved
Hello world!  <%=HELLO_WORLD%>
                     ^--------^

java version: 1.6

pom.xml

 spring
 <version>1.0.0-BUILD-SNAPSHOT</version>
 <properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.2.8.RELEASE</org.springframework-version>
    <org.springjs-version>2.0.5.RELEASE</org.springjs-version>
    <org.springws-version>2.2.1.RELEASE</org.springws-version>
    <org.springsecurity-version>3.2.3.RELEASE</org.springsecurity-version>
    <jackson-version>1.9.10</jackson-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>

Scriplet issue was solved with below codes, and Content of RNConstant is still the same.

<%@ page import="id.co.telkom.common.RNConstant" %>
...
...
<%= RNConstant.HELLO_WORLD %>

Cheers,

Hendry

Adrianus Hendry
  • 143
  • 2
  • 9
  • I am confused, can't you access them by using model.addAttribute("nameuwishinjsp",nameofvariableinjava); . Am I incorrect? – We are Borg May 29 '15 at 11:46
  • @WeareBorg yes we could do that, however, what im trying to achieve is that to create a constant class that could be called directly and globally from .jsp files – Adrianus Hendry May 31 '15 at 03:54

2 Answers2

6

Keep the import statement simple

<%@ page import="static id.co.telkom.common.RNConstant.*" %>

Remove ".*" after RNConstant. Also remove static word in the beginning.

<%@ page import="id.co.telkom.common.RNConstant" %>. 

To call HELLO_WORLD constant use

<p>${RNConstant.HELLO_WORLD}</p> <p>${RNConstant.HELLO_WORLD}</p>
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
gujralam
  • 187
  • 1
  • 12
  • @JordiCastilla editted, should use scriplet tag to call constant class instead of EL. – Adrianus Hendry Jun 03 '15 at 02:12
  • @AdrianusHendry your question was how to reference constants using Expression Language. The answer given addresses a different issue and hence should not be accepted as an answer. – solimant Jun 18 '15 at 22:03
1

Expectation in jsp to be called with EL

<p>${RNConstant.HELLO_WORLD}</p>

EL checks the bean and translates HELLOWORLD to getHELLOWORLD() because specs says that attributes must be accessed in this way, so you must create a getter or visibility for constants will be limited in jsp view:

public class RNConstant 
{    
    public final static String HELLO_WORLD = "Hello World again!";

    public static String getHELLO_WORLD() {
        return HELLO_WORLD;
    }   
}

If you can't create getters check this answer

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • hi, but the scriplet works in apache tomcat v8 server. i could not get that getter would be the cause – Adrianus Hendry May 29 '15 at 09:05
  • 2
    Keep the import statement simple <%@ page import="id.co.telkom.common.RNConstant" %>. Remove ".*" after RNConstant and also remove static word in the beginning. To call HELLO_WORLD constant use

    ${RNConstant.HELLO_WORLD}

    ${RNConstant.HELLO_WORLD}

    – gujralam May 29 '15 at 09:44
  • 1
    @gujralam hi, this does not solve the issue. The error gone, but value does not come up. No error message on the weblogic server log – Adrianus Hendry May 29 '15 at 10:07
  • @gujralam this should be posted as an answer – Jordi Castilla May 29 '15 at 10:08
  • 1
    @gujralam Hi thanks, it works now, it has to be printed using scriplet tag as well such as <%= RNConstant.HELLO_WORLD %> you should write it as an answer. – Adrianus Hendry May 29 '15 at 10:16
  • Thanks Adrianus. I have added that as a answer. Please accept the answer. – gujralam May 29 '15 at 10:39