现在让我们看一个Sling Models的例子
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="nt:unstructured"
jcr:title="User Component"
sling:resourceType="cq/gui/components/authoring/dialog">
<content
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<layout
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/tabs"
type="nav"/>
<items jcr:primaryType="nt:unstructured">
<properties
jcr:primaryType="nt:unstructured"
jcr:title="User Properties"
sling:resourceType="granite/ui/components/foundation/container">
<layout
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns"/>
<items jcr:primaryType="nt:unstructured">
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/container">
<items jcr:primaryType="nt:unstructured">
<firstName
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
class="field-whitespace"
fieldDescription="Please enter the first name"
fieldLabel="First Name"
name="./firstName"/>
<lastName
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/textfield"
class="field-whitespace"
fieldDescription="Please enter the last name"
fieldLabel="Last Name"
name="./lastName"/>
<gender
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
fieldDescription="Select your grnder"
fieldLabel="gender"
name="./gender">
<items jcr:primaryType="nt:unstructured">
<male
jcr:primaryType="nt:unstructured"
text="Male"
value="Male"/>
<female
jcr:primaryType="nt:unstructured"
text="Female"
value="Female"/>
<other
jcr:primaryType="nt:unstructured"
text="Other"
value="Other"/>
items>
gender>
<country
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
fieldDescription="Select your country"
fieldLabel="Country"
name="./country">
<items jcr:primaryType="nt:unstructured">
<option1
jcr:primaryType="nt:unstructured"
text="India"
value="India"/>
<option2
jcr:primaryType="nt:unstructured"
text="Canada"
value="Canada"/>
<option3
jcr:primaryType="nt:unstructured"
text="Israel"
value="Israel"/>
<option4
jcr:primaryType="nt:unstructured"
text="Other"
value="Other"/>
items>
country>
items>
columns>
items>
properties>
items>
content>
jcr:root>
package com.adobe.aem.guides.wknd.core.models;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import javax.inject.Inject;
/**
* @author kevin.
* @Title: rhtj.
* @Package com.adobe.aem.guides.wknd.core.models.
* @Description: sing model.
* @date 2020/04/3 16:22 下午.
*/
@Model(adaptables = Resource.class)
public class UserModel {
@Inject
private String firstName;
@Inject
private String lastName;
@Inject
private String gender;
@Inject
private String country;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
}
让我们了解该类的代码。我们使用@Model注释指定此模型类为Sling模型。每个数据成员都用@Inject注释。此类将映射到JCR中的资源。
要在JCR中访问此资源,让我们创建一个Sling Servlet
package com.adobe.aem.guides.wknd.core.servlets;
import com.adobe.aem.guides.wknd.core.models.UserModel;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
/**
* @author kevin.
* @Title: rhtj.
* @Package com.adobe.aem.guides.wknd.core.servlets.
* @Description: 这个servlet使用HTTP GET方法从RESTful web服务读取数据..
* @date 2020/04/07 9:22 上午.
*/
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Sling Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/slingmodel/user"})
public class SlingModelServlet extends SlingSafeMethodsServlet {
/**
* uuid.
*/
private static final long serialVersionUID = 7558680464517017317L;
/**
* Logger.
*/
private static final Logger log = LoggerFactory.getLogger(SlingModelServlet.class);
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
try (ResourceResolver resourceResolver = request.getResourceResolver()) {
log.info("----------< Processing starts >----------");
//获取资源.
Resource resource = resourceResolver
.getResource("/content/forms/af/wknd/test-dome/jcr:content/guideContainer/rootPanel/items/user");
//将资源调整为UserModel类.
UserModel model = resource.adaptTo(UserModel.class);
//
response.getWriter()
.print("存储在资源中的数据是:\nFirst Name: " + model.getFirstName() + "\nLast Name: "
+ model.getLastName() + "\nGender: " + model.getGender() + "\nCountry: "
+ model.getCountry());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
获取资源(根据资源路径获取资源).
Resource resource = resourceResolver
.getResource("/content/forms/af/wknd/test-dome/jcr:content/guideContainer/rootPanel/items/user");
将资源调整为UserModel类.
UserModel model = resource.adaptTo(UserModel.class);