You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2017/04/01 08:08:14 UTC

[1/2] struts-site git commit: cleaned up hello-world-using-struts2 page

Repository: struts-site
Updated Branches:
  refs/heads/master fcd543656 -> 0f5845961


cleaned up hello-world-using-struts2 page


Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/d876161c
Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/d876161c
Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/d876161c

Branch: refs/heads/master
Commit: d876161c0db576c762a28a66b5acd5fdf486230e
Parents: 0afeb55
Author: Stefaan Dutry <st...@gmail.com>
Authored: Sat Apr 1 08:57:34 2017 +0200
Committer: Stefaan Dutry <st...@gmail.com>
Committed: Sat Apr 1 08:57:34 2017 +0200

----------------------------------------------------------------------
 .../hello-world-using-struts2.md                | 375 +++----------------
 1 file changed, 61 insertions(+), 314 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts-site/blob/d876161c/source/getting-started/hello-world-using-struts2.md
----------------------------------------------------------------------
diff --git a/source/getting-started/hello-world-using-struts2.md b/source/getting-started/hello-world-using-struts2.md
index e2fe694..359d59e 100644
--- a/source/getting-started/hello-world-using-struts2.md
+++ b/source/getting-started/hello-world-using-struts2.md
@@ -23,67 +23,46 @@ By creating these components, we are separating the work flow into three well-kn
 Let's look at an example model class, Action, server page, and mapping. If you like, fire up your Java IDE, and enter the code as we go.
 
 
-> This tutorial assumes you've completed the [How To Create A Struts 2 Web Application](#PAGE_14811860) tutorial and have a working basic Struts project. The example code for this tutorial, helloworld, is available for checkout from the 
-
->  Struts 2 GitHub repository at [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples). The example projects use Maven
-
->  to manage the artifact dependencies and to build the .war files.
-
-> 
+> This tutorial assumes you've completed the [How To Create A Struts 2 Web Application](#PAGE_14811860) tutorial and have a working basic Struts project. The example code for this tutorial, helloworld, is available for checkout from the Struts 2 GitHub repository at [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples). The example projects use Maven to manage the artifact dependencies and to build the .war files.
 
 __The Code__
 
 Let's modify either the basic_struts project to add a model class to store our message, a view that displays our message, an Action class to act as the controller, and a configuration that ties everything together.
 
