You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Oscar Calderon <os...@gmail.com> on 2010/01/21 23:43:46 UTC

Problem with excel download file from Action (S 2.1.8)

Hi to all, i wanna ask you a question about downloading files from 
struts action. Actually i'm working on application that has some 
jasperreports, where by default my action prints as PDF, and it works 
fine, but now i'm trying to serve an excel file, so i'm using the next code:

byte[] arrStream = 
reportObj.getXlsReportStream("/reports/repDetalleRecarg.jrxml", parameters);
ServletOutputStream out = servletResponseObj.getOutputStream();
servletResponseObj.setContentType("application/vnd.ms-excel");
out.write(arrStream);
out.flush();
out.close();

Where reportObj is an object from a class that i code to abstract the 
report byte[] generation, so i obtain the byte array, then declare and 
instantiate the object from ServletOutputStream to write the stream, 
then i establish the content type to excel file and finally write the 
stream, but the problem is that works, but in the browser it gives me 
the option to download a file with extension .action, like   
myAction!myMethod.action  , not an excel file, so i have to download it, 
change it's extension to open it.

The code used for pdf generation is almost equal and it works fine,is this:

byte[] arrStream = 
reportObj.getPdfReportStream("/reports/repDetalleRecarg.jrxml", parameters);
ServletOutputStream out = servletResponseObj.getOutputStream();
servletResponseObj.setContentType("application/pdf");
out.write(arrStream);
out.flush();
out.close();

Thanks in advance

-- 
Oscar Calderon

JAVA Tutorials and How to's? Visit http://www.javahowto.net/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Oscar Calderon <os...@gmail.com>.
Thanks for the aclaration, really i didn't realize about the struts 
documentation's version.I imagine that the better is to add the 
"attachment " to avoid problems.

El 1/22/2010 9:33 AM, Dale Newfield escribió:
> Stephen Turner wrote:
>> Oscar's syntax is described in the Struts docs: 
>> http://struts.apache.org/2.0.8/docs/stream-result.html
>
> It's been updated in 
> http://struts.apache.org/2.1.8.1/docs/stream-result.html
> Too bad the docs are versioned like that so that bad documents don't 
> "go away" once they've been fixed.
>
>> It's working well for us without the "attachment;" piece.
>
> Because the browsers you've tested happen to accept the value even 
> though it's not according to the specification.
>
> Disposition means "OK, what do I do with this thing?"  If the answer 
> is "save it", THEN the question of "as what?" arises.  Stating the 
> filename without first answering the "display it inline" vs. "save it" 
> question is ambiguous and not guaranteed to get you the result you want.
>
> -Dale
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

-- 
Oscar Calderon

JAVA Tutorials and How to's? Visit http://www.javahowto.net/


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: I18nInterceptor / request_locale not working as expected

Posted by Bhaarat Sharma <bh...@gmail.com>.
Hi stephan,

I see you solved your issue regarding resource bundle.

I am having a problem where intermittently the resource bundles get lost and
instead of seeing values on the browser...we start seeing actual properties
like: my.label.name.


using the execute method for your code below...is there any way to find out
whether a resource file named "MyResources" is loaded in the session?


On Thu, Jan 28, 2010 at 2:07 PM, Stephan R. Mueller <
stephanr.mueller@stud.leuphana.de> wrote:

