Hide last authors
Ken McWilliams 7.1 1 Every Struts2 user should be using the conventions plugin. It reduces xml configuration to almost nothing, speeds up development time and improves readability. There is really no reason not to use it. For detailed information on the struts2-conventions-plugin see the [[struts2 site>>url:http://struts.apache.org/2.1.6/docs/convention-plugin.html]].
Ken McWilliams 1.1 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
Ken McWilliams 7.1 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).
Ken McWilliams 1.1 10
11 == **Basic Rules you must obey when using conventions** ==
12
Ken McWilliams 7.1 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.
Ken McWilliams 8.1 14 * Action Classes follow Java standard naming practices. That is the classes are written in camel case.
15 * The JSPs which act as the view for an action class will be all lower case with hyphens between words.
16 * The package structure after "action", "struts" or "struts2" become part of the name space for the action. Packages which follow become struts2 namespaces.
Ken McWilliams 7.1 17 * JSPs for the root namespace are found under ///WEB-INF/content// other name spaces are added as folders under content.
18 * JSPs under// /WEB-INF/content// automatically become actions even if there is no corresponding action class.
Ken McWilliams 1.1 19
20 == **Examples** ==
21
22 The following examples use the the following:
23
24 **Server**: http:~/~/localhost:8080/
25
26 **Context Root**: Example/
27
28 So all our pages will be prefixed with http:~/~/localhost:8080/Example/
29
30 === **Example 1 - Create an action to display a Form (No action class required)** ===
31
32 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.
33
34 Create a JSP at /WEB-INF/content/enter-name.jsp
35
36 <%@taglib prefix="s" uri="/struts-tags"%>
37 <%@page contentType="text/html" pageEncoding="UTF-8"%>
38 <!DOCTYPE html>
39 <html>
40 <head>
41 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
42 <title>Enter Name Example</title>
43 </head>
44 <body>
45 <h1>Enter Name Example</h1>
46 <s:form action="process-form">
47 Please enter your name: <s:textfield name="name"/>
48 <s:submit/>
49 </s:form>
50 </body>
51 </html>
52
53 To access this action: http:~/~/localhost:8080/Example/enter-name
54
Ken McWilliams 9.1 55 === **Example 2 - Process form and display results** ===
Ken McWilliams 2.1 56
57 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".
58
59 **Create a package called**: com.example.action (really the package structure before ".action" can be what ever you want)
60
61 **Create a file in this package called**: ProcessForm.java
62
63 **Add the following source**:
64
65 package com.example.action;
66 \\import com.opensymphony.xwork2.ActionSupport;
67 \\public class ProcessForm extends ActionSupport{
Ken McWilliams 9.1 68 private String name; ~/~/If this was public we could omit the getters/setters
Ken McWilliams 2.1 69 \\ @Override
70 public String execute(){
71 ~/~/processing goes here
72 ~/~/lets do something really simple like adding Hello to the name
73 name = "Hello, " + name + "!";
74 return SUCCESS;
75 }
76 \\ public String getName() {
77 return name;
78 }
79 \\ public void setName(String name) {
80 this.name = name;
81 }
82 }
Ken McWilliams 3.1 83
84 **Create the JSP for this action**: /WEB-INF/content/process-form **and add the following content**
85
86 <%@taglib prefix="s" uri="/struts-tags"%>
87 <%@page contentType="text/html" pageEncoding="UTF-8"%>
88 <!DOCTYPE html>
89 <html>
90 <head>
91 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
92 <title>Display Processing Example</title>
93 </head>
94 <body>
95 <h1>Display Processing Example</h1>
96 <s:property value="name"/>
97 </body>
98 </html>
Ken McWilliams 3.2 99
Ken McWilliams 7.1 100 **That's it! Run the project and test it with** __http:~/~/localhost:8080/Example/enter-name__
101
102 To input a value or supply a get request by using the url, such as: __http:~/~/localhost:8080/Example/process-form?name=Ken__
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 13.9 - Documentation