Posts

Showing posts with the label Struts2

How to Get Spring Managed Service Instance Outside the Action Class in Struts2

In Struts 2, we can configure Spring to inject service instanced using Dependency Injection into Action classes. But sometime we need these service class instances in other components who are are Spring aware. For example you need AccountsService instance in StudentService class. For such purpose I have createds util class which can return me reference to any service instance at run time, in any class. I call is SpringContext, here is its definition: package com.bitspedia.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContext implements ApplicationContextAware { private static ApplicationContext CONTEXT; public void setApplicationContext(ApplicationContext context) throws BeansException { CONTEXT = context; } public static Object getBean(String beanName) { return CONTEXT.getBean(beanName); } } Now getting the r...

In Struts 2 - Where to Put Frequently Used Options Lists

Image
Instead of populating your options lists in Action under populate() method. Put them in application scope, so that lists are not created on each call to an action. This is specially true if your option lists is being used on multiple forms. Such options lists include Countries, Products, Cities, etc. These rarely change in application life cycle. And if change is required, you can update the application scope object. If we do not use centralized approach, we have to do something like this in our JSP page. <s:select name="country" list="countries"/> And in Action class we must define a countries list, like this: private List<String> countries; public List<String> getCountries() { return countries; } public void setCountries() { countries = new ArrayList<String>(); countries.add("Germany"); countries.add("India"); countries.add("US"); countries.add("Russia"); // .... } In execute() or populate() m...