taglib - How to access component properties from a Java Class -


i working on component requires properties (that user sets during run time) work intended.

initially, using properties.get('foo') fetch needed property component, i'm trying remove traces of script-let code component jsp file.

how fetch property 'foo' (which set during runtime on component) within java code? remember reading somewhere using valuemap best way, tried using this:-

public static map<string, object> getresourceproperties(string path,             slinghttpservletrequest request) {         resourceresolver resourceresolver = request.getresourceresolver();         map<string, object> props= new hashmap<string, object>();         resource resource = resourceresolver.getresource(path);         if (null != resource) {             props.putall(resource.adaptto(valuemap.class));         }         return props;     }  

and in jsp:- <c:set var="refproperties" value="${xyz:getresourceproperties(properties.path,slingrequest)}" />

but doesn't return value want.

this simplest method accomplish include /libs/foundation/global.jsp , use properties object in scope ${properties.foo}.

include global.jsp @ top of component jsp so:

<%@include file="/libs/foundation/global.jsp"%> 

as comments in file indicate, registers sling (sling), cq (cq), , jstl (c,fmt,fn) taglib namespaces use in jsp.

then, of cq:defineobjects taglib, brings many helpful objects scope.

<cq:defineobjects /> 

here list:

@param slingrequest slinghttpservletrequest @param slingresponse slinghttpservletresponse @param resource current resource @param currentnode current node @param log default logger @param sling sling script helper  @param componentcontext component context of request @param editcontext edit context of request @param properties properties of addressed resource (aka "localstruct") @param pagemanager page manager @param currentpage containing page addressed request (aka "actpage") @param resourcepage containing page of addressed resource (aka "mypage") @param pageproperties properties of containing page @param component current cq5 component @param designer designer @param currentdesign design of addressed resource  (aka "actdesign") @param resourcedesign design of addressed resource (aka "mydesign") @param currentstyle style of addressed resource (aka "actstyle") 

this means using cq:defineobjects taglib have access properties valuemap through jsp expression language (el). no additional conversion required access properties within jsp.

<c:out value="${properties.foo}" /> 

to access properties within own java taglib or bean pass appropriate object code using standard jstl tags. pass whole request, current resource, or properties. passing whole request gives java code access current resource , objects created cq:defineobjects taglib including properties valuemap.

in jsp:

<jsp:usebean id="mybean" scope="request" class="com.my.impl.thebean">    <jsp:setproperty name="mybean" property="slingrequest" value="${slingrequest}"/>    <jsp:setproperty name="mybean" property="resource" value="${resource}"/>    <jsp:setproperty name="mybean" property="properties" value="${properties}"/> </jsp:usebean> 

in bean:

public void setslingrequest(final slinghttpservletrequest slingrequest) {     this.slingrequest = slingrequest;     // use 1 created cq:defineobjects     this.properties = (valuemap)this.slingrequest.getattribute("properties");     // or adapt resource     this.properties = this.slingrequest.getresource().adaptto(valuemap.class); }  public void setresource(final resource resource) {     this.resource = resource;     this.properties = this.resource.adaptto(valuemap.class); }  public void setproperties(final valuemap properties) {     this.properties = properties; } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -