2/24/15: ty-danielson answer . JSON, , freemarker ( ).
freemarker : - , ServletResponse . FreksarkResult Struts , , (, GBIF)
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 {
} Catch(SomeExceptionType ex) {
addActionError("you did something bad");
HttpServletResponse response = ServletActionContext.getResponse();
response.setStatus(400);
}
}