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

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

I wonder if this can be done, im completely new to Spring and jsp.

Persistent 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";
}

      

Waiting in jsp to be called with EL

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

      

I could do it with a script like below, but I couldn't get this to work if it works 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>

      

The script issue was resolved with the codes below and the contents of the RNConstant still remain.

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

      

Greetings,

Hendry

+3


source to share


2 answers


Just keep the import statement

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

      

Remove the ". *" After RNConstant

. Also remove the static word at the beginning.



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

      

To cause permanent use of HELLO_WORLD

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

      

+4


source


Waiting in jsp to be called with EL

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

      

EL checks and bean translates HELLOWORLD

into getHELLOWORLD()

, because the spec says that access to these attributes must be in such a way, so you have to create getter

or to visibility constants will be limited to the representation of jsp:



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

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

      

If you cannot create getters check this answer

+1


source







All Articles