madvoc : Action question regarding download file after submt post method

12 views
Skip to first unread message

moh....@googlemail.com

unread,
Nov 5, 2015, 7:56:32 AM11/5/15
to Jodders
Hello,

we are changing few pages (realized via servlets only) to madvoc.

After setting up madvoc itself successfully, the first issue occurs.

In the jsp-page you can write a sql statement and choice output format (csv,html).
By hitting the form button , method will be executed in Action class.

The question is:  Is it possible to display the result as html table or as cvs-download-file within one action method?

Code snippets:

sql.jsp
            <jodd-lagarto:form>
               
<form id="form" method="post" action="sql.executeSql">
                   
<fieldset><legend>SQL</legend>
                   
<label>Statement:<br />
                   
<textarea name="sql" cols="100" rows="5">${sqlResult.sql}</textarea></label>
                   
<br />
                   
<label>output format:
                       
<select name="format" size="1">
                       
<jodd:iter items="${outputTypes}" var="i">
                           
<jodd:ifelse test="${format == i}">
                               
<jodd:then>
                                   
<option selected>${i}</option>
                               
</jodd:then>
                               
<jodd:else>
                                   
<option>${i}</option>
                               
</jodd:else>
                           
</jodd:ifelse>
                       
</jodd:iter>
                       
</select>
                   
</label>
                   
<br />
                   
<button type="submit" name="submit">Go</button>
                   
</fieldset>
               
</form>
           
</jodd-lagarto:form>


SqlAction.java
@MadvocAction
public class SqlAction {

   
@Action(method = "POST", extension = Action.NONE)
   
public String executeSql() {
   
// cut of business logic (execute sql, set result in model class)

   
return "#";  

   
}


}

Showing data within html table works.

I struggle with downloading cvs file.
Within old servlet code i have used :

                                response.setHeader("Content-Type", "text/csv");
                                response
.setHeader("Content-Disposition", "attachment; filename=\"sql.csv\"");
                               
out.println(csvstringwriter.getBuffer().toString());


Should i use two action methods? One for html output, other for csv file - mybe with "RawDownload"
I can change the action attribute of the html form via javascript....

Thank you for your hints / help!


Greetz
Sascha


Jodders

unread,
Nov 5, 2015, 10:12:21 AM11/5/15
to Jodders
Hello!

The short answer is: Yes, one action may return different 'types' of result content. The action method just should return an `Object`.

1) one response content is pure HTML i.e. text. In this case, just let your method return a String.

2) for the download file just return RawDownload instead.

However, I would rather prefer to do one of the following, especially since action method is returning `Object` - it is less visual :) :

+ to have 2 methods, where one returns String and the other returns Raw - of course, the common logic of both methods is encapsulated in some third method.
+ if you need this on more places ie. if more actions need this behavior, then just add your own custom Result, e.g. TextOrCsvResult. That is also valid :)

Hope this helps!


On Thursday, November 5, 2015 at 1:56:32 PM UTC+1, 

moh....@googlemail.com

unread,
Nov 6, 2015, 7:05:14 AM11/6/15
to Jodders
Hi,

i prefere the "two methods" , too :-)
Implementation works properly.

Idea with "TextOrCsvResult" sounds interesting!


Thank you for your answers!


Greetz
Sascha
Reply all
Reply to author
Forward
0 new messages