开发者

struts2+jquery+ajax call: return xml file from action

I would like some help with something I have to implement. Thank you very much in advance.

I am doing a webapplication with struts2. Right now I need to implement a way trough which the user could obtain a xml file but without reloading the web page.

From the jsp, with jquery, I have implemented an ajax call (to an action) like this:

$(function() {
[...]
$("#getXML").click(function() {
        [...]
        $.ajax( {
            type : "POST",
            url : "Servlets.action",
            data : "id="+$id+"&objects="+$objectsMap+"&relations="+$linesMap+"&inputs="+$inputs+"&option=2",
            dataType : "xml",
            success : function(data) {
                        //¿??
                      }
        });
        });   });

I have defined the action Servlets in the struts.xml configuration.

And the action right now looks like this:

public class Servlets extends ActionSupport implements SessionAware, ServletRequestAware{

private Appl开发者_运维问答icationDao applicationDao;
public void setServletRequest(HttpServletRequest request) {
    this.request = request;
}

public String execute() throws Exception {
    [...]
    if (request.getParameter("option").equals("1")) {
        [...]
    }
    if (request.getParameter("option").equals("2")) {
        [...]
        File xml = new File(id+".xml");
        XMLcreator.createXML(id,project,xml);
        //I want to return the File object "xml" to the user through the jquery
    }
    else {
        return ERROR;
    }
    return SUCCESS;
}  }

I would like someone to tell me how can I return the File object "xml" to the user that has ordered it.

Thank you very much,

Aleix


First, I would encourage you to never "return ERROR". A better approach is to just throw an exception and let the ExceptionMappingInterceptor map that to your error page.

As for your problem, you want your Ajax action to return some XML. One way to do that is to expose the File object via a getFile() method and then create a JSP to display it (as XML).

For example

public class Servlets extends ActionSupport ... {
  private File file;

  public String execute() throws Exception {
    [...]
    if (request.getParameter("option").equals("2")) {
      [...]
      file = new File(id + ".xml");
      XMLcreator.createXML(id, project, file);
    } else {
      throw new RuntimeException("Your error message here.");
    }
    return SUCCESS;
  }

  public File getFile() {
    return file;
  }
}

Then, your JSP would look similar to this (not sure what your specific XML looks like):

<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="text/xml;charset=UTF-8" language="java" %>
<file hidden="${action.file.hidden}">
  <name>${action.file.name}</name>
</file>

${action.file} refers to the getFile() method on your action.

Then just make sure that the JSP is mapped to the SUCCESS result.

Another way to handle this is to create a custom result type.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