You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Roberto Nunnari <ro...@supsi.ch> on 2007/07/18 16:48:41 UTC

Struts2 and images from database

Hello.

What is the best way to serve images stored in database in Struts2?

action, resultType, or servlet?

Thank you.

-- 
Robi


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


Re: Struts2 and images from database

Posted by bartlebooth <ba...@telenet.be>.
Perhaps the result name in my example does not correspond to the return 
value of your action ?
I assume you are using the param interceptor here

public class ImageAction {
    private    int imageId;
    private    Image image;
   
    // this method is called when using the param-interceptor
    public void setImageId(int imageId) {
       this.imageId = imageId;
    }

    public InputStream getImageStream() {
       // create a stream from your image
    }

    public String execute() {
       // get image
       image = dao.getImage() // or something like that, could be in the 
prepare method if you're using the prepare interceptor

       return "all"; // if you use "success" here, you have to modify 
the result name in your struts.xml
    }
}

hope this helps

bartlebooth



Roberto Nunnari wrote:
> Hi bartlebooth.
>
> That is very neat!
>
> But the getImageStream method in my action is never called..
>
> why?
>
>
>
> bartlebooth wrote:
>> You could use the stream result-type.
>>
>>            <result name="all" type="stream">
>>                <param name="contentType">image/jpg</param>
>>                <param name="inputName">imageStream</param>
>>                <param 
>> name="contentDisposition">filename="image.jpg"</param>
>>                <param name="bufferSize">1024</param>
>>            </result>
>>
>> In your action, you have to provide a method getImageStream 
>> (corresponding to the inputName parameter above).
>>
>> public InputStream getImageStream() {
>>    // create a stream from your image
>> }
>>
>>
>> Josh Vickery wrote:
>>> If you return null from your Action you can write directly to the 
>>> response:
>>>
>>> HttpServletResponse response = ServletActionContext.getResponse();
>>> response.setContentType("image/jpg");
>>> out = response.getOutputStream();
>>> write image here
>>> return null;
>>>
>>> On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
>>>> hehe.. ok.. let's add some context..
>>>>
>>>> Till now I was using a servlet to serve to the web images stored
>>>> in a database. To access the database I had the myDAO
>>>> object in the applicationContext and so it was accessible by
>>>> the servlet and actions.
>>>>
>>>> Now I have added Spring to the game, and the myDAO object is
>>>> no longer in the applicationContext, as it is managed by Spring.
>>>>
>>>> All my actions now extends a base class that has a setMyDAO
>>>> method, and so they get access to the myDAO object.
>>>>
>>>> But what about the servlet? It will not be injected by Spring.
>>>>
>>>> What is the solution?
>>>>
>>>> Thank you.
>>>>
>>>> Best regards.
>>>>
>>>> -- 
>>>> Robi.
>>>>
>>>>
>>>>
>>>> Roberto Nunnari wrote:
>>>> > Hello.
>>>> >
>>>> > What is the best way to serve images stored in database in Struts2?
>>>> >
>>>> > action, resultType, or servlet?
>>>> >
>>>> > Thank you.
>>>> >
>
>
>
> ---------------------------------------------------------------------
> 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


Re: Struts2 and images from database

Posted by Roberto Nunnari <ro...@supsi.ch>.
my fault.. action.execute() was still returning SUCCESS while
the mapping result name is "all"!

Thank you!

--
Robi.


