开发者

Struts2 execute multiple results off of one action from the struts.xml file

开发者 https://www.devze.com 2023-03-08 05:43 出处:网络
I want to run two action results from the struts.xml file off of one click from a submit button on a form. Is this possible, and if so how? I have a short example below:

I want to run two action results from the struts.xml file off of one click from a submit button on a form. Is this possible, and if so how? I have a short example below:

After hitting my submit button on my form is there anyway in struts2 to run both action result success_csv and reload the page with result success

I have a form on my index.jsp such as:

<form id="example" name="example" action="makecsv" >
<!-- form sutff --> 
<input class="submit" type="submit" value="Submit" >
</form>

In my strut.xml file I have this action:

<action name="makecsv" class="com.example.actions.MakecsvAction">

<result name="success_csv" type="stream">

            <param name="contentType">image/jpeg</param>开发者_如何学C
            <param name="inputName">fileStream</param>
            <param name="contentDisposition">attachment;filename="${filename}"</param>
            <param name="bufferSize">1024</param>

</result>
<result name="success"> index.jsp </result>
</action>

Action result success_csv will stream out a file that the user can save or open. Action result success will open index.jsp.

Also my action file MackcsvAction.java:

public class RegisterAction extends ActionSupport   
{
public String execute() throws Exception 
{
     //blahblah code that creates csv file 
     //sets the filename and fileStream of the file

     return "succes_csv";
}
}

Right now it only gives the csv file I would like it also to open index.jsp


Before thinking about Struts2, ask yourself what do you want to happen from a higher level, from the browser point of view - recall that a browser sents a request, and it expects just a response (a html page OR som other content).

If you want the behaviour in which the request returns a html result page and then (automagically) a download starts, read about that kind of pattern. (Note, BTW, that there are always two requests.)

Only after this step, when you have decided what you want to happen, think about how you are going to make it happen - i.e. think how you'll implement that with Struts.

For example, I'd say that your "first" action generates the file to be later downloaded, saves it in a temporary location (together with some secure id), and returns the html pages as result. That page includes a reference to a new request (via a meta refresh, javascript, iframe , or whatever - and probably also an explicit link with the typical 'If your download does not start in a few seconds, click here'), with the id of hte file as parameter. This request hits a second action, that receives a file id, and returns it as a binary result.


Use listentopics and notifyTopic in Jsp

Visit the following link for listentopics and notifyTopic syntax

http://struts.apache.org/2.0.11/docs/struts-2-spring-2-jpa-ajax.html

add listentopic to form say "filecomplete" on Download file link write notifytopic "triggerFilecomplete" this will trigger the form submission.


You just can't stream an image and at the same time show HTML page.

One solution i could figure out is using javascript to open the image in a new window.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号