Feedback Form

Expression Language (EL) API: ELResolver, FunctionMapper, VariableMapper

Expression Language (EL) API in JSP: ELResolver, FunctionMapper, VariableMapper

जब हम JSP (Java Server Pages) में Expression Language यानी EL का इस्तेमाल करते हैं, तो हमें dynamic data को बहुत आसानी से access करने की सुविधा मिलती है। EL का पूरा structure और working इसके तीन main components पर depend करता है — ELResolver, FunctionMapper, और VariableMapper। ये तीनों मिलकर EL को powerful बनाते हैं ताकि developer को scripting या Java code लिखने की जरूरत ना पड़े।

Expression Language (EL) API Introduction

Expression Language (EL) API JSP framework का वो हिस्सा है जो page में मौजूद data objects को dynamically access करने की power देता है। इसका main goal है JSP pages को simple और readable बनाना ताकि HTML और dynamic content के बीच balance बना रहे।

EL का use करके हम JSP page में directly Java objects या variables को बिना scriptlet के access कर सकते हैं। EL syntax बहुत simple है — बस ${expression} का use करना होता है।

Example of Basic EL Usage

${user.name}
${requestScope.product.price}

ऊपर दिए गए example में EL अपने आप request, session या application scope में data को खोजता है और उसका value दिखाता है।

1. ELResolver क्या है?

ELResolver Expression Language का सबसे core component है। इसका काम होता है किसी expression को evaluate करके उसका actual value निकालना। जब हम JSP page में ${user.name} जैसे expression लिखते हैं, तो ELResolver ही decide करता है कि “user” variable कहाँ define है और उसका value क्या है।

ELResolver की Working Process

जब JSP container को कोई EL expression मिलता है, तो वो निम्न steps follow करता है:

  • पहले ELResolver को invoke किया जाता है।
  • ELResolver check करता है कि expression में कौन सा variable या property है।
  • वो सभी scopes (page, request, session, application) में search करता है।
  • मिल जाने पर उसका actual value return करता है।

Types of ELResolvers

JSP specification में कई built-in ELResolvers दिए गए हैं, जो अलग-अलग type के data को handle करते हैं। जैसे:

ELResolver Type Description
BeanELResolver Java Beans की properties को access करता है।
MapELResolver Map type objects (जैसे HashMap) को handle करता है।
ListELResolver List या array type data को resolve करता है।
ResourceBundleELResolver ResourceBundle files से localized messages पढ़ता है।
ScopedAttributeELResolver Page, Request, Session और Application scope में variables खोजता है।

Custom ELResolver

हम अपनी जरूरत के अनुसार custom ELResolver भी बना सकते हैं। मान लीजिए हमें किसी special data source से value लानी है, तो हम खुद का ELResolver class बना सकते हैं।

public class MyCustomResolver extends ELResolver { public Object getValue(ELContext context, Object base, Object property) { if ("today".equals(property)) { context.setPropertyResolved(true); return new java.util.Date(); } return null; } }

ऊपर के example में हमने एक custom resolver बनाया है जो ${today} लिखने पर current date return करेगा।

2. FunctionMapper क्या है?

FunctionMapper EL API का वो हिस्सा है जो EL expressions में custom functions को handle करता है। EL में functions का use करके हम static methods या custom logic को call कर सकते हैं।

FunctionMapper का काम

  • Function का name और उसकी corresponding Java method को map करता है।
  • Expression में जब कोई function call होता है, तो वही method execute होती है।

Syntax for EL Functions

${fn:length(list)}
${math:sqrt(25)}

यहाँ fn:length() और math:sqrt() function calls हैं, जो FunctionMapper के जरिए resolve होते हैं।

Custom Function Mapping Example

अगर हमें अपने JSP page में custom function बनाना है, तो सबसे पहले हमें एक static method define करनी होगी और फिर उसे TLD (Tag Library Descriptor) file में declare करना होगा।

