Strecks simplifies action development by supporting dependency injection.
Below is a simple vanilla Struts action, which is being used to retrieve data from the service tier for display
via a web interface. The action extends Spring's ActionSupport
to simplify
Spring integration.
/**
* Plain Struts action without dependency injection
*/
public class RetrieveBookingAction extends ActionSupport
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
HolidayBookingService holidayBookingService =
(HolidayBookingService) getWebApplicationContext().getBean(
"holidayBookingService");
long id = Long.parseLong(request.getParameter("holidayBookingId"));
HolidayBooking holidayBookings = holidayBookingService.getHolidayBooking(id);
request.setAttribute("holidayBooking", holidayBookings);
return mapping.findForward("success");
}
}
The Strecks equivalent of this action is shown below:
/**
* Strecks action with dependency injection
*/
@Controller(name = BasicController.class)
public class RetrieveBookingAction implements BasicAction
{
private HolidayBookingService holidayBookingService;
private WebHelper webHelper;
private long holidayBookingId;
public String execute()
{
HolidayBooking holidayBookings =
holidayBookingService.getHolidayBooking(holidayBookingId);
webHelper.setRequestAttribute("holidayBooking", holidayBookings);
return "success";
}
@InjectSpringBean(name = "holidayBookingService")
public void setService(HolidayBookingService service)
{
this.holidayBookingService = service;
}
@InjectWebHelper
public void setWebHelper(WebHelper webHelper)
{
this.webHelper = webHelper;
}
@InjectRequestParameter(required = true)
public void setHolidayBookingId(long holidayBookingId)
{
this.holidayBookingId = holidayBookingId;
}
}
It appears to be a few lines longer, but bear in mind that the only real working code in execute()
. The rest are simply setters
for dependencies. The example can be simplified even further, if necessary. Action state can be accessed directly from JSPs, which means
that the above example can be reduced to the following:
@Controller(name = BasicController.class) public class RetrieveBookingAction implements BasicAction { private HolidayBookingService holidayBookingService; private long holidayBookingId; private HolidayBooking holidayBooking; public String execute() { holidayBooking = holidayBookingService .getHolidayBooking(holidayBookingId); return "success"; } @InjectSpringBean(name = "holidayBookingService") public void setService(HolidayBookingService service) { this.holidayBookingService = service; } @InjectRequestParameter(required = true) public void setHolidayBookingId(long holidayBookingId) { this.holidayBookingId = holidayBookingId; } public HolidayBooking getHolidayBooking() { return holidayBooking; } }
Notice how there is no need for programmatically retrieving the dependencies, not even request parameters.