How to access session in struts2?
Either we can implement SessionAware interface or obtain the session attributes from the ActionContext. Implementing SessionAware is preferred over ActionContext. Example
Using ActionContext
With the help of ActionContext instance, provided by ThreadLocal, you can get session attributes.
Map sessionAttibutes = ActionContext.getContext().getSession();
Using SessionAware interface
Before using it ensure that servlet-config Interceptor is included in the Action's stack, comes by default. After then edit your Action and implement the SessionAware interface. Override the setSession and getSession method. We can easily access the session attributes calling the getSession, returning a map of session attributes. If we made any changes in this Map like insert,update or remove a variable or its value, they will be reflected in actual HttpSessionRequest.
Map sessionAttibutes = this.getSession();
Can we divide a large struts.xml file into smaller modules for a modular development?
We can either package a struts.xml files in a JAR or include other struts.xml-format files from a bootstrap struts.xml file. Example
By JAR
Different "module" can also be added to an application by placing struts.xml and related classes into JAR. Remember they must be on the classpath. Templates like FreeMarker and Velocity can also be part of JAR, making it possible to distribute a module in a single, self-contained JAR which is automatically configured on startup.By Include
Best way to make development modular. In case of a large project involving many modules, the include files can be used to organize different modules of the application that are being developed by different team members in different locations or at same place also.<struts>
<include file="HomePage.xml"/>
<include file="User.xml"/>
<include file="Shoppingcart.xml"/>
<include file="Order.xml"/>
<include file="/com/admin/admin.xml"/>
</struts>
Important point to note is each included file must be in the same format as struts.xml, including the DOCTYPE. We can place the files anywhere on the classpath and should be referred to by that path by the attribute known as "file".
How can we download files using struts2 framework?
Stream Result is a custom Result type for sending raw data directly to the HttpServletResponse via an InputStream. It is useful for allowing users to download content. However there are some issues with Internet Explorer when using HTTPS, For details and solution Stream result issue with Internet Explorer over HTTPS. Example
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="OrderSummary.pdf"</param>
<param name="bufferSize">2048</param>
</result>
Let us have a brief about the Parameters:
contentType - stream mime-type sent to the web browser, default is text/plain.
contentLength - stream length in bytes.
contentDisposition - the content disposition header value for specifing the file name.
inputName - the name of the InputStream property from the chained action.
bufferSize - the size of the buffer to copy from input to output.
allowCaching - if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content, default values is true.
contentCharSet - if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header
These parameters can also be set or overrided in action using a named getter method in Action.
No comments:
Post a Comment