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.

What's the best way to use custom classes in JSP files

Avatar

Level 2

Hey there

I'm kinda new to the OSGi development and to get some experience I'm trying to develop a smaller project for our company. Basically it's a form which allows the user to enter the business hours of their location, so I can submit them to a SAP web-service.

I already started developing something, but I'm unsure if I'm doing it in a proper way or if I should try another approach. What confuses me are the OSGi components and when to use a component. Is it considered good behaviour to directly import my custom classes into the JSP (via @page import) or should I put them into a component? The classes I use on my JSP actually don't provide much business logic, they're more sort of a data class.

I've created a JSP called openingtimes_global.jsp which includes my most common classes (like Dealer, Brand and Department) and the localization object. The file looks like this:

<%@include file="/libs/wcm/global.jsp" %> <%@page import="ch.company.webtech.cq.openingtimes.Dealer, ch.company.webtech.cq.openingtimes.Brand, ch.company.webtech.cq.openingtimes.Department, com.day.cq.wcm.api.WCMMode, org.apache.sling.api.resource.ResourceUtil, java.util.Locale, java.util.ResourceBundle, com.day.cq.i18n.I18n"%> <% Locale pageLocale = currentPage.getLanguage(true); ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLocale); I18n i18n = new I18n(resourceBundle); Dealer dealer = new Dealer(request.getHeader("USERID")); String brandName = ""; String dealerId = dealer.getDealerId(); String departmentName = ""; String id = ""; %>

My body.jsp then includes the global file, so I have access to the initialized dealer object.

<%@include file="/libs/wcm/global.jsp" %> <%@include file="/apps/openingtimes/components/openingtimes_global.jsp" %> <body> <h1><%= i18n.get("openingtimes") %></h1><% for(Brand brand : dealer.getBrands()) { brandName = brand.getBrandName(); %> <h1><%= i18n.get(brandName) %></h1> <div class="brand" > <ul><% for(Department department : brand.getDepartments()) { departmentName = department.getName(); id = brandName + "-" + departmentName; %><li><a href="#<%= id %>"><%= i18n.get(departmentName) %></a></li><% } %> </ul><% for(Department department : brand.getDepartments()) { departmentName = department.getName(); id = brandName + "-" + departmentName; %><div id="<%= id %>" class="department" > <form class="timeForm"> <cq:include script="/apps/openingtimes/components/contentpage/timetable.jsp" /> <input type="hidden" name="brand" value="<%= brandName %>" /> <input type="hidden" name="department" value="<%= departmentName %>" /> <input type="hidden" name="dealerId" value="<%= dealerId %>" /> <center><button type="submit" value="Speichern" class="save"><%= i18n.get("save")%></button></center> </form> </div><% }%> </div><% }%> <h1><%= i18n.get(brandName) &></h1> <div class="brand"> <% %> </div> </body>

I think that it would be possible to create a service for the dealer object (since it has some initialization code, which queries some data from a LDAP server), but what about my data classes like Department and Brand. Should I create a service for those too? I think it's a bit overkill because I'd have to request those services from sling, just because my dealer object returns those object types.

Overall, I'm confident that I'd be able to create a working application, but I'm more worried that it would be considered "crappy code". Is my way of doing things ok or should I try another approach?

Sorry if my question seems a bit weird, I'm just a bit unsure since it's my first real CQ application :)

Thank you
Ahatius

0 Replies