Date: 2004-04-29T09:09:59
   Editor: 131.191.40.91 <>
   Wiki: Apache Struts Wiki
   Page: StrutsCatalogEschewUrlForProtocol
   URL: http://wiki.apache.org/struts/StrutsCatalogEschewUrlForProtocol

   no comment

New Page:

StrutsCatalog: '''Sick of URLs and all the "relative" and "absolute" conundrums?  Do 
you have 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 {
  private   static Log log = LogFactory.getLog(InitResponse.class);
  public InitResponse() {
  }

  public String init(HttpServletRequest request,
                     HttpServletResponse response) {
    String fileType = (request.getParameter(ImageConstant.TYPE)).intern();
    String fileName = request.getParameter(ImageConstant.NAME);
    String contentType = null;
    String file = null;
    if (fileType == ImageConstant.JPEG_FILE) {
      contentType = ImageConstant.JPEG_CONTENT;
      file        = ImageConstant.JPEG_LOCATION;
    }
    else if (fileType == ImageConstant.FLASH_FILE) {
      contentType = ImageConstant.FLASH_CONTENT;
      file = ImageConstant.FLASH_LOCATION;
    }
    else if (fileType == ImageConstant.GIF_FILE) {
      contentType = ImageConstant.GIF_CONTENT;
      file        = ImageConstant.GIF_LOCATION;
    }
    else if (fileType == ImageConstant.TEXT_FILE || fileType == 
ImageConstant.CSS_FILE) {
      contentType = ImageConstant.TEXT_CONTENT;
      file        = ImageConstant.TEXT_LOCATION;
    }
    else if (fileType == ImageConstant.HTML_FILE) {
      contentType = ImageConstant.HTML_CONTENT;
      file        = ImageConstant.HTML_LOCATION;
    }
    else if (fileType == ImageConstant.APPLET_FILE) {
      contentType = ImageConstant.APPLET_CONTENT;
      file        = ImageConstant.APPLET_LOCATION;
    }
    response.setContentType(contentType);
    return Classpath.WEB_INF + file + fileName;
  }
}
}}}

Here are the image constants:

{{{
public class InitResponse {
  private   static Log log = LogFactory.getLog(InitResponse.class);
  public InitResponse() {
  }

  public String init(HttpServletRequest request,
                     HttpServletResponse response) {
    String fileType = (request.getParameter(ImageConstant.TYPE)).intern();
    String fileName = request.getParameter(ImageConstant.NAME);
    String contentType = null;
    String file = null;
    if (fileType == ImageConstant.JPEG_FILE) {
      contentType = ImageConstant.JPEG_CONTENT;
      file        = ImageConstant.JPEG_LOCATION;
    }
    else if (fileType == ImageConstant.FLASH_FILE) {
      contentType = ImageConstant.FLASH_CONTENT;
      file = ImageConstant.FLASH_LOCATION;
    }
    else if (fileType == ImageConstant.GIF_FILE) {
      contentType = ImageConstant.GIF_CONTENT;
      file        = ImageConstant.GIF_LOCATION;
    }
    else if (fileType == ImageConstant.TEXT_FILE || fileType == 
ImageConstant.CSS_FILE) {
      contentType = ImageConstant.TEXT_CONTENT;
      file        = ImageConstant.TEXT_LOCATION;
    }
    else if (fileType == ImageConstant.HTML_FILE) {
      contentType = ImageConstant.HTML_CONTENT;
      file        = ImageConstant.HTML_LOCATION;
    }
    else if (fileType == ImageConstant.APPLET_FILE) {
      contentType = ImageConstant.APPLET_CONTENT;
      file        = ImageConstant.APPLET_LOCATION;
    }
    response.setContentType(contentType);
    return Classpath.WEB_INF + file + fileName;
  }
}
}}}

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 StrutsCatalogMultipleImageButtonsWithNoJavaScript  
Enjoy!

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to