Skip to content

SLICE-128 making SliceLookUpTag type attribute value String #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<slice:lookup var="model" type="<%=com.example.components.text.TextModel%>" />
<slice:lookup var="model" type="<%= com.example.components.text.TextModel.class %>" />
<p>${model.text}</p>
```
or it can be
```jsp
<slice:lookup var="model" type="com.example.components.text.TextModel" />
<p>${model.text}</p>
```

Expand Down Expand Up @@ -220,7 +225,7 @@ Add dependencies to your POM file:
<dependency>
<groupId>com.cognifide.slice</groupId>
<artifactId>slice-core-api</artifactId>
<version>4.3.0</version>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.cognifide.slice</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,45 @@ public static <T> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down