0

I am using heavily jstl in all our jsp pages instead of jsp scriptlets, but i could not be able find what is the alternative for jsp expressions in jstl.

I have my code snippet

mycode.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

<c:set  var="appId" value="${requestScope.appId}"/>
<h1>${appId}</h1>
<BR/>
<a href="/admin/loadData">Load Data</a>
||&nbsp;&nbsp <a href="<%= UrlConstants.ADJUSTMENT_NOTE_LEDGER_SUGGEST_BOX%>">Load Demo</a>

May i know what is the alternative for the line <%= UrlConstants.ADJUSTMENT_NOTE_LEDGER_SUGGEST_BOX%> in jstl. here UrlConstants is my interface which is used declaring some url constants

majji
  • 169
  • 2
  • 3
  • 19

1 Answers1

0

edited

You can not do this with JSTL standard.

Using scriptlet, be it a line <% =%> or more <% %> JSP goes through the translation phase of JSP page to then be compiled into a servlet. During the translation of the instructions are inserted into the Scriptlet _jspService() method in the same way on the Web page. The _jspService () method Corresponds to the body of the JSP page.

In your case, it's like generated a line like this:

out.write("<a href=\" +  UrlConstants.ADJUSTMENT_NOTE_LEDGER_SUGGEST_BOX + "\" >Load Demo</a>" );

So this works with scriptlets. With JSTL is a little different.

The EL allows page authors to use expressions to dynamically read application data stored in JavaBeans components, various data structures, and implicit objects. These expressions are evaluated at the appropriate time and supports immediate and deferred evaluation. Immediate evaluation means that the expression is evaluated and the result returned as soon as the page is first rendered.

Deferred evaluation means that the technology using the expression language can use its own machinery to evaluate the expression sometime later during the page’s lifecycle, whenever it is appropriate to do so. In the case of JavaServer Faces technology.

All expressions using the ${} syntax are evaluated immediately, this is made by ELResolver, ELContext and other objects. At the center of the EL machinery is the extensible ELResolver class.

A class that implements ELResolver defines how to resolve expressions referring to a particular type of object or property. In terms of the following expression, a BeanELResolver instance is called the first time to find the base object, employee, which is a JavaBeans component. Once the resolver finds the object, it is called again to resolve the property, lName of the employee object.

${employee.lName}

The unified EL includes a set of standard resolver implementations: ArrayELResolver, BeanELResolver, ListELResolver, MapELResolver and ResourceBundleELResolver. JSP 2.1 provides two EL resolvers to handle expressions that reference these objects: ImplicitObjectELResolver and ScopedAttributeELResolver.

Depending on the technology using the unified EL, other resolvers might be available. In addition, application developers can add their own implementations of ELResolver to support resolution of expressions not already supported by the unified EL by registering them with an application.

So, by default EL does not evaluate the constant value of the mode you want.

See this link for more details: http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html see this link How to reference constants in EL? There have several options to solve your problem

Community
  • 1
  • 1
Lucas Oliveira
  • 833
  • 1
  • 10
  • 24
  • Thanks for your reply, but this is not my expected answer for this question, Is there any alternative for above jsp expression(please re-read my query) – majji Nov 27 '13 at 13:32
  • 1
    @majji: accessing static fields of a class in impossible with the JSP EL, until the version 3 of the spec. If you're using a Java EE 7 container such as Tomcat 8, it's possible, but not before. See http://stackoverflow.com/questions/20234337/why-am-i-not-able-to-find-static-methods-through-expression-language/20236571#20236571 – JB Nizet Nov 27 '13 at 17:06