Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

sending sling model values to servlet in aem 6.5

Avatar

Level 1

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 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

8 Replies

Avatar

Level 6

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

Avatar

Community Advisor

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

Avatar

Level 1

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

 

kindly suggest

 

Avatar

Community Advisor

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.

Avatar

Community Advisor

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;

 

Avatar

Community Advisor

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

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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.