Version 6.1 by Ken McWilliams on 2012/03/03 23:27

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. There is a very simple example, for detailed information on the struts2-conventions-plugin see the [[struts2 site>>url:http://struts.apache.org/2.1.6/docs/convention-plugin.html]].
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 ===
55
56 We need to create an action to process the name from the above example. We know that we must put this action in a package which contains the name "action", "struts" or "struts2".
57
58 **Create a package called**: com.example.action (really the package structure before ".action" can be what ever you want)
59
60 **Create a file in this package called**: ProcessForm.java
61
62 **Add the following source**:
63
64 package com.example.action;
65 \\import com.opensymphony.xwork2.ActionSupport;
66 \\public class ProcessForm extends ActionSupport{
67 private String name; ~/~/If this was public we could ommit the getters/setters
68 \\ @Override
69 public String execute(){
70 ~/~/processing goes here
71 ~/~/lets do something really simple like adding Hello to the name
72 name = "Hello, " + name + "!";
73 return SUCCESS;
74 }
75 \\ public String getName() {
76 return name;
77 }
78 \\ public void setName(String name) {
79 this.name = name;
80 }
81 }
82
83 **Create the JSP for this action**: /WEB-INF/content/process-form **and add the following content**
84
85 <%@taglib prefix="s" uri="/struts-tags"%>
86 <%@page contentType="text/html" pageEncoding="UTF-8"%>
87 <!DOCTYPE html>
88 <html>
89 <head>
90 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
91 <title>Display Processing Example</title>
92 </head>
93 <body>
94 <h1>Display Processing Example</h1>
95 <s:property value="name"/>
96 </body>
97 </html>
98
99 **That's it run the project and test it with** http:~/~/localhost:8080/Example/enter-name to input a value or supply a get request by using the url, such as: http:~/~/localhost:8080/Example/process-form?name=Ken