Liferay - displaying the last page with error message when using @ResourceMapping

Woe to me, I am new to Liferay and am having difficulty with simple examples, tutorials and documentation.

I have a page with one button (for simplicity). The button asks the server to create a file and return it. It works without a hitch. When the server has a problem creating the file (for whatever reason), instead of returning an empty file, nothing happens. This will cause the page to be blank (where - as before, this was the last page I was enabled on with the file loaded).

How can I send an error message to the previous page (without using AJAX now) from this function? A blank error page is not what I'm looking for.

controller.java

@ResourceMapping
public void getFile(ResourceRequest request, ResourceResponse response) {
    String zipName = "myfile.zip";

    try {
        //Do something that might throw an error

        response.setProperty("Content-Disposition", "attachment; filename=" + zipName);
        response.setContentType("application/zip");
        //Write to output stream here
    } catch(Exception e) {
        myLogger.error("Error from throwable: " + e.getMessage());

        //Send error message to user on the last page without getting a blank screen
    }

      

View.jsp

${lastError}
<form method="post" action="<portlet:resourceURL/>">
  <input type="checkbox" name="Modify File" value="true" />
  <input type="submit" value="Get File" />
</form>

      

+3


source to share


2 answers


You get a blank page because nothing was answered.

When you create a ResourceRequest, your response is always ONLY what you put into the ResorceResponse, you never get a portal page as a response (not even part of it).

So, what you want to do, as you are trying now, is impossible. You must change your approach.

The simplest (not the best) solution would be

<form method="post" action="<portlet:resourceURL/>" target="_blank">
   <input type="checkbox" name="Modify File" value="true" />
   <input type="submit" value="Get File" />
</form>

      



note target="_blank"

.

Your controller ...

@ResourceMapping
public void getFile(ResourceRequest request, ResourceResponse response) {
  String zipName = "myfile.zip";

  try {   

    response.setProperty("Content-Disposition", "attachment; filename=" + zipName);
    response.setContentType("application/zip");

    //Write to output stream here

  } catch(Exception e) {
    myLogger.error("Error from throwable: " + e.getMessage());
    // write the error message to the response stream here.
    response.setContentType("text/plain");
    response.getWriter().print("last error");
  }
}

      

Thus, you get a file or an error in a new window leaving the portal page as it is.

Other parameters: AJAX or your response can be html with javascript that makes a request to your portal page with an additional parameter for the error, ...

+2


source


Do you really need a form for this? Can't you use the link with yours href

installed on <portlet:resourceURL/>

?

Also, why are you setting the content type to be plain text when uploading the zip file? I hope this is just for the error scenario.



I think you can separate your response generation logic based on try / catch result.

@ResourceMapping
public void getFile(ResourceRequest request, ResourceResponse response) {
    String zipName = "myfile.zip";

    try {   

        response.setProperty("Content-Disposition", "attachment; filename=" + zipName);
        response.setContentType("application/zip");

        //Write to output stream here

    } catch(Exception e) {
        myLogger.error("Error from throwable: " + e.getMessage());
        // write the error message to the response stream here.
        response.setContentType("text/plain");
    }
}

      

0


source







All Articles