HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Strutting your Stuff"
Vol. 6, Issue 11, p. 42

	


Listing 1: The additions to WEB.XML required to use Struts


 <servlet>
 <servlet-name>action</servlet-name>
 <servlet-class>
 org.apache.struts.action.ActionServlet
 </servlet-class>
 <init-param>
 <param-name>config</param-name>
 <param-value>
 /WEB-INF/struts-config.xml
 </param-value>
 </init-param>
 <init-param>
 <param-name>debug</param-name>
 <param-value>2</param-value>
 </init-param>
 <init-param>
 <param-name>mapping</param-name>
 <param-value>
 org.apache.struts.action.RequestActionMapping
 </param-value>
 </init-param>
 <load-on-startup>2</load-on-startup>
 </servlet>



 <servlet-mapping>
 <servlet-name>action</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 <taglib>
 <taglib-uri>
 /WEB-INF/struts-bean.tld
 </taglib-uri>
 <taglib-location>
 /WEB-INF/struts-bean.tld
 </taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>
 /WEB-INF/struts-html.tld
 </taglib-uri>
 <taglib-location>
 /WEB-INF/struts-html.tld
 </taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>
 /WEB-INF/struts-logic.tld
 </taglib-uri>
 <taglib-location>
 /WEB-INF/struts-logic.tld
 </taglib-location>
 </taglib>
 <taglib>
 <taglib-uri>
 /WEB-INF/struts-template.tld
 </taglib-uri>
 <taglib-location>
 /WEB-INF/struts-template.tld
 </taglib-location>
 </taglib>


Listing 2: A simple STRUTS-CONFIG.XML document

<struts-config>
 <form-beans>
 <form-bean
 name="addItem"
 type="schedule.ScheduleItem" />
 </form-beans>

 <action-mappings>
 <action
 path="/sched"
 type="schedule.ViewScheduleAction" />
 <action
 path="/schedEntry"
 type="schedule.ScheduleEntryAction" />
 <action
 path="/add"
 type="schedule.AddToScheduleAction"
 name="addItem"
 input="/ScheduleEntryView.jsp"
 validate="true" />
 </action-mappings>

</struts-config>

Listing 3: A small Action subclass

package schedule;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

public class ViewScheduleAction extends Action {

 public ViewScheduleAction() {

 }

 public ActionForward perform(
<x-tab></x-tab> ActionMapping mapping, ActionForm form,
<x-tab></x-tab> HttpServletRequest request,
<x-tab></x-tab> HttpServletResponse response)
 <x-tab></x-tab>throws 
  java.io.IOException,
 <x-tab></x-tab>javax.servlet.ServletException 
  {
 ScheduleBean 
  sb = new ScheduleBean();
 sb.populate();
 request.setAttribute("scheduleBean", 
  sb);
 return 
  new ActionForward("/ScheduleView.jsp");
 }
}


Listing 4: The View JSP for the schedule application
<p> <HTML>
  <HEAD>
  <%@ taglib uri="/WEB-INF/struts-logic.tld"
  prefix="logic" %>
<p> <jsp:useBean id="scheduleBean" scope="request"
  class="schedule.ScheduleBean" 
  />
  <jsp:useBean id="scheduleItem" scope="page"
  class="schedule.ScheduleItem" 
  />
  <TITLE>
  Schedule View
  </TITLE>
  </HEAD>
  <BODY>
  <H1>
  Schedule
  </H1>
<p> <table border="2">
  <tr bgcolor="green">
  <th>Start 
  Date</th>
  <th>Duration</th>
  <th>Text</th>
  <th>Event 
  Type</th>
  </tr>
<p> <logic:iterate id="schedItem"
  type="schedule.ScheduleItem"
  collection="<%= scheduleBean.getList() 
  %>">
  <tr>
  <td><%= 
  schedItem.getStart() %></td>
  <td><%= 
  schedItem.getDuration() %></td>
  <td><%= 
  schedItem.getText() %></td>
  <td><%= 
  schedItem.getEventType() %></td>
  </tr>
  </logic:iterate>
  </table>
  <p>
<p>

<a href="schedEntry.do"> Add New Schedule Item</a><p>

</BODY>
</HTML>


Listing 5: An HTML form using the Struts HTML custom tags.<p>
<%@ taglib uri="/WEB-INF/struts-html.tld"
 prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld"
 prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts.tld"
 prefix="struts" %>
<jsp:useBean id="scheduleItem" scope="request"
 class="schedule.ScheduleItem" />
<jsp:useBean id="scheduleBean" scope="page"
 class="schedule.ScheduleBean" />
<% pageContext.setAttribute("eventTypes",
<x-tab></x-tab>scheduleBean.getEventTypes()); %>
<p> <HEAD>
  <TITLE>
  ScheduleEntryView
  </TITLE>
  </HEAD>
  <BODY>
  <H1>
  Add Schedule Item
  </H1>
  <hr>
  <html:errors/>
  <html:form action="add.do"> 
  <table border="0" width="30%" align="left">
  <tr>
  <th align="right">
  <struts:message 
  key="prompt.duration"/>
  </th>
  <td align="left">
  <html:text 
  property="duration"
  size="16"/>
  </td>
  </tr>
  <tr>
  <th align="right">
  <struts:message 
  key="prompt.eventType"/>
  </th>
  <td align="left">
  <html:select 
  property="eventType">
  <html:options 
  collection="eventTypes"
  property="value"
  labelProperty="label"/>
  </html:select>
<p> </td>
  </tr>
  <tr>
  <th align="right">
  <struts:message 
  key="prompt.start"/>
  </th>
  <td align="left">
  <html:text 
  property="start"
  size="16"/>
  </td>
  </tr>
  <tr>
  <th align="right">
  <struts:message 
  key="prompt.text"/>
  </th>
  <td align="left">
  <html:text 
  property="text"
  size="16"/>
  </td>
  </tr>
<p> <tr>
  <td align="right">
  <struts:submit>
  <bean:message 
  key="button.submit"/>
  </struts:submit>
  </td>
  <td align="right">
  <html:reset>
  <bean:message 
  key="button.reset"/>
  </html:reset>
  </td>
  </tr>
  </table>
  </html:form>
<p>

</BODY>
</HTML>

Listing 6: The validation method for the ScheduleItem form bean


public ActionErrors validate(
 ActionMapping actionMapping,
 HttpServletRequest request) {
 ActionErrors ae = new ActionErrors();
 if (duration < 0 || duration > 31) {
 ae.add("duration", new ActionError(
 "error.invalid.duration", "8"));
 }
 if (text == null || text.length() < 1) {
 ae.add("event text",
 new ActionError("error.no.text"));
 }
 return ae;
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.