Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Struts to Stripes—A Road Worth Traveling : Page 2

Porting your existing Struts application to the Stripes Framework can simplify Web development, and the conversion process is easier than you might think.


advertisement

The Porting Process

I started my port from the view layer and worked my way back to actions. I had no real logic to this approach; I had to start somewhere and views were as good a starting point as any.

JavaServer Pages
Stripes, like Struts, uses JSPs for its view layer. I was pleasantly surprised to find that the Stripes tag library is similar to Struts' HTML taglib. In fact, I was able to upgrade many of my tags using the universal replace.

Stripes relies on JSTL for logic in the JSP view. I was using a mix of Struts logic tags and JSTL in my application, so I was ahead of the game. By porting all of my logic tags to JSTL, I was also able to take advantage of JSTL's superior handling of if/else and case statements, which are either rudimentary or non-existent in the Struts logic taglib.



Internationalization
The next surprise came when I was porting my Struts' message resources. On the configuration side, all this operation required was renaming my Struts message resource files. Inside my JSPs, I was able to replace 100 percent of my Struts message tags (e.g., <bean:message key="buttons.save"/>) with JSTL format tags (e.g., <fmt:message key="buttons.save"/>) using universal replace. The JSTL format tag also supports message resource bundling available in Struts.

Forms
The most rewarding part of my port was getting rid of my Struts Action Forms, which can require extensive XML markup and tedious conversions in the Action class as the following sample shows:

<form-bean name="employeeUpdateForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="employeeid" type="java.lang.Long" /> <form-property name="firstname" type="java.lang.String" /> <form-property name="lastname" type="java.lang.String" /> <form-property name="phone" type="java.lang.String" /> <form-property name="email" type="java.lang.String" /> <form-property name="phone" type="java.lang.String" /> <form-property name="socialsecurity" type="java.lang.String" /> <form-property name="birthdate" type="java.lang.String" /> <form-property name="salary " type="java.lang.String" /> </form-bean> public ActionForward executeAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Employee employee=new Employee(); DynaValidatorForm eaf = (DynaValidatorForm) form; employee.setFirstname(eaf.getString("firstname")); employee.setLastname(eaf.getString("lastname")); employee.setPhone (eaf.getString("phone")); employee.setEmail (eaf.getString("email")); employee.setEmployeeid ((Integer)eaf.get("employeeid")); employee.setSocialsecurity(Long.parseLong(eaf.getString("socialsecurity"))); employee.setBirthdate(MyUtils.convertStringToDate(eaf.getString("birthdate"))); employee.setSalary(MyUtils.convertStringToBigDecimal(eaf.getString("salary"))); EmployeeDAOService.updateEmployee(employee); return new ActionForward(mapping.getForward()); }

Stripes form handling, on the other hand, allows you to use your domain object as a form:

public class UpdateEmployeeActionBean implements ActionBean { private ActionBeanContext context; private Employee employee; public ActionBeanContext getContext() { return context; } public void setContext(ActionBeanContext context) { this.context = context; } public void setEmployee(Employee employee) { this.employee = employee; } public Employee getEmployee() { return this.employee; } @DefaultHandler public Resolution update() { EmployeeDAOService.updateEmployee(employee); return new ForwardResolution("/employees/updateEmployee.jsp"); } }

In most cases, I was able to embed a copy of my domain object as a property of my Stripes ActionBean class and include only the code involved in moving that object to and from my persistence layer. I threw away all of the form handling in Struts Actions, including initial configuration, casting the form to the appropriate class, and copying and converting that data to and from the domain object—about 30 percent of the code in most action classes I've seen. It was not needed in Stripes.

In short, embed the domain object as a property of your ActionBean class, provide it getter and setter methods, and voilà! The whole enchilada—including lists—is exposed to the form in the HTML view.

What I could do with forms I could also do with query-string parameters. I simply made those parameters a property of my ActionBean and they were automatically copied into those fields if they were part of the request.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap