Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

sending sling model values to servlet in aem 6.5

kattakiran1990
Level 1
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
Kiran_Vedantam
Correct answer by
Community Advisor
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
manikanthar1295
Level 5
Level 5

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

SivaPrasadReddyS
Level 8
Level 8

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

kattakiran1990
Level 1
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

 

SivaPrasadReddyS
Level 8
Level 8

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.

AnmolBhardwaj1808
Level 3
Level 3

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;

 

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

 

Veena_Vikram
Community Advisor
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 ? 

Kiran_Vedantam
Correct answer by
Community Advisor
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.