DekGenius.com
Previous Section  < Day Day Up >  Next Section

10.3 Creating the Controller

We've set things up in the view so the data in the HTML controls is sent to Ch10_04.do. That's the controller, or action servlet, in our application. We'll connect the extension .do to the action servlet for the application in web.xml. You can see how that's done in web.xml in Example 10-5, using the <servlet> and <servlet-mapping> elements, much as we did in the previous chapter.

Example 10-5. web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
        <display-name>Struts Example Application</display-name>

  <!-- Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <taglib>
    <taglib-uri>/Ch10</taglib-uri>
    <taglib-location>/WEB-INF/Ch10.tld</taglib-location>
  </taglib>

  <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>

</web-app>

This file, web.xml, goes in the deployment\WEB-INF directory in the Eclipse project. That connects the extension .do to the action servlet in our application, but we haven't yet specified the actual code for that servlet. You do that in the struts-config.xml file, which also goes in the deployment\WEB-INF directory. For this example, the action servlet's code is to be Ch10_04.class, the summary page that should be displayed if there was no problem with the data entered by the user is Ch10_05.jsp, and the bean that will store the data from the user is Ch10_06.class. You can see how this works in Example 10-6.

Example 10-6. struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

  <form-beans>
    <form-bean name="Ch10_06" type="org.eclipsebook.ch10.Ch10_06"/>
  </form-beans>

  <action-mappings>
    <action path="/Ch10_04"
      type="org.eclipsebook.ch10.Ch10_04"
      name="Ch10_06"
      scope="request"
      input="/Ch10_01.jsp">
      <forward name="OK" path="/Ch10_05.jsp"/>
    </action>

  </action-mappings>

</struts-config>

Now the data in the view will be sent to the Ch10_06 bean, and control will pass to the controller, Ch10_04. In the controller, we're passed an object of the bean class that holds the data the user entered into the view's HTML controls, and we'll check that data in the controller's code, as you see in Example 10-7. If the user hasn't entered some of the needed data, we'll display an error message; if the data is OK, we'll forward it on to the summary page, Ch10_05.jsp. This file, Ch10_04.java, goes into the src folder, as do all the other Java source files in the project.

Example 10-7. A forwarding class for Struts
package org.eclipsebook.ch10;

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

public class Ch10_04 extends Action 
{
    public ActionForward execute(ActionMapping mapping,
                ActionForm form,
                HttpServletRequest request,
                HttpServletResponse response)
        throws Exception {

        ActionErrors actionerrors = new ActionErrors( );
                
        Ch10_06 orderForm = (Ch10_06)form;
        
        String email = orderForm.getEmail( );        
        if(email.trim( ).equals("")) {
            actionerrors.add(ActionErrors.GLOBAL_ERROR, new 
                ActionError("error.noemail"));
        }
        
        String type = orderForm.getType( );        
        if(type.trim( ).equals("")) {
            actionerrors.add("ActionErrors.GLOBAL_ERROR", new 
                ActionError("error.notype"));
        }
        
        String[] items = orderForm.getItems( );        
        if(items == null) {
            actionerrors.add("ActionErrors.GLOBAL_ERROR", new 
                ActionError("error.noitems"));
        }
            
        if(actionerrors.size( ) != 0) {            
            saveErrors(request, actionerrors);
            return new ActionForward(mapping.getInput( ));            
        }

        return mapping.findForward("OK");
    }
}

The error messages referenced in the action servlet's code are stored in the file ApplicationResources.properties, which goes in the deployment\WEB-INF\classes directory in the Eclipse project. You can see this file in Example 10-8 (and it's connected to the action servlet in web.xml, Example 10-5).

Example 10-8. ApplicationResources.properties
email=<b>Your email:</b>
type=<b>Type:</b>
items=<b>Items:</b>

error.noemail=<li><font color="red">Please enter your email address.</font></li>
error.notype=<li><font color="red">Please select a type.</font></li>
error.noitems=<li><font color="red">Please select at least one item.</font></li>

errors.header=<font color="red">Please correct the following error(s):<ul>
errors.footer=</ul>

If the user omits some data in the view, he'll see one of the error messages in ApplicationResources.properties, as you see in Figure 10-3.

Figure 10-3. Handling an error
figs/ecps_1003.gif
    Previous Section  < Day Day Up >  Next Section