0

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.

jsalvata
  • 2,155
  • 15
  • 32

2 Answers2

1

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>
objects
  • 8,637
  • 4
  • 30
  • 38
  • I guess this is part of the solution, but: - As given, it will issue an HTML page - If replaced by a 400.txt, I will get a FIXED text plain message -- not the one issued by the action. – jsalvata Mar 20 '11 at 22:47
  • you can access the message from the action in the error page http://stackoverflow.com/questions/995248/how-to-get-the-message-in-a-custom-error-page-tomcat – objects Mar 20 '11 at 23:25
  • Make it a JSP page with a Content-Type of text/plain. – Steven Benitez Mar 21 '11 at 02:54
  • Thanks guys/gals -- this makes the complete answer. The trick is to use a JSP -- all attempts to get Struts2 to serve the error page have been fruitless. – jsalvata Mar 22 '11 at 20:17
0

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>
mauretto
  • 3,183
  • 3
  • 27
  • 28