0

My JSP page have entry like:

demo.jsp:

<s:form action="demo" theme="simple">
  Enter the location and filename for template to be saved (eg: C:\temp\a.xml)
  <s:textfield name="fileLoation" id="FileLoationID" />
  <s:submit value="Save" method="saveTemplate" />
</s:form>

struts.xml:

<action name="demo" class="com.test.action.DemoAction" >
    <result name="preview">/jsp/demo/preview.jsp</result>
    <result name="save">/jsp/demo/demo.jsp</result>
    <result name="success">/jsp/demo/demo.jsp</result>
    <result name="error">/loginError.jsp</result>
</action>

action class:

public String saveTemplate() {

        try {
            previewTemplate();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");            
            DOMSource source = (DOMSource) animalTemplateDetails.get("xmlStringDOMSource");         

            File file = new File(fileLoation);
            file.getParentFile().mkdirs();

            StreamResult result = new StreamResult(file);

            transformer.transform(source, result);

        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "save";
    }

Current process:

  1. I provided path like "C:/temp/abc.xml"
  2. Click save.
  3. Goes to action class method saveTemplate() and save the file. And returns "save"
  4. Will redirect to same page since <result name="save">/jsp/demo/demo.jsp</result>

My requirement:

  1. I provided path like "C:/temp/abc.xml"
  2. Click save.
  3. Without Page refresh, it need to hit action class method saveTemplate().

Actually i tried with ajax, but that button is not working..

This is what i did:

  1. Added <%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
  2. Added <sx:head />
  3. This part i am confused: i tried

(a) <s:submit value="Save" method="saveTemplate" theme="ajax" /> but here its showing error: "FreeMarker template error!Expression parameters.pushId is undefined on line 103, column 6 in template/ajax/submit.ftl."

(b) <sx:submit value="Save" method="saveTemplate" /> but here the button is not working. Nothing is happening.

Can anyone help?

abyin007
  • 361
  • 2
  • 4
  • 14

1 Answers1

0
<s:form action="demo" theme="simple" target="foo">
  Enter the location and filename for template to be saved (eg: C:\temp\a.xml)
  <s:textfield name="fileLoation" id="FileLoationID" />
  <s:submit value="save" method="saveTemplate" target="foo" />
</s:form>

<iframe style="display:none" name="foo">
   This is an hidden iframe targeted by the form to 
   avoid page refresh and new page / tab opening
</iframe>

And you can return NONE from the Action.

Btw you will have to notify the user if the operation has been successfully executed or not, then use iframe to receive an JSP snippet with the ok or ko message...

This is only ONE of the several ways to call Actions without refreshing the page, it's up to you...

But the code of your Action is executed on your SERVER, not on your CLIENT.

Then you need to change paradigm; use an Action that returns a Stream result, with content-disposition: attachment: this will ask the user where to download the file.

Read this too.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243