sending sling model values to servlet in aem 6.5 | Community
Skip to main content
December 1, 2021
Solved

sending sling model values to servlet in aem 6.5

  • December 1, 2021
  • 5 replies
  • 4783 views

Can you please share the suggestion for the below scenario:

 

Have created sling model to read multifield values, need to send these values to a servlet kindly suggest 

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 Kiran_Vedantam

Hi All,

 

There are certain use-cases where we would like to access the values stored in particular components in servlet. This might or might not be the use case for @kattakiran1990 

 

@kattakiran1990 to achieve this, use the below code

 

Resource resource = resourceResolver.getResource("/content/we-retail/language-masters/en/user/jcr:content/root/responsivegrid/user");
UserModel model = resource.adaptTo(UserModel.class);

 

Hope this helps!

 

Thanks,

Kiran Vedantam.

5 replies

manikanthar1295
Level 5
December 1, 2021

Hi @kattakiran1990 

 

In Sling servlet you can use below code and try

 

Model model =resourceResolver.adaptTo(model.class)

 

then u use   the getter method as you need.

 

Regards

Manikantha R

Siva_Sogalapalli
Community Advisor
Community Advisor
December 1, 2021

@kattakiran1990 What is the reason you want to pass values from sling model to servlet ? 

 

If you really want to use servlet, as @manikanthar1295 suggested,  you can try below using request / resource resolver. 

 

SlingModel model = request.adaptTo(SlingModel.class);

Or 

SlingModel model = resourceResolver.adaptTo(SlingModel.class);

December 1, 2021

I am using sling model to get a multifield values. Now i need to send those as parameters to my servlet.

 

kindly suggest

 

Siva_Sogalapalli
Community Advisor
Community Advisor
December 1, 2021

Still it's not clear to me the reason why you want to use servlet,  but you can simply try with osgi service and achieve similar: 

 

1. Create Interface: 

public interface ISampleService {
testMethod(Strig args);
}

2. Create service: 

@Component(service = ISampleService.class,immediate=true)

public class SampleService {
           testMethod(Strig args){

                  //write logic

          } 
}

 

3. Call OSGI service from sling model:

 

public class SlingModel{

@OSGiService
private ISammpleService sampleService;

   

@PostConstruct
protected void init() {

sampleService.testMethod(pass required arguments here);

}

Hope this helps you.

Anmol_Bhardwaj
Community Advisor
Community Advisor
December 1, 2021

I would do something like this:

HTL:

 <div data-sly-list.pathlist="${resource.getChildren}">
		<div data-sly-test="${pathlist.name == 'multiField'}">   
			<div data-sly-list.multiplePathList= "${pathlist.getChildren}">
				<input type="hidden" id="multiplePathList" value="${multiplePathList.propName}"></input>
			</div>		
		</div>
	</div>

JS:

 

 

let multiFieldValue = document.QuerySelector("#multiplePathList").value;
var url = "servletPath?" + "multiplePathList=" + multiplePathList;

Java:

String[] multiplePathList = StringUtils.isNotBlank(request.getParameter("multiplePathList")) ? request.getParameter("multiplePathList").split(",") : null;

 

arunpatidar
Community Advisor
Community Advisor
December 2, 2021

Hi,

you need to get all param in javascript and then you can sepaarte them and send to servlet, I would suggest use post request to send all as a single param with multivalues

 

const pathItems = userList.querySelectorAll("#multiplePathlist");
let values = ""; pathItems.forEach(function(userItem) {
values = values + item.value + ",";
});

 

Arun Patidar
VeenaVikraman
Community Advisor
Community Advisor
December 1, 2021

@kattakiran1990 ,

   Could you please help me understand what is the usecase you are trying to achieve ? I am not understanding the requirement to send the values from a Sling Model to Servlet ? 

Kiran_Vedantam
Community Advisor
Kiran_VedantamCommunity AdvisorAccepted solution
Community Advisor
December 2, 2021

Hi All,

 

There are certain use-cases where we would like to access the values stored in particular components in servlet. This might or might not be the use case for @kattakiran1990 

 

@kattakiran1990 to achieve this, use the below code

 

Resource resource = resourceResolver.getResource("/content/we-retail/language-masters/en/user/jcr:content/root/responsivegrid/user");
UserModel model = resource.adaptTo(UserModel.class);

 

Hope this helps!

 

Thanks,

Kiran Vedantam.