// Java Class public class MyFunctions { public static String greet(String name) { return "Hello, " + name + "!"; } } <function> <name>greet</name> <function-class>com.example.MyFunctions</function-class> <function-signature>java.lang.String greet(java.lang.String)</function-signature> </function>

अब हम JSP page में use कर सकते हैं:

${my:greet('Student')}

Result: Hello, Student!

FunctionMapper का Practical Use

FunctionMapper का use तब जरूरी होता है जब हमें business logic को JSP में directly execute नहीं करना और उसे utility functions के माध्यम से handle करना होता है। ये approach JSP को clean और maintainable बनाती है।

3. VariableMapper क्या है?

VariableMapper EL API का वो component है जो variables को manage करता है। इसका काम होता है variables को EL expressions में link करना और उनके actual values को map करके रखना।

VariableMapper का Role

  • VariableMapper expression context में variables को store करता है।
  • जब EL expression evaluate होती है, तो VariableMapper उस variable का value provide करता है।
  • अगर कोई variable change होता है, तो mapper उसे update कर देता है।

Example of VariableMapper

ELContext context = pageContext.getELContext(); VariableMapper varMapper = context.getVariableMapper(); ValueExpression expr = expressionFactory.createValueExpression("Hello EL", String.class); varMapper.setVariable("msg", expr);

अब हम JSP page में लिख सकते हैं:

${msg}

Result: Hello EL

VariableMapper के फायदे

  • Dynamic variables को manage करना आसान बनाता है।
  • Reusability बढ़ाता है क्योंकि variables को बार-बार define करने की जरूरत नहीं होती।
  • Expression evaluation को efficient बनाता है।

4. ELContext और इन Components का Relationship

EL API में ये तीनों components — ELResolver, FunctionMapper और VariableMapper — ELContext के अंदर work करते हैं। यानी जब कोई expression evaluate होती है, तो JSP engine ELContext object बनाता है, जिसमें ये तीनों components जुड़े होते हैं।

Component Responsibility
ELResolver Expression के value को resolve करता है।
FunctionMapper Functions को map और execute करता है।
VariableMapper Variables को manage और store करता है।

ELContext का Use Example

ELContext elContext = pageContext.getELContext(); Object result = expressionFactory.createValueExpression(elContext, "${user.name}", String.class).getValue(elContext);

यह code ELContext के माध्यम से expression को evaluate करता है और ELResolver के जरिए value प्राप्त करता है।

5. Real-world Use in JSP

Expression Language के ये तीन components JSP pages को dynamic और powerful बनाते हैं। कुछ real-world use cases:

  • ELResolver – जब हमें different scopes से user data access करना हो।
  • FunctionMapper – जब हमें re-usable logic जैसे formatting या calculation functions लगानी हों।
  • VariableMapper – जब हमें temporary variables define करने हों जो expression evaluation के दौरान use हों।

Example in JSP Page

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <c:set var="userName" value="Ravi" /> Hello, ${fn:toUpperCase(userName)}!

यहाँ fn:toUpperCase() function FunctionMapper के द्वारा map हुआ है, और variable userName VariableMapper द्वारा handle किया गया है।

6. Expression Language (EL) API के फायदे

  • JSP pages को readable और maintainable बनाता है।
  • Scriptlets की dependency को खत्म करता है।
  • Data binding आसान बनाता है।
  • Custom ELResolvers और functions के जरिए flexibility बढ़ाता है।
  • Dynamic variable management provide करता है।

7. Summary (Exam Notes)

Component Definition Purpose
ELResolver Expression values को resolve करता है। Variables और properties के value निकालता है।
FunctionMapper EL functions को map करता है। Static methods और logic को call करने देता है।
VariableMapper Variables को manage करता है। Temporary variables को store और update करता है।

इस तरह Expression Language (EL) API JSP के अंदर dynamic expressions को handle करने के लिए एक structured और efficient system provide करती है। ELResolver, FunctionMapper और VariableMapper तीनों मिलकर JSP को smart, flexible और developer-friendly बनाते हैं।