Passing Attributes to Sling Model via React? | Community
Skip to main content
Level 2
October 12, 2021
Solved

Passing Attributes to Sling Model via React?

  • October 12, 2021
  • 1 reply
  • 962 views

We have a React component that returns data from the DAM based on the category Tag assigned to it. Is there a way to pass an attribute from React to the sling model and update the model.json with category data to display in React? Or will we have to keep using servlets to achieve this? 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @gregy68980908, did you consider to pass the value using GET parameter. Sling model has access to request, so you should be able to retrieve GET param value and update data exposed by model respectively, e.g assuming this is url you are using now

/content/we-retail/language-masters/en.model.json

it can be changed to something like that (this is request you will sent from React)

/content/we-retail/language-masters/en.model.json?attribute=value

In your sling model you can access value like this:

@Self
private SlingHttpServletRequest request;

private String attributeValue;

@PostConstruct
protected void init() {
    this.attributeValue = request.getParameter("attribute");
} 

 

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
October 12, 2021

Hi @gregy68980908, did you consider to pass the value using GET parameter. Sling model has access to request, so you should be able to retrieve GET param value and update data exposed by model respectively, e.g assuming this is url you are using now

/content/we-retail/language-masters/en.model.json

it can be changed to something like that (this is request you will sent from React)

/content/we-retail/language-masters/en.model.json?attribute=value

In your sling model you can access value like this:

@Self
private SlingHttpServletRequest request;

private String attributeValue;

@PostConstruct
protected void init() {
    this.attributeValue = request.getParameter("attribute");
}