Roberto Nunnari wrote:
> Hi bartlebooth.
> 
> That is very neat!
> 
> But the getImageStream method in my action is never called..
> 
> why?
> 
> 
> 
> bartlebooth wrote:
>> You could use the stream result-type.
>>
>>            <result name="all" type="stream">
>>                <param name="contentType">image/jpg</param>
>>                <param name="inputName">imageStream</param>
>>                <param 
>> name="contentDisposition">filename="image.jpg"</param>
>>                <param name="bufferSize">1024</param>
>>            </result>
>>
>> In your action, you have to provide a method getImageStream 
>> (corresponding to the inputName parameter above).
>>
>> public InputStream getImageStream() {
>>    // create a stream from your image
>> }
>>
>>
>> Josh Vickery wrote:
>>> If you return null from your Action you can write directly to the 
>>> response:
>>>
>>> HttpServletResponse response = ServletActionContext.getResponse();
>>> response.setContentType("image/jpg");
>>> out = response.getOutputStream();
>>> write image here
>>> return null;
>>>
>>> On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
>>>> hehe.. ok.. let's add some context..
>>>>
>>>> Till now I was using a servlet to serve to the web images stored
>>>> in a database. To access the database I had the myDAO
>>>> object in the applicationContext and so it was accessible by
>>>> the servlet and actions.
>>>>
>>>> Now I have added Spring to the game, and the myDAO object is
>>>> no longer in the applicationContext, as it is managed by Spring.
>>>>
>>>> All my actions now extends a base class that has a setMyDAO
>>>> method, and so they get access to the myDAO object.
>>>>
>>>> But what about the servlet? It will not be injected by Spring.
>>>>
>>>> What is the solution?
>>>>
>>>> Thank you.
>>>>
>>>> Best regards.
>>>>
>>>> -- 
>>>> Robi.
>>>>
>>>>
>>>>
>>>> Roberto Nunnari wrote:
>>>> > Hello.
>>>> >
>>>> > What is the best way to serve images stored in database in Struts2?
>>>> >
>>>> > action, resultType, or servlet?
>>>> >
>>>> > Thank you.
>>>> >



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


Re: Struts2 and images from database

Posted by Roberto Nunnari <ro...@supsi.ch>.
Hi bartlebooth.

That is very neat!

But the getImageStream method in my action is never called..

why?



bartlebooth wrote:
> You could use the stream result-type.
> 
>            <result name="all" type="stream">
>                <param name="contentType">image/jpg</param>
>                <param name="inputName">imageStream</param>
>                <param 
> name="contentDisposition">filename="image.jpg"</param>
>                <param name="bufferSize">1024</param>
>            </result>
> 
> In your action, you have to provide a method getImageStream 
> (corresponding to the inputName parameter above).
> 
> public InputStream getImageStream() {
>    // create a stream from your image
> }
> 
> 
> Josh Vickery wrote:
>> If you return null from your Action you can write directly to the 
>> response:
>>
>> HttpServletResponse response = ServletActionContext.getResponse();
>> response.setContentType("image/jpg");
>> out = response.getOutputStream();
>> write image here
>> return null;
>>
>> On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
>>> hehe.. ok.. let's add some context..
>>>
>>> Till now I was using a servlet to serve to the web images stored
>>> in a database. To access the database I had the myDAO
>>> object in the applicationContext and so it was accessible by
>>> the servlet and actions.
>>>
>>> Now I have added Spring to the game, and the myDAO object is
>>> no longer in the applicationContext, as it is managed by Spring.
>>>
>>> All my actions now extends a base class that has a setMyDAO
>>> method, and so they get access to the myDAO object.
>>>
>>> But what about the servlet? It will not be injected by Spring.
>>>
>>> What is the solution?
>>>
>>> Thank you.
>>>
>>> Best regards.
>>>
>>> -- 
>>> Robi.
>>>
>>>
>>>
>>> Roberto Nunnari wrote:
>>> > Hello.
>>> >
>>> > What is the best way to serve images stored in database in Struts2?
>>> >
>>> > action, resultType, or servlet?
>>> >
>>> > Thank you.
>>> >



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


Re: Struts2 and images from database

Posted by bartlebooth <ba...@telenet.be>.
You could use the stream result-type.

            <result name="all" type="stream">
                <param name="contentType">image/jpg</param>
                <param name="inputName">imageStream</param>
                <param 
name="contentDisposition">filename="image.jpg"</param>
                <param name="bufferSize">1024</param>
            </result>

In your action, you have to provide a method getImageStream 
(corresponding to the inputName parameter above).

public InputStream getImageStream() {
    // create a stream from your image
}


