...
If you start Tomcat (or whichever J2EE container you are using) and type in http://localhost:8080/hello-world (assuming that your context path is "/
", ie. starting application from Eclipse) into your browser you should get this result:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
Hello world! |
This illustrates that the Convention plugin will find results even when no action exists and it is all based on the URL passed to Struts.
...
If you compile this class and place it into your application in the WEB-INF/classes, the Convention plugin will find the class and map the URL /hello-world to it. Next, we need to update our JSP to print out the message we setup in the action class. Here is the new JSP:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<html> <body> The message is ${message} </body> </html> |
...
If start up the application server and open up http://localhost:8080/hello-world in our browser, we should get this result:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
The message is Hello World! |
...
URL | Result | File that could match | Result Type |
---|---|---|---|
/hello | success | /WEB-INF/content/hello.jsp | Dispatcher |
/hello | success | /WEB-INF/content/hello-success.htm | Dispatcher |
/hello | success | /WEB-INF/content/hello.ftl | FreeMarker |
/hello-world | input | /WEB-INF/content/hello-world-input.vm | Velocity |
/test1/test2/hello | error | /WEB-INF/content/test/test2/hello-error.html | Dispatcher |
Multiple names
It is possible to define multiple names for the same result:
Code Block | ||
---|---|---|
| ||
@Action(results = {
@Result(name={"error", "input"}, location="input-form.jsp"),
@Result(name="success", location="success.jsp")
}) |
Such functionality was added in Struts 2.5
Chaining
If one action returns the name of another action in the same package, they will be chained together, if the first action doesn't have any result defined for that code. In the following example:
...
Using our example from above, the XWork package for our action would be:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
com.example.actions#/#conventionDefault |
...