Reputation: 2205
I want an action to issue a 400 error HTTP response with an explanation of the problem in text/plain whenever a validation fails.
I can use org.apache.struts2.dispatcher.HttpHeaderResult to get the 400 error:
@Action(results = {
@Result(name = SUCCESS, type=ResultTypes.HTTP_HEADER,
params = { "status", "200" }),
@Result(name = INPUT, type=ResultTypes.HTTP_HEADER,
params = { "error", "400", "errorMessage", "${errorMessage}" })
})
@Override
public String execute() {
return INPUT;
}
public String getErrorMessage() {
return "There was an error";
}
The problem is that Tomcat "wraps" my response into an HTML page as follows:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1214
Date: Sun, 20 Mar 2011 00:43:55 GMT
Connection: close
<html><head><title>Apache Tomcat/6.0.29 - Error report</title><style><!--
H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;}
BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}
B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;}
P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}
A {color : black;} A.name {color : black;} HR {color : #525D76;}--></style> </head>
<body><h1>HTTP Status 400 - There was an error</h1><HR size="1" noshade="noshade">
<p><b>type</b> Status report</p><p><b>message</b> <u>There was an error</u></p>
<p><b>description</b> <u>The request sent by the client was syntactically incorrect (There was an error).</u></p>
<HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.29</h3></body></html>
While I would simply want something like:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=utf-8
Content-Length: 18
Date: Sun, 20 Mar 2011 00:43:55 GMT
Connection: close
There was an error
Thanks.
Upvotes: 0
Views: 2327
Reputation: 3212
You could use:
- your action code -
if (errorOccurs)
return ERROR;
struts.xml:
<action name="..." class="...">
<result type="json"> <!-- success -->
<param name="root">...</param>
</result>
<result name="error" type="json"> <!-- error -->
<param name="root">...</param>
<param name="statusCode">500</param>
</result>
</action>
Upvotes: 0
Reputation: 8677
specify the page to use for response code using the error-page configuration in web.xml
<error-page>
<error-code>400</error-code>
<location>/error/400.html</location>
</error-page>
Upvotes: 1