-
-
-| The [Struts 2 user mailing list](http://struts.apache.org/mail.html)^[http://struts.apache.org/mail.html] is an excellent place to get help. If you are having a problem getting this application to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
-
-| 
+| The [Struts 2 user mailing list](http://struts.apache.org/mail.html) is an excellent place to get help. If you are having a problem getting this application to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
 
 __Step 1 - Create The Model Class MessageStore.java__
 
 If you're using the Basic_Struts2_Ant project to start with create the MessageStore class in the src folder and if you're using the Basic_Struts2_Mvn class create the MessageStore class in src/main/java. Be sure to note the package statement below.
 
-
-> 
-
-> 
-
 > Note that in the code shown below the JavaDoc comments are omitted. In the download example, JavaDoc comments are included.
 
-> 
-
 **MessageStore.java**
 
 
-~~~~~~~
+```java
 package org.apache.struts.helloworld.model;
 
 public class MessageStore {
 	
-	private String message;
-	
-	public MessageStore() {
-		
-		setMessage("Hello Struts User");
-	}
+    private String message;
 
-	public String getMessage() {
+    public MessageStore() {
+        setMessage("Hello Struts User");
+    }
 
-		return message;
-	}
+    public String getMessage() {
+        return message;
+    }
 
-	public void setMessage(String message) {
-
-		this.message = message;
-	}
+    public void setMessage(String message) {
+        this.message = message;
+    }
 
 }
+```
 
-~~~~~~~
-
-In the model class above note the use of public set and get methods to allow access to the private message String attribute. The Struts 2 framework requires that objects you want to expose to the view (HelloWorld.jsp) follow the [JavaBean-style conventions](http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions)^[http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions].
+In the model class above note the use of public set and get methods to allow access to the private message String attribute. The Struts 2 framework requires that objects you want to expose to the view (HelloWorld.jsp) follow the [JavaBean-style conventions](http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions).
 
 __Step 2 - Create The Action Class HelloWorldAction.java__
 
@@ -93,8 +72,7 @@ Note the package and import statements below.
 
 **HelloWorldAction.java**
 
-
-~~~~~~~
+```java
 package org.apache.struts.helloworld.action;
 
 import org.apache.struts.helloworld.model.MessageStore;
@@ -102,87 +80,39 @@ import com.opensymphony.xwork2.ActionSupport;
 
 public class HelloWorldAction extends ActionSupport {
 
-	private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = 1L;
 
-	private MessageStore messageStore;
-	
-	public String execute() throws Exception {
-		
-		messageStore = new MessageStore() ;
-		return SUCCESS;
-	}
+    private MessageStore messageStore;
 
-	public MessageStore getMessageStore() {
-		return messageStore;
-	}
+    public String execute() throws Exception {
+        messageStore = new MessageStore() ;
+        return SUCCESS;
+    }
 
-	public void setMessageStore(MessageStore messageStore) {
-		this.messageStore = messageStore;
-	}
-
-}
+    public MessageStore getMessageStore() {
+        return messageStore;
+    }
 
-~~~~~~~
+    public void setMessageStore(MessageStore messageStore) {
+        this.messageStore = messageStore;
+    }
 
-The Struts 2 framework will create an object of the 
-
-~~~~~~~
-HelloWorldAction
-~~~~~~~
-�class and call the execute method in response to a user's action (clicking on a hyperlink that sends a specific URL to the Servlet container).
-
-In this example, the execute method creates an object of class 
-
-~~~~~~~
-MessageStore
-~~~~~~~
-�and then returns the String constant 
-
-~~~~~~~
-SUCCESS
-~~~~~~~
-.
-
-Note also the public getter and setter methods for the private 
+}
+```
 
-~~~~~~~
-MessageStore
-~~~~~~~
-�object. Since we want to make the 
+The Struts 2 framework will create an object of the `HelloWorldAction`�class and call the execute method in response to a user's action (clicking on a hyperlink that sends a specific URL to the Servlet container).
 
-~~~~~~~
-MessageStore
-~~~~~~~
-�object available to the view page, 
+In this example, the execute method creates an object of class `MessageStore`�and then returns the String constant `SUCCESS`.
 
-~~~~~~~
-HelloWorld.jsp
-~~~~~~~
-�we need to follow the [JavaBean-style](http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions)^[http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions] of providing get and set methods.
+Note also the public getter and setter methods for the private `MessageStore`�object. Since we want to make the `MessageStore`�object available to the view page, `HelloWorld.jsp`,�we need to follow the [JavaBean-style](http://en.wikipedia.org/wiki/JavaBean\#JavaBean_conventions) of providing get and set methods.
 
 __Step 3 - Create The View HelloWorld.jsp__
 
-We need a server page to present the message that is stored in the model class 
-
-~~~~~~~
-MessageStore
-~~~~~~~
-. Create the below JSP in the 
-
-~~~~~~~
-WebContent
-~~~~~~~
-�folder (if using Ant) or in 
-
-~~~~~~~
-src/main/webapp
-~~~~~~~
- (if using Maven).
+We need a server page to present the message that is stored in the model class `MessageStore`. Create the below JSP in the `WebContent`�folder (if using Ant) or in `src/main/webapp` (if using Maven).
 
 **HelloWorld.jsp**
 
-
-~~~~~~~
+```html
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
@@ -196,96 +126,23 @@ src/main/webapp
     <h2><s:property value="messageStore.message" /></h2>
   </body>
 </html>
+```
 
-~~~~~~~
-
-The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will be preceded by an 
-
-~~~~~~~
-s
-~~~~~~~
-.
-
-The 
-
-~~~~~~~
-<s:property>
-~~~~~~~
-�tag displays the value returned by calling the method 
-
-~~~~~~~
-getMessageStore
-~~~~~~~
-�of the 
-
-~~~~~~~
-HelloWorldAction
-~~~~~~~
-�controller class. That method returns a 
-
-~~~~~~~
-MessageStore
-~~~~~~~
-�object. By adding the 
-
-~~~~~~~
-.message
-~~~~~~~
-�onto the messageStore part of the value attribute we are telling the Struts 2 framework to call the 
-
-~~~~~~~
-getMessage
-~~~~~~~
-�method of that 
+The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will be preceded by an `s`.
 
-~~~~~~~
-MessageStore
-~~~~~~~
-�object. The 
-
-~~~~~~~
-getMessage
-~~~~~~~
-�method of class 
-
-~~~~~~~
-MessageStore
-~~~~~~~
-�returns a String. It is that String that will be displayed by the 
-
-~~~~~~~
-<s:property>
-~~~~~~~
-�tag.
+The `<s:property>`�tag displays the value returned by calling the method `getMessageStore`�of the `HelloWorldAction`�controller class. That method returns a `MessageStore`�object. By adding the `.message`�onto the messageStore part of the value attribute we are telling the Struts 2 framework to call the `getMessage`�method of that `MessageStore`�object. The `getMessage`�method of class `MessageStore` returns a String. It is that String that will be displayed by the `<s:property>`�tag.
 
 We'll learn more about tags in the next tutorial. See the _Struts Tags_  for more information about tags.
 
 __Step 4 - Add The Struts Configuration In struts.xml__
 
-We need a mapping to tie the URL, the 
-
-~~~~~~~
-HelloWorldAction
-~~~~~~~
-�class (controller), and 
- the 
-
-~~~~~~~
-HelloWorld.jsp
-~~~~~~~
-�(the view) together. The mapping tells the Struts 2 framework which class will respond to the user's action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns.
+We need a mapping to tie the URL, the `HelloWorldAction`�class (controller), and the `HelloWorld.jsp`�(the view) together. The mapping tells the Struts 2 framework which class will respond to the user's action (the URL), which method of that class will be executed, and what view to render based on the String result that method returns.
 
-Edit the 
-
-~~~~~~~
-struts.xml
-~~~~~~~
- file (in the Mvn project that file is in the src/main/resources folder) to add the action mapping. Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:
+Edit the `struts.xml` file (in the Mvn project that file is in the src/main/resources folder) to add the action mapping. Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:
 
 **struts.xml**
 
-
-~~~~~~~
+```xml
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
@@ -305,52 +162,37 @@ struts.xml
     </action>
   </package>
 </struts>
-
-~~~~~~~
+```
 
 __Step 5 - Create The URL Action__
 
 In index.jsp (see WebContent folder for Ant project and src/main/webapp for Mvn project) let's add an Action URL the user can click on to tell the Struts 2 framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.
 
-First add the taglib directive at the top of the jsp 
-
-~~~~~~~
-<%@ taglib prefix="s" uri="/struts-tags" %>
-~~~~~~~
-. Next add this p tag 
-
-~~~~~~~
-<p><a href="<s:url action='hello'/>">Hello World</a></p>
-~~~~~~~
- after the h1 tag. Your new index.jsp should look like:
+First add the taglib directive at the top of the jsp `<%@ taglib prefix="s" uri="/struts-tags" %>`. Next add this p tag `<p><a href="<s:url action='hello'/>">Hello World</a></p>` after the h1 tag. Your new index.jsp should look like:
 
 **index.jsp**
 
-
-~~~~~~~
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
-    pageEncoding="ISO-8859-1"%>
+```html
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
-<title>Basic Struts 2 Application - Welcome</title>
-</head>
-<body>
-<h1>Welcome To Struts 2!</h1>
-<p><a href="<s:url action='hello'/>">Hello World</a></p>
-</body>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+    <title>Basic Struts 2 Application - Welcome</title>
+  </head>
+  <body>
+    <h1>Welcome To Struts 2!</h1>
+    <p><a href="<s:url action='hello'/>">Hello World</a></p>
+  </body>
 </html>
-
-
-~~~~~~~
+```
 
 The Struts url tag creates the URL with an action of hello. The hello action was mapped to the HelloWorldAction class and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute method of the HelloWorldAction class. After that method returns the String success, the view page HelloWorld.jsp will be rendered.
 
 __Step 6 - Build the WAR File and Run The Application__
 
-Execute mvn clean package to create the war file.
+Execute `mvn clean package` to create the war file.
 
 Copy the war file to your Servlet container. After your Servlet container successfully deploys the war file go to this URL [http://localhost:8080/helloworld/index.action](http://localhost:8080/helloworld/index.action) where you should see the following:
 
@@ -362,119 +204,24 @@ Click on the Hello World link and you should get the HelloWorld.jsp page:
 
 __Getting Help__
 
-The [Struts 2 user mailing list](http://struts.apache.org/mail.html)^[http://struts.apache.org/mail.html] is an excellent place to get help. If you are having a problem getting this application to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
+The [Struts 2 user mailing list](http://struts.apache.org/mail.html) is an excellent place to get help. If you are having a problem getting this application to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
 
 __How the Code Works__
 
 Your browser sends to the web server a request for the URL [http://localhost:8080/Hello_World_Struts2_Ant/hello.action](http://localhost:8080/Hello_World_Struts2_Ant/hello.action).
 
-1. The container receives from the web server a request for the resource 
+1. The container receives from the web server a request for the resource `hello.action`. According to the settings loaded from the _web.xml_ , the container finds that all requests are being routed to `org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter`, including the `*.action` requests. The StrutsPrepareAndExecuteFilter is the entry point into the framework.
 
-~~~~~~~
-hello.action
-~~~~~~~
-. According to the settings loaded from the _web.xml_ , the container finds that all requests are being routed to 
+2. The framework looks for an action mapping named "hello", and it finds that this mapping corresponds to the class "HelloWorldAction". The framework instantiates the Action and calls the Action's `execute` method.
 
-~~~~~~~
-org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
-~~~~~~~
-, including the 
+3. The `execute` method creates the MessageStore object and returns `SUCCESS`. The framework checks the action mapping to see what page to load if `SUCCESS` is returned. The framework tells the container to render as the response to the request, the resource `HelloWorld.jsp`.
 
-~~~~~~~
-*.action
-~~~~~~~
- requests. The StrutsPrepareAndExecuteFilter is the entry point into the framework.
-
-2. The framework looks for an action mapping named "hello", and it finds that this mapping corresponds to the class "HelloWorldAction". The framework instantiates the Action and calls the Action's 
-
-~~~~~~~
-execute
-~~~~~~~
- method.
-
-3. The 
-
-~~~~~~~
-execute
-~~~~~~~
- method creates the MessageStore object and returns 
-
-~~~~~~~
-SUCCESS
-~~~~~~~
-. The framework checks the action mapping to see what page to load if 
-
-~~~~~~~
-SUCCESS
-~~~~~~~
- is returned. The framework tells the container to render as the response to the request, the resource 
-
-~~~~~~~
-HelloWorld.jsp
-~~~~~~~
-.
-
-4. As the page 
-
-~~~~~~~
-HelloWorld.jsp
-~~~~~~~
- is being processed, the 
-
-~~~~~~~
-<s:property value="messageStore.message" />
-~~~~~~~
- tag calls the getter 
-
-~~~~~~~
-getMessageStore
-~~~~~~~
- of the 
-
-~~~~~~~
-HelloWorld
-~~~~~~~
- Action and then calls the 
-
-~~~~~~~
-getMessage
-~~~~~~~
- of the MessageStore object returned by 
-
-~~~~~~~
-getMessageStore
-~~~~~~~
-, and the tag merges into the response the value of the message attribute.
+4. As the page `HelloWorld.jsp` is being processed, the `<s:property value="messageStore.message" />` tag calls the getter `getMessageStore` of the `HelloWorld` Action and then calls the `getMessage` of the MessageStore object returned by `getMessageStore`, and the tag merges into the response the value of the message attribute.
 
 5. A pure HTML response is sent back to the browser.
 
 __What to Remember__
 
-The framework uses Actions to process HTML forms and other requests. The 
-
-~~~~~~~
-Action
-~~~~~~~
- class returns a result-name such as 
-
-~~~~~~~
-SUCCESS
-~~~~~~~
-, 
-
-~~~~~~~
-ERROR
-~~~~~~~
-, or 
-
-~~~~~~~
-INPUT
-~~~~~~~
-. Based on the mappings loaded from the 
-
-~~~~~~~
-struts.xml
-~~~~~~~
-, a given result-name may select a page (as in this example), another action, or some other web resource (image, PDF).
+The framework uses Actions to process HTML forms and other requests. The `Action` class returns a result-name such as `SUCCESS`, `ERROR` or `INPUT`. Based on the mappings loaded from the `struts.xml`, a given result-name may select a page (as in this example), another action, or some other web resource (image, PDF).
 
 When a server page is rendered, most often it will include dynamic data provided by the Action. To make it easy to display dynamic data, the framework provides a set of tags that can be used along with HTML markup to create a server page.


[2/2] struts-site git commit: Cleans up hello-world-using-struts2 page Close #5

Posted by lu...@apache.org.
Cleans up hello-world-using-struts2 page
Close #5


Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/0f584596
Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/0f584596
Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/0f584596

Branch: refs/heads/master
Commit: 0f584596189188853b3da4afba9ee8e25ff24ff9
Parents: fcd5436 d876161
Author: Lukasz Lenart <lu...@gmail.com>
Authored: Sat Apr 1 10:07:36 2017 +0200
Committer: Lukasz Lenart <lu...@gmail.com>
Committed: Sat Apr 1 10:07:36 2017 +0200

----------------------------------------------------------------------
 .../hello-world-using-struts2.md                | 375 +++----------------
 1 file changed, 61 insertions(+), 314 deletions(-)
----------------------------------------------------------------------