Sling model

Internal object:

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(
		adaptables = {Resource.class})
public class AbstractModelResource {
	 @SlingObject
	 ResourceResolver resolver;

	 @SlingObject
	 Resource resource;
}

real fields:

@Model(adaptables=Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class JobDetailModel extends AbstractModel{
	
	@OSGiService
	JobTriggerService jobTriggerService;
	
	private JobEntity job;
	private String appUrl;
	private String postingDate;
	private String postingDateFormat;
	
	@Inject @Optional @Named("subTitle") @Default(values="")
	private String subTitle;
	
	@Inject @Optional @Named("qualifications") @Default(values="")
	private String qualifications;
	
	@Inject @Optional @Named("jobID") @Default(values="")
	private String jobID;
	
	@Inject @Optional @Named("jobFunction") @Default(values="")
	private String jobFunction;
	
	@Inject @Optional @Named("postingDateLabel") @Default(values="")
	private String postingDateLabel;
	
	@Inject @Optional @Named("applyBtn") @Default(values="")
	private String applyBtn;
	
	@Inject @Optional @Named("scheduleLabel") @Default(values="")
	private String scheduleLabel;
	
	@Inject @Optional @Named("jobDetailsText") @Default(values="")
	private String jobDetailsText;
	
	@Inject @Optional @Named("relJobsText") @Default(values="")
	private String relJobsText;
	
	@Inject @Optional @Named("Job")
	private JobModel jobModel;
	
	@PostConstruct
	protected void init() {
}
}

Job Model:


import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Model(adaptables = Resource.class)
public class JobModel {
	
	@Inject @Optional@Named("BrandName") @Default(values="")
	private String BrandName;
	
	@Inject @Optional @Named("PrimaryLocation")
	PrimaryLocation primaryLocation;
	
	@Inject @Optional @Named("Postings")
	List postings;
	
	@Inject @Optional @Named("JobField")
	JobField JobField;
	
	@Model(adaptables = Resource.class)
	public static class PrimaryLocation{
		@Inject @Optional @Named("Country") @Default(values="")
		String Country;

		@Inject @Optional @Named("State") @Default(values="")
		String State;
		
		}
}

for request:

@Model(
        adaptables = {Resource.class, SlingHttpServletRequest.class},
        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public abstract class AbstractModel extends AbstractModelResource{
    
     @SlingObject
     ResourceResolver resolver;

     @Inject
     @Via("resource")
     Resource resource;

     @Inject
     @Via("request")
     SlingHttpServletRequest request;

     @OSGiService

}

转载于:https://my.oschina.net/slightScenery/blog/3076202

你可能感兴趣的:(Sling model)