Version 1.1 by Ken McWilliams on 2012/03/03 23:02

Show last authors
1 I think every struts2 user should be using the conventions plugin. It reduces xml to almost nothing, speeds up development time and improves readablility. I really can't think of a down side.
2
3 Starting with an [[empty Struts2 project>>doc:Setting up a new Struts2 Project in Netbeans]].
4
5 All we need to do is add the struts2-conventions-plugin (using the same version as struts2) to your class path.
6
7 Using the conventions plugin is very easy!
8
9 It forms a relationship between your packages and the JSPs under WEB-INF (you can use other view technology but in this example we'll use JSPs).
10
11 == **Basic Rules you must obey when using conventions** ==
12
13 * Packages are scanned for "action", "struts" or "struts2" when such a package is found all Classes which implement ActionSupport or end with the name "Action" become actions.
14 * The package structure after "action", "struts" or "struts2" become part of the name space for the action.
15 * JSPs for the root namespace are found under /WEB-INF/content other name spaces are added as folders under content.
16 * JSPs under /WEB-INF/content automatically become actions even if there is no corresponding action class.
17 * Action Classes will follow Java standard naming, that is camel case and the JSPs will be all lower case with hyphens between words.
18
19 == **Examples** ==
20
21 The following examples use the the following:
22
23 **Server**: http:~/~/localhost:8080/
24
25 **Context Root**: Example/
26
27 So all our pages will be prefixed with http:~/~/localhost:8080/Example/
28
29 === **Example 1 - Create an action to display a Form (No action class required)** ===
30
31 The following create an action to display a JSP which allows the user to enter their name, in the next example we allow them to submit it and process the results.
32
33 Create a JSP at /WEB-INF/content/enter-name.jsp
34
35 <%@taglib prefix="s" uri="/struts-tags"%>
36 <%@page contentType="text/html" pageEncoding="UTF-8"%>
37 <!DOCTYPE html>
38 <html>
39 <head>
40 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
41 <title>Enter Name Example</title>
42 </head>
43 <body>
44 <h1>Enter Name Example</h1>
45 <s:form action="process-form">
46 Please enter your name: <s:textfield name="name"/>
47 <s:submit/>
48 </s:form>
49 </body>
50 </html>
51
52 To access this action: http:~/~/localhost:8080/Example/enter-name
53
54 === Example 2 - Process form and display results ===