I have one component(Name, Price, Button Label) which has button . Whenever button is clicked it should redirect to new page and fetch the same data and print there.
I am trying to use href with parameters p and name using which I coul get the data and show in new component present there. I have written below code which is giving error org.apache.sling.scripting.sightly.render.ObjectModel Cannot access method name on object com.sample.core.models.DescriptionModel@76c56976
Tried restarting aem, refreshing bundle giving build etc nothing worked
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DescriptionModel {
private final Logger logger = LoggerFactory.getLogger(getClass());
@SlingObject
SlingHttpServletRequest request;
@OSGiService
private transient ResourceResolverFactory resolverFactory;
public ValueMap getResource() throws LoginException {
ResourceResolver resolver=request. getResourceResolver();
Resource resource = resolver.getResource(request.getParameter("p"));
logger.info("RESOURCE IS {}",resource.getChild("jcr:content/"+request.getParameter("name")).getPath());
return resource.getChild("jcr:content/"+request.getParameter("name")).getValueMap();
}
public String getName() throws LoginException {
ValueMap properties=getResource();
return (String) properties.get("name");
}
public int getPrice() throws LoginException {
ValueMap properties=getResource();
return (int) properties.get("price");
}
public String getBtnLabel() throws LoginException {
ValueMap properties=getResource();
return (String) properties.get("btnLabel");
}
Solved! Go to Solution.
Views
Replies
Total Likes
Please create @PostConstruct and move logic of getResource() to @PostConstruct method. Just have a private variable which can be initialized in Post Construct method and passed to getName().
Basically if any exception comes in getter you get this issue. You can go to bottom of stack trace to get exact exception.
Refer below dummy code which will fix your error :
public String getName() throws LoginException { String dummyString = "dummy"; try { ValueMap properties = getResource(); dummyString = (String) properties.get("name"); }catch(Exception e) { //TODO } return dummyString; }
Please check if there is any error when bundle is installed and check the timestamp of the Last Modification Time of your bundle in Felix console. Probably the bundle with latest changes is not getting installed.
Also you can follow the best practice by creating an interface and implementation class for Model. Moving getResource() in @PostConstruct will execute getting ValueMap only once.
Bundle is updating properly and I will follow best practice for debugging only I did that still not working
Please create @PostConstruct and move logic of getResource() to @PostConstruct method. Just have a private variable which can be initialized in Post Construct method and passed to getName().
Basically if any exception comes in getter you get this issue. You can go to bottom of stack trace to get exact exception.
Refer below dummy code which will fix your error :
public String getName() throws LoginException { String dummyString = "dummy"; try { ValueMap properties = getResource(); dummyString = (String) properties.get("name"); }catch(Exception e) { //TODO } return dummyString; }
Hi,
Don't do it this at client side because if the page is cached then you will have the same value for all the users.
better to fetch value using a servlet which can be called upon the click or next page load.