sending sling model values to servlet in aem 6.5 | Community
Skip to main content
New Participant
Dec 1, 2021 6:42 AM UTC
Solved

sending sling model values to servlet in aem 6.5

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.
Kiran_Vedantam
Kiran_VedantamAccepted solution
New Participant
Dec 2, 2021 1:03 PM UTC

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.

8 replies

Kiran_Vedantam
Kiran_VedantamAccepted solution
New Participant
Dec 2, 2021 1:03 PM UTC

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.

Translate
VeenaVikraman
New Participant
Dec 1, 2021 12:45 PM UTC

@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 ? 

Translate
Anmol_Bhardwaj
New Participant
Dec 1, 2021 11:25 AM UTC

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;

 

Translate
arunpatidar
New Participant
Dec 2, 2021 6:07 PM UTC

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 + ",";
});

 

Translate
Arun Patidar
Siva_Sogalapalli
New Participant
Dec 1, 2021 10:16 AM UTC

@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);

Translate
New Participant
Dec 1, 2021 10:36 AM UTC

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

 

kindly suggest

 

Translate
Siva_Sogalapalli
New Participant
Dec 1, 2021 10:59 AM UTC

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.

Translate
manikanthar1295
Level 5
Dec 1, 2021 6:59 AM UTC

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

Translate