> Hi list,
>
> I'm using struts 2.1.8 with JDK6
>
> I've Message.properties | Message_de_DE.properties
> and Message_en_US.properties.
>
> When switching the local via a browser plugin everything
> works fine and I can see that Session.WW_TRANS_I18N_LOCALE
> contains the expected locale (de_DE or en_US).
> Now I've added a languages.jsp that contains 2 links
> Constructed like this:
> <s:url id="url" action="./languages">
>            <s:param name="request_locale">en_US</s:param>
>        </s:url>
>        <s:a href="%{url}">English</s:a>
>    </li>
>    <li>
>        <s:url id="url" action="./languages">
>            <s:param name="request_locale">de_DE</s:param>
>        </s:url>
>        <s:a href="%{url}">Deutsch</s:a>
>
> When using one of these links I can see that
> Session.WW_TRANS_I18N_LOCALE changes it's value
> accordingly but the displayed language stays the same and
> doesn't change.
> languages.jsp uses an action like the following:
> --snip-
> public class LanguagesShowAction extends ActionSupport implements
> ServletRequestAware {
>
>        HttpServletRequest request;
>        private Map<String, Object> application;
>        private Map<String, Object> session;
>
>        public String execute() {
>
>                application = ActionContext.getContext().getApplication();
>                session = ActionContext.getContext().getSession();
>                session.put("referer", "languages");
>
>                String localeParam = request.getParameter("request_locale");
>
>                System.out.println("localeParam = " + localeParam);
>
>                if (localeParam != null && !localeParam.isEmpty()) {
>                        return "localeChngd"; //used to redirect to another
> jsp
>                }
>                System.out.println("loading languages page");
>                return SUCCESS;
> ---snap---
>
> Any ideas on what I may have done wrong?
> Additional info needed?
>
> Regards,
> Stephan
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: I18nInterceptor / request_locale not working as expected [INVALID/SOLVED]

Posted by "Stephan R. Mueller" <st...@stud.leuphana.de>.
I beg your pardon for spamming.

I'm mixing JSTL/Struts2 and my i18n messages
are generated using JSTL.
JSTL uses a java.util.Locale Object stored in the session
via key "javax.servlet.jsp.jstl.fmt.locale".
While Struts2 uses it's own key.

Now using the JSTL key to stuff the current locale into
and everything is fine.

So long,
Stephan


Am 28.01.2010 um 20:07 schrieb Stephan R. Mueller:

> Hi list,
> 
> I'm using struts 2.1.8 with JDK6
> 
> I've Message.properties | Message_de_DE.properties
> and Message_en_US.properties.
> 
> When switching the local via a browser plugin everything
> works fine and I can see that Session.WW_TRANS_I18N_LOCALE
> contains the expected locale (de_DE or en_US).
> Now I've added a languages.jsp that contains 2 links
> Constructed like this:
> <s:url id="url" action="./languages">
>            <s:param name="request_locale">en_US</s:param>
>        </s:url>
>        <s:a href="%{url}">English</s:a>
>    </li>
>    <li>
>        <s:url id="url" action="./languages">
>            <s:param name="request_locale">de_DE</s:param>
>        </s:url>
>        <s:a href="%{url}">Deutsch</s:a>
> 
> When using one of these links I can see that 
> Session.WW_TRANS_I18N_LOCALE changes it's value
> accordingly but the displayed language stays the same and
> doesn't change.
> languages.jsp uses an action like the following:
> --snip-
> public class LanguagesShowAction extends ActionSupport implements ServletRequestAware {
> 
> 	HttpServletRequest request;
> 	private Map<String, Object> application;
> 	private Map<String, Object> session;
> 	
> 	public String execute() {
> 		
> 		application = ActionContext.getContext().getApplication();
> 		session = ActionContext.getContext().getSession();
> 		session.put("referer", "languages");
> 		
> 		String localeParam = request.getParameter("request_locale");
> 		
> 		System.out.println("localeParam = " + localeParam);
> 
> 		if (localeParam != null && !localeParam.isEmpty()) {
> 			return "localeChngd"; //used to redirect to another jsp
> 		}
> 		System.out.println("loading languages page");
> 		return SUCCESS; 
> ---snap---
> 
> Any ideas on what I may have done wrong?
> Additional info needed?
> 
> Regards,
> Stephan
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


I18nInterceptor / request_locale not working as expected

Posted by "Stephan R. Mueller" <st...@stud.leuphana.de>.
Hi list,

I'm using struts 2.1.8 with JDK6

I've Message.properties | Message_de_DE.properties
and Message_en_US.properties.

When switching the local via a browser plugin everything
works fine and I can see that Session.WW_TRANS_I18N_LOCALE
contains the expected locale (de_DE or en_US).
Now I've added a languages.jsp that contains 2 links
Constructed like this:
<s:url id="url" action="./languages">
            <s:param name="request_locale">en_US</s:param>
        </s:url>
        <s:a href="%{url}">English</s:a>
    </li>
    <li>
        <s:url id="url" action="./languages">
            <s:param name="request_locale">de_DE</s:param>
        </s:url>
        <s:a href="%{url}">Deutsch</s:a>

When using one of these links I can see that 
Session.WW_TRANS_I18N_LOCALE changes it's value
accordingly but the displayed language stays the same and
doesn't change.
languages.jsp uses an action like the following:
--snip-
public class LanguagesShowAction extends ActionSupport implements ServletRequestAware {

	HttpServletRequest request;
	private Map<String, Object> application;
	private Map<String, Object> session;
	
	public String execute() {
		
		application = ActionContext.getContext().getApplication();
		session = ActionContext.getContext().getSession();
		session.put("referer", "languages");
		
		String localeParam = request.getParameter("request_locale");
		
		System.out.println("localeParam = " + localeParam);

		if (localeParam != null && !localeParam.isEmpty()) {
			return "localeChngd"; //used to redirect to another jsp
		}
		System.out.println("loading languages page");
		return SUCCESS; 
---snap---

Any ideas on what I may have done wrong?
Additional info needed?

Regards,
Stephan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Stephen Turner <st...@MIT.EDU>.
On Fri, 22 Jan 2010 10:33:25 -0500, Dale Newfield <da...@newfield.org>  
wrote:

> Disposition means "OK, what do I do with this thing?"  If the answer is
> "save it", THEN the question of "as what?" arises.  Stating the filename
> without first answering the "display it inline" vs. "save it" question
> is ambiguous and not guaranteed to get you the result you want.
>
> -Dale
>

Dale,

Thanks for the explanation - this works much better now. We were basically  
deferring the handling of this to the browser, and by using "attachment;"  
we now get consistent behavior.

Steve

-- 
Stephen Turner
Senior Programmer/Analyst - SAIS
MIT IS&T

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Dale Newfield <da...@newfield.org>.
Stephen Turner wrote:
> Oscar's syntax is described in the Struts docs: 
> http://struts.apache.org/2.0.8/docs/stream-result.html

It's been updated in 
http://struts.apache.org/2.1.8.1/docs/stream-result.html
Too bad the docs are versioned like that so that bad documents don't "go 
away" once they've been fixed.

> It's working well for us without the "attachment;" piece.

Because the browsers you've tested happen to accept the value even 
though it's not according to the specification.

Disposition means "OK, what do I do with this thing?"  If the answer is 
"save it", THEN the question of "as what?" arises.  Stating the filename 
without first answering the "display it inline" vs. "save it" question 
is ambiguous and not guaranteed to get you the result you want.

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Stephen Turner <st...@MIT.EDU>.
On Fri, 22 Jan 2010 09:59:52 -0500, Dale Newfield <da...@newfield.org>  
wrote:

> Glad you were able to get that working.
>
> Where did you find the documentation that suggested this part of your
> stream result?
>
> Oscar wrote:
>>   <param name="contentDisposition">filename="nameoffile.xls"</param>
>
> The filename can also be specified, but the only valid
> "contentDisposition" values are "inline" or "attachment".
>
> So you want:
> <param
> name="contentDisposition">attachment;filename="nameoffile.xls"</param>
>
> -Dale
>

Dale,

Oscar's syntax is described in the Struts docs:  
http://struts.apache.org/2.0.8/docs/stream-result.html

It's working well for us without the "attachment;" piece.

Steve

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Dale Newfield <da...@newfield.org>.
Glad you were able to get that working.

Where did you find the documentation that suggested this part of your 
stream result?

Oscar wrote:
>   <param name="contentDisposition">filename="nameoffile.xls"</param>

The filename can also be specified, but the only valid 
"contentDisposition" values are "inline" or "attachment".

So you want:
<param 
name="contentDisposition">attachment;filename="nameoffile.xls"</param>

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Problem with excel download file from Action (S 2.1.8)

Posted by Oscar <os...@gmail.com>.
Thanks Dale. I searched a little bit about it and i solve the problem
creating a InputStream field in my action, then adding a stream result type
as you said, like this:

<result name="xlsRep" type="stream">
  <param name="contentType">application/vnd.ms-excel</param>

  <param name="inputName">nameOfInputStreamFieldFromYourAction</param>
  <param name="contentDisposition">filename="nameoffile.xls"</param>

  <param name="bufferSize">1024</param>
</result>



2010/1/21 Dale Newfield <da...@newfield.org>

> Oscar Calderon wrote:
>
>> a question about downloading files from struts action.
>>
>
> I would suggest using the stream result type rather than hand-constructing
> the servlet response.  In either case, though, you want to look into the
> "contentDisposition" result parameter.  Probably you want a value something
> like 'attachment;filename="foo.xls"'
>
> -Dale
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Oscar

Re: Problem with excel download file from Action (S 2.1.8)

Posted by Dale Newfield <da...@newfield.org>.
Oscar Calderon wrote:
> a question about downloading files from struts action.

I would suggest using the stream result type rather than 
hand-constructing the servlet response.  In either case, though, you 
want to look into the "contentDisposition" result parameter.  Probably 
you want a value something like 'attachment;filename="foo.xls"'

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


[Struts 2.1.8] Displaying images in a report with the JasperReports plugin

Posted by Celinio Fernandes <ce...@yahoo.com>.
Hi,
Using Struts 2.1.8 and the JasperReports plugin for Struts 2 struts2-jasperreports-plugin-2.1.8.jar.

I am trying to display an image in a PDF report. That image is stored as BLOB in the database.

In struts.xml I have specified the following code:

<action name="afficheFichePDF" class="com.eni.dvtejb.clientStruts2.action.JasperArticleAction">
        <result name="success" type="jasper">
            <param name="location">rapports\ficheArticle.jasper</param>
            <param name="dataSource">artDetails</param>
            <param name="format">PDF</param>
            <param name="reportParameters">paramsRapport</param>
            <param name="imageServletUrl">/servlets/image?image=</param>            
        </result>
</action>

In my web.xml I have added the mapping for the ImageServlet servlet that is part of the JasperReports library :

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/servlets/image</url-pattern>
  </servlet-mapping>
  
In my .jrxml file, produced with iReport, I have defined a field with Expression class equal to java.io.InputStream.


I get the following error :

Struts Problem Report

Struts has detected an unhandled exception:
Messages:     

   1. Cannot cast object 'org.apache.struts2.views.jasperreports.ValueStackDataSource@a0d16c' with class 'org.apache.struts2.views.jasperreports.ValueStackDataSource' to class 'java.io.InputStream'
   2. Error evaluating expression : Source text : $F{image}
   3. Error evaluating expression : Source text : $F{image}

File:     org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation.java
Line number:     340
Stacktraces
javax.servlet.ServletException: Error evaluating expression : Source text : $F{image}

    org.apache.struts2.views.jasperreports.JasperReportsResult.doExecute(JasperReportsResult.java:331)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)

I tried without the image, it works. But then how exactly do you handle images with that plugin ? What is the format of the image supposed to be in the .jrxml file ?

Thanks for helping.