Reputation: 2087
I'm assuming this is very easy to do, but I've been searching for a couple hours on Google and StackOverflow with no luck.
For starters, I have a jsp page using Dojo. I use Dojo's request module to make AJAX calls to Spring controllers for all sorts of things. The controllers call services, the services call DAOs, etc etc.
Originally, I had a properties file that contained several configuration settings for my site. These were injected into various Spring components using @Value. I wanted to make these dynamically configurable from my admin page, so I added a domain object called "SiteSettings" that holds the values from my properties file. When my webapp starts up, if it can't fine a SiteSettings object in persistence, it creates a new one, populates it with the settings from my properties file, and persists it. If one is already in persistence, it just uses it.
All of the site properties are used on the back-end except for one, a boolean called "authenticationRequired". I need to access this value in a javascript value on the front-end. This would be very easy to obtain via AJAX after the page loads, but I thought it's kind of silly to have to make an additional request, when I'm sure I should just be able to get the value from my SiteSettingsService (which uses the DAO to get the domain object).
Right now, I'm getting the value directly from the properties file:
<fmt:bundle basename="swtc">
<fmt:message key="swtc.authenticationRequired" var="authenticationRequired"/>
</fmt:bundle>
<script type="text/javascript">
window.authenticationRequired = <c:out value="${authenticationRequired}"/>;
</script>
How can I modify this get the value from my service/dao/domain object instead of directly from my properties file? Here are my controller and service files... nothing fancy:
Controller:
@RequestMapping(value = "/getSiteSettings", method = RequestMethod.GET)
@ResponseBody
public ModelMap getSiteSettings(ModelMap model) {
try {
SiteSettings siteSettings = siteSettingsService.getSiteSettings();
model.addAttribute("siteSettings", siteSettings);
model.addAttribute("success", true);
} catch (Exception ex) {
logger.error("There was an error getting the site settings data. ", ex);
model.addAttribute("exceptionMessage", ex.getLocalizedMessage());
model.addAttribute("success", false);
}
return model;
}
Service:
@Override
public SiteSettings getSiteSettings(){
List<SiteSettings> siteSettings = siteSettingsDao.findAll();
if (siteSettings != null && !siteSettings.isEmpty()){
return siteSettingsDao.findAll().get(0);
}
return null;
}
Upvotes: 0
Views: 1586
Reputation: 23004
In the controller method that is responsible for rendering the overall jsp that requires the property, you should just be able to put the authenticationRequired
attribute on the Model
so it becomes available in the page for output into the Javascript. You'd need to autowire an instance of the SiteSettingsService
into that controller:
@Controller
public class MainPageController { // or whatever it happens to be called
@Autowired
private SiteSettingsService siteSettingsService;
@RequestMapping(value="/mainPage", method=RequestMethod.GET)
public String displayMainPage(Model model) {
SiteSettings siteSettings = siteSettingsService.getSiteSettings();
// Make the property available to the view
model.addAttribute("authenticationRequired", siteSettings.isAuthenticationRequired());
return "mainPage";
}
}
Upvotes: 1