diff --git a/README.md b/README.md index eb16a9b8..e30049cc 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,12 @@ No more business logic in your view (JSP, Sightly scripts) - business logic's pl **JSPs made clean and tidy** - no more these ugly scriptlets. ```jsp - + +

${model.text}

+``` +or it can be +```jsp +

${model.text}

``` @@ -220,7 +225,7 @@ Add dependencies to your POM file: com.cognifide.slice slice-core-api - 4.3.0 + 5.0.0 com.cognifide.slice diff --git a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceLookupTag.java b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceLookupTag.java index 2bbc20b3..c2af3284 100644 --- a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceLookupTag.java +++ b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceLookupTag.java @@ -33,31 +33,40 @@ public class SliceLookupTag extends SimpleTagSupport { private String appName; // auto-detected when null + private String name; + private Class type; private void clean() { type = null; var = null; appName = null; + name = null; } @Override public void doTag() throws JspException { try { - if (StringUtils.isBlank(var) || (type == null)) { + if (StringUtils.isBlank(var) || (type == null && StringUtils.isEmpty(name))) { throw new JspTagException("Var and Type must be set " + appName); } final PageContext pageContext = (PageContext) getJspContext(); - final Object model = SliceTagUtils.getFromCurrentPath(pageContext, type, appName); + final Object model = StringUtils.isEmpty(name) ? SliceTagUtils.getFromCurrentPath(pageContext, type, appName) : SliceTagUtils.getFromCurrentPath(pageContext, name, appName); pageContext.setAttribute(var, model, PageContext.PAGE_SCOPE); + } catch (ClassNotFoundException cause) { + throw new JspTagException("Could not get class for " + name + cause.getMessage()); } finally { clean(); } } - public void setType(Class type) { - this.type = type; + public void setType(Object type) { + if(type instanceof Class){ + this.type = (Class)type; + }else if(type instanceof String){ + this.name = (String)type; + } } public void setVar(String var) { diff --git a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceTagUtils.java b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceTagUtils.java index 4bcc0c6a..99b0a1b3 100644 --- a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceTagUtils.java +++ b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/SliceTagUtils.java @@ -73,6 +73,45 @@ public static T getFromCurrentPath(final PageContext pageContext, final Clas return getFromCurrentPath(request, injectorsRepository, requestContextProvider, type, appName); } + /** + * A helper method that returns the model of {@code type} + * + * @param pageContext allows to access request context + * @param type canonical name of the class, whose model object needs to be returned + * @return Model object pertaining to {@code type} as it's canonical name + * @throws ClassNotFoundException if the class was not found + */ + public static Object getFromCurrentPath(final PageContext pageContext, final String type, + final String appName) throws ClassNotFoundException { + final SlingHttpServletRequest request = slingRequestFrom(pageContext); + final InjectorWithContext injector = getInjectorWithContext(pageContext, request, appName); + + injector.pushContextProvider(contextProviderFrom(pageContext)); + + final ModelProvider modelProvider = injector.getInstance(ModelProvider.class); + + try { + return modelProvider.get(type, request.getResource()); + } finally { + injector.popContextProvider(); + } + } + + private static InjectorWithContext getInjectorWithContext(final PageContext pageContext, + final SlingHttpServletRequest request, final String appName){ + final InjectorsRepository injectorsRepository = injectorsRepositoryFrom(pageContext); + + final String injectorName = getInjectorName(request, appName, injectorsRepository); + + InjectorWithContext injector = injectorsRepository.getInjector(injectorName); + + if (injector == null) { + throw new IllegalStateException("Guice injector not found for app: " + appName); + } else { + return injector; + } + } + /** * A helper method that returns a model of the Sling resource related to given request * diff --git a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/package-info.java b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/package-info.java index b4155f5d..5513196b 100644 --- a/slice-core-api/src/main/java/com/cognifide/slice/api/tag/package-info.java +++ b/slice-core-api/src/main/java/com/cognifide/slice/api/tag/package-info.java @@ -17,7 +17,7 @@ * limitations under the License. * #L% */ -@Version("4.3.0") +@Version("5.0.0") package com.cognifide.slice.api.tag; import aQute.bnd.annotation.Version;