How to return HTTP * and * error status code in Struts2

I have a controller that accepts accepts HTTP files and responds using JSON confirmation. If there are any problems with loading processing, I want to return an HTTP error status code (for example, 403 for invalid requests or 500 for a general processing error), but I also want to send a list of detailed JSON error messages. I know how to return 500 error (thanks to this post ), but I don't know how to return 500 code and still send content.

Here is a snippet of my code (which does not do what I want):

@Action(value = "upload", results = { 
  @Result(name = SUCCESS, type = "freemarker", location = "results.ftl", params = { "contentType", "text/plain" }), 
  @Result(name = ERROR, type = "freemarker", location = "error.ftl", params = { "contentType", "text/plain" }), 
  @Result(name = ERROR, type = "httpheader", params = { "status", "500" }) 
})

public String upload() {
  //do stuff
  if(CollectionUtils.isEmpty(getActionErrors()) {
    return SUCCESS;
  } else {
     return ERROR;
  }
}
+3
source share
3 answers

, Struts-JSON, , , , , :

@Result(name = ERROR, type="json", 
        params = {"root","errorResponse", "statusCode", "500"}
)   
+3

2/24/15: ty-danielson answer . JSON, , freemarker ( ).

freemarker : - , ServletResponse . FreksarkResult Struts , , (, GBIF)

/** 
 * Same as FreemarkerResult, but with added 'statusCode' parameter.  
 * (don't forget to register this result type in struts-config.xml)
 */
public class FreemarkerHttpResult extends FreemarkerResult {
  private int status;

  public int getStatusCode() {
    return status;
  }

  public void setStatusCode(int status) {
    this.status = status;
  }

  @Override
  protected void postTemplateProcess(Template template, TemplateModel data) throws IOException {
    super.postTemplateProcess(template, data);
    if (status >= 100 && status < 600) {
      HttpServletResponse response = ServletActionContext.getResponse();
      response.setStatus(status);
    }
  }
}

:

@Action(value = "myAction", results = { 
    @Result(name = SUCCESS, type = "freemarker", location = "results.ftl"), 
    @Result(name = ERROR, type = "freemarkerhttp", location = "error.ftl", params = { "statusCode", "500"})            
})
public String myAction() {
   //do stuff, then return SUCCESS or ERROR
}

, , "" struts2, , http, freemarker. , .

@Action(value = "upload", results = { 
@Result(name = SUCCESS, type = "freemarker", location = "results.ftl", params = { "contentType", "text/plain"}), 
@Result(name = ERROR, type = "freemarker", location = "error.ftl", params = { "contentType", "text/plain"})            
})
    public String upload() {
       try {
       //do stuff
       } Catch(SomeExceptionType ex) {
            addActionError("you did something bad");
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setStatus(400);
       }


    }
+2

Struts2, , , :

@Result(name = ERROR, type = "freemarker", location = "error.ftl",
    params = { "contentType", "text/plain", "status", "500" })
0

All Articles