StrutsCatalog: Sick of URLs and all the "relative" and "absolute" conundrums? Do you hate having to cooperate with client browsers? You've found Valhalla! Substitute protocols in struts actions for all that nonsense.

Here's the action:

public final class ResourceAction
    extends Action {
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws IOException,
             ServletException {
    new ResourceFacade().handle(request, response);
    return null;
  }
}

Here's the facade:

public class ResourceFacade {
  public ResourceFacade() {
    super();
  }

  public void handle(HttpServletRequest request,
                     HttpServletResponse response) {
    new WriteResponse().write(new InitResponse().init(request, response), response);
    return;
  }
}

Here's the initialization of the response:

{{{public class InitResponse {

} }}}

Here is the response writer:

public class WriteResponse {

  public WriteResponse() {
  }

  public static void write(String fileName,HttpServletResponse response) {
    // Necessary to avoid problems with Internet Explorer
    response.setHeader("Cache-Control", "");
    response.setHeader("Pragma", "");
    response.setHeader("Expires", "");
    response.addHeader("Content-Disposition","filename=" + fileName);

    try {
      FileInputStream     fis      = new FileInputStream(fileName);
      BufferedInputStream bis      = new BufferedInputStream(fis);
      byte[]              bytes    = new byte[bis.available()];
      OutputStream        os       = response.getOutputStream();
      bis.read(bytes);
      os.write(bytes);
      os.close();
    } catch (IOException ioe) {
      StdOut.redirect("log.error","WriteResponse: problem file is: " + fileName + "\n" + StackTrace.trace(ioe));
    }
  }
}

Here are the image constants:

public class ImageConstant {
  private ImageConstant() {
  }
  // Basic resource identifiers for request parameters.
  public static final String TYPE = "file_type";
  public static final String NAME = "file_name";

  // Content types
  public static final String CSS_CONTENT = "text/css";
  public static final String HTML_CONTENT = "text/html";
  public static final String TEXT_CONTENT = "text/plain";
  public static final String GIF_CONTENT = "image/gif";
  public static final String JPEG_CONTENT = "image/jpeg";
  public static final String FLASH_CONTENT = "application/x-shockwave-flash";
  public static final String APPLET_CONTENT = "application/x-java-applet";

  // File types
  public static final String CSS_FILE = "css";
  public static final String HTML_FILE = "html";
  public static final String TEXT_FILE = "text";
  public static final String GIF_FILE = "gif";
  public static final String JPEG_FILE = "jpeg";
  public static final String FLASH_FILE = "flash";
  public static final String APPLET_FILE = "applet";

  // Utility constants for file locations
  private static final String VIEW_PATH   = ("resource" + File.separator);
  private static final String APPLET_PATH = ("classes" + File.separator + "com" + File.separator + "crackwillow" + File.separator);

  // File file locations
  public static final String CSS_LOCATION = (VIEW_PATH + "css" + File.separator);
  public static final String HTML_LOCATION = (VIEW_PATH   + "html"   + File.separator);
  public static final String TEXT_LOCATION = (VIEW_PATH   + "text"    + File.separator);
  public static final String GIF_LOCATION = (VIEW_PATH   + "gif"    + File.separator);
  public static final String JPEG_LOCATION = (VIEW_PATH   + "jpeg"    + File.separator);
  public static final String FLASH_LOCATION = (VIEW_PATH   + "flash"    + File.separator);
  public static final String APPLET_LOCATION = (APPLET_PATH + "applet" + File.separator);
}

Here's the Classpath code:

public final class Classpath {
  public static final String HERE = Classpath.class.getClassLoader().getResource("com" + File.separator + 
                                                                                 "crackwillow" + File.separator + 
                                                                                 "classpath" + File.separator + 
                                                                                 "Classpath.class").getFile();
  public static final String WEB_INF = HERE.substring(0, HERE.lastIndexOf("classes"));
}

You know the code on the Struts ActionServlet, I assume. Here's the action mapping:

<action 
  path='/resource' 
  type='com.crackwillow.struts.action.ResourceAction'
  scope='request'/>

And, here is a use.

<%@ page language='java' %>
<%@ page contentType='text/html; charset=UTF-8' %>
<%@ taglib uri='struts-bean'    prefix='bean' %>
<%@ taglib uri='struts-html'    prefix='html' %>
<%@ taglib uri='struts-tiles'   prefix='tiles' %>

<html:html>
  <head>
    <title>HTML Buttons Example</title>
      <link
        rel="STYLESHEET"
        type="text/css"
        href="resource.do?file_type=css&file_name=index.css">
  </head>
  <body
    background="resource.do?file_type=gif&file_name=green.gif"
    text="#ffffff">
    <h1
      align="center">
      HTML Buttons Example
    </h1>
    <table
      border="0"
      align="center"
      width="80%"
      cellpadding="3"
      cellspacing="0">
      <tr>
        <td>
          <h1>
            Registration Form
          </h1>
        </td>
      </tr>
    </table>
    <html:form
      name="ButtonForm"
      type="com.crackwillow.struts.form.ButtonForm" method="get"
      action='/button.do'>
      <table
        border="0"
        align="center"
        width="80%"
        cellpadding="3"
        cellspacing="0">
        <tr>
          <td
            width="1%"
            nowrap>
            First name:&nbsp;
          </td>
          <td>
            <input
              type="text"
              name="firstName"
              value="">
          </td>
        </tr>
        <tr>
          <td
            width="1%"
            nowrap>
            Last name:&nbsp;
          </td>
          <td>
            <input
              type="text"
              name="lastName"
              value="">
          </td>
        </tr>
        <tr>
          <td
            width="1%"
            nowrap>
            Email:&nbsp;
          </td>
          <td>
            <input
              type="text"
              name="ssn"
              value="">
          </td>
        </tr>
        <tr>
          <td
            width="1%"
            nowrap>
            &nbsp;
          </td>
          <td>
          <br><br>
              <html:image
                property="button.submit"
                src="resource.do?file_type=gif&file_name=cw_logo.gif"/>
              <html:image
                property="button.clear"
                src="resource.do?file_type=gif&file_name=cw_logo.gif"/>
              <html:image
                property="button.cancel"
                src="resource.do?file_type=gif&file_name=cw_logo.gif"/>
              <html:image
                property="button.reset"
                src="resource.do?file_type=gif&file_name=cw_logo.gif"/>
          </td>
        </tr>
      </table>
    </html:form>
  </body>
</html:html>

What is this button nonsense? See StrutsCatalogFiveMultipleButtonSolutions

-- Michael McGrady

mike@michaelmcgrady.com

cto@crackwillow.com

StrutsCatalogEschewUrlForProtocol (last edited 2009-09-20 23:12:08 by localhost)