Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call java constant variables from jsp expression language in Spring?

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

like image 977
Adrianus Hendry Avatar asked May 29 '15 09:05

Adrianus Hendry


1 Answers

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>
like image 138
gujralam Avatar answered Nov 14 '22 21:11

gujralam