Josh Vickery wrote:
> If you return null from your Action you can write directly to the 
> response:
>
> HttpServletResponse response = ServletActionContext.getResponse();
> response.setContentType("image/jpg");
> out = response.getOutputStream();
> write image here
> return null;
>
> On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
>> hehe.. ok.. let's add some context..
>>
>> Till now I was using a servlet to serve to the web images stored
>> in a database. To access the database I had the myDAO
>> object in the applicationContext and so it was accessible by
>> the servlet and actions.
>>
>> Now I have added Spring to the game, and the myDAO object is
>> no longer in the applicationContext, as it is managed by Spring.
>>
>> All my actions now extends a base class that has a setMyDAO
>> method, and so they get access to the myDAO object.
>>
>> But what about the servlet? It will not be injected by Spring.
>>
>> What is the solution?
>>
>> Thank you.
>>
>> Best regards.
>>
>> -- 
>> Robi.
>>
>>
>>
>> Roberto Nunnari wrote:
>> > Hello.
>> >
>> > What is the best way to serve images stored in database in Struts2?
>> >
>> > action, resultType, or servlet?
>> >
>> > Thank you.
>> >
>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>
>


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


Re: Struts2 and images from database

Posted by Roberto Nunnari <ro...@supsi.ch>.
Thank you Josh!

I'll try that right away..
I thought that struts actions could only return text..

Do you have to change the resultType for that action?

--
Robi


Josh Vickery wrote:
> If you return null from your Action you can write directly to the response:
> 
> HttpServletResponse response = ServletActionContext.getResponse();
> response.setContentType("image/jpg");
> out = response.getOutputStream();
> write image here
> return null;
> 
> On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
>> hehe.. ok.. let's add some context..
>>
>> Till now I was using a servlet to serve to the web images stored
>> in a database. To access the database I had the myDAO
>> object in the applicationContext and so it was accessible by
>> the servlet and actions.
>>
>> Now I have added Spring to the game, and the myDAO object is
>> no longer in the applicationContext, as it is managed by Spring.
>>
>> All my actions now extends a base class that has a setMyDAO
>> method, and so they get access to the myDAO object.
>>
>> But what about the servlet? It will not be injected by Spring.
>>
>> What is the solution?
>>
>> Thank you.
>>
>> Best regards.
>>
>> -- 
>> Robi.
>>
>>
>>
>> Roberto Nunnari wrote:
>> > Hello.
>> >
>> > What is the best way to serve images stored in database in Struts2?
>> >
>> > action, resultType, or servlet?
>> >
>> > Thank you.
>> >
>>
>>
>> ---------------------------------------------------------------------
>> 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


Re: Struts2 and images from database

Posted by Josh Vickery <jo...@vickeryj.com>.
If you return null from your Action you can write directly to the response:

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("image/jpg");
out = response.getOutputStream();
write image here
return null;

On 7/18/07, Roberto Nunnari <ro...@supsi.ch> wrote:
> hehe.. ok.. let's add some context..
>
> Till now I was using a servlet to serve to the web images stored
> in a database. To access the database I had the myDAO
> object in the applicationContext and so it was accessible by
> the servlet and actions.
>
> Now I have added Spring to the game, and the myDAO object is
> no longer in the applicationContext, as it is managed by Spring.
>
> All my actions now extends a base class that has a setMyDAO
> method, and so they get access to the myDAO object.
>
> But what about the servlet? It will not be injected by Spring.
>
> What is the solution?
>
> Thank you.
>
> Best regards.
>
> --
> Robi.
>
>
>
> Roberto Nunnari wrote:
> > Hello.
> >
> > What is the best way to serve images stored in database in Struts2?
> >
> > action, resultType, or servlet?
> >
> > Thank you.
> >
>
>
> ---------------------------------------------------------------------
> 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


Re: Struts2 and images from database

Posted by Roberto Nunnari <ro...@supsi.ch>.
hehe.. ok.. let's add some context..

Till now I was using a servlet to serve to the web images stored
in a database. To access the database I had the myDAO
object in the applicationContext and so it was accessible by
the servlet and actions.

Now I have added Spring to the game, and the myDAO object is
no longer in the applicationContext, as it is managed by Spring.

All my actions now extends a base class that has a setMyDAO
method, and so they get access to the myDAO object.

But what about the servlet? It will not be injected by Spring.

What is the solution?

Thank you.

Best regards.

--
Robi.



Roberto Nunnari wrote:
> Hello.
> 
> What is the best way to serve images stored in database in Struts2?
> 
> action, resultType, or servlet?
> 
> Thank you.
> 


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