You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Max Cooper <ma...@maxcooper.com> on 2002/08/23 02:35:23 UTC

nocache=true, but I want to allow caching for one action

I am using Struts 1.0.2 and I can't easily switch to 1.1. In my AcionServlet
specification in web.xml, I have nocache set to true, like so (irrelevant
stuff omitted):

   <servlet>
      <servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class
>
      <init-param>
         <param-name>nocache</param-name>
         <param-value>true</param-value>
      </init-param>
   </servlet>

However, I do have one Action that I would like to allow the browser to
cache. Can I specify this somehow, or am I SOL? I looked in the
struts-config.xml DTD for the action element, but I didn't see anything
relevant.

Thanks,
-Max


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: nocache=true, but I want to allow caching for one action

Posted by Max Cooper <ma...@maxcooper.com>.
With some help from my local Struts gurus (Prakash M. and Steve D.), we came
up with these three possible solutions:

#1) Create a second controller instance that allows caching and explicitly
map the URLs for the actions you want caching for the this controller. The
downsides to this are that is takes longer to start the server and it seems
wasteful to have two controllers ready to handle any of my Actions (when any
particular action will only be handled by one of them). It seems like it
could be a PITA to configure all the mappings for the "exceptional" actions
that you want to
have a caching policy other than the default. Here's an example web.xml for
having caching off for everything but /cacheMeBaby.do:

   <!-- nocache action servlet -->
   <servlet>
      <servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class
>
      <init-param>
         <param-name>nocache</param-name>
         <param-value>true</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>

   <!-- caching action servlet -->
   <servlet>
      <servlet-name>action-cache</servlet-name>

<servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class
>
      <init-param>
         <param-name>nocache</param-name>
         <param-value>false</param-value>
      </init-param>
      <load-on-startup>3</load-on-startup>
   </servlet>

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


#2) Do like #1 except have different struts-config.xml files for each one to
avoid duplication. In this scenario, you would explicitly put each of your
actions in just one of the files, indicating whether you want caching or
not. In this scenario, it would probably be smart to decide on some
convention for the URL patterns to make the mapping easier.


#3) "Clear" the headers that control caching in the Action.

I am not sure what to do about the Pragma header, but this seems like it
works:

         response.setHeader("Pragma", "");
         response.setHeader("Cache-Control", "private");
         response.setDateHeader("Expires", Long.MAX_VALUE);

Of course, this does set us up for a "Y2K"-like problem on 17 Aug 292278994
(the Date that Long.MAX_VALUE translates to) but I don't know what else to
do about that.


I only have one Action for which I want caching, so I am going with option
#3.

-Max


----- Original Message -----
From: "Max Cooper" <ma...@maxcooper.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, August 22, 2002 5:35 PM
Subject: nocache=true, but I want to allow caching for one action


> I am using Struts 1.0.2 and I can't easily switch to 1.1. In my
AcionServlet
> specification in web.xml, I have nocache set to true, like so (irrelevant
> stuff omitted):
>
>    <servlet>
>       <servlet-name>action</servlet-name>
>
>
<servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class
> >
>       <init-param>
>          <param-name>nocache</param-name>
>          <param-value>true</param-value>
>       </init-param>
>    </servlet>
>
> However, I do have one Action that I would like to allow the browser to
> cache. Can I specify this somehow, or am I SOL? I looked in the
> struts-config.xml DTD for the action element, but I didn't see anything
> relevant.
>
> Thanks,
> -Max
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: nocache=true, but I want to allow caching for one action

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 22 Aug 2002, Max Cooper wrote:

> Date: Thu, 22 Aug 2002 17:35:23 -0700
> From: Max Cooper <ma...@maxcooper.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: nocache=true, but I want to allow caching for one action
>
> I am using Struts 1.0.2 and I can't easily switch to 1.1. In my AcionServlet
> specification in web.xml, I have nocache set to true, like so (irrelevant
> stuff omitted):
>

By the way, the 1.1 equivalent to the "nocache" servlet init parameter is
the "nocache" attribute in the <controller> element.

>    <servlet>
>       <servlet-name>action</servlet-name>
>
> <servlet-class>org.apache.struts.tiles.ActionComponentServlet</servlet-class
> >
>       <init-param>
>          <param-name>nocache</param-name>
>          <param-value>true</param-value>
>       </init-param>
>    </servlet>
>
> However, I do have one Action that I would like to allow the browser to
> cache. Can I specify this somehow, or am I SOL? I looked in the
> struts-config.xml DTD for the action element, but I didn't see anything
> relevant.
>

There's nothing current that lets you do this -- the nocache setting is
treated as global.  You'll have to subclass ActionServlet (1.0) or
RequestProcessor (1.1) to change that, although if you're using a servlet
2.3 based container you might consider doing this kind of thing in a
Filter instead of in Struts.

> Thanks,
> -Max

Craig


>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>