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/02 09:13:03 UTC

[02/13] struts-site git commit: cleaned up processing forms page

cleaned up processing forms 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/1454da1d
Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/1454da1d
Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/1454da1d

Branch: refs/heads/master
Commit: 1454da1df367e271a3e8bb74393f34dbbe7d6deb
Parents: 4322cf8
Author: Stefaan Dutry <st...@gmail.com>
Authored: Sat Apr 1 18:02:07 2017 +0200
Committer: Stefaan Dutry <st...@gmail.com>
Committed: Sat Apr 1 18:02:07 2017 +0200

----------------------------------------------------------------------
 source/getting-started/processing-forms.md | 185 ++++++++++--------------
 1 file changed, 73 insertions(+), 112 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts-site/blob/1454da1d/source/getting-started/processing-forms.md
----------------------------------------------------------------------
diff --git a/source/getting-started/processing-forms.md b/source/getting-started/processing-forms.md
index 585f29e..909a8a9 100644
--- a/source/getting-started/processing-forms.md
+++ b/source/getting-started/processing-forms.md
@@ -4,7 +4,7 @@ title: Processing forms
 ---
 ## Processing Forms
 
-This tutorial assumes you've completed the _Coding Struts 2 Actons_  tutorial and have a working coding_actions project. The example code for this tutorial, form_processing, is available for checkout from the Struts 2 GitHub subversion repository: [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples).
+This tutorial assumes you've completed the [Coding Struts 2 Actons](coding-actions.html) tutorial and have a working coding_actions project. The example code for this tutorial, form_processing, is available for checkout from the Struts 2 GitHub subversion repository: [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples).
 
 > 
 
@@ -12,80 +12,63 @@ __Introduction__
 
 In this tutorial we'll explore using Struts 2 to do more involved processing of a form submission. We'll cover how to use a Java model class to store the form input and how to create the Struts 2 form to match up with that model class.
 
-The code provided in this tutorial may be added to the _Coding Struts 2 Actions_  example or you can download this complete example from Google Code - [http://code.google.com/p/struts2-examples/downloads/list](http://code.google.com/p/struts2-examples/downloads/list).
+The code provided in this tutorial may be added to the [Coding Struts 2 Actions](coding-actions.html) example or you can download this complete example from Google Code - [http://code.google.com/p/struts2-examples/downloads/list](http://code.google.com/p/struts2-examples/downloads/list).
 
-
-
-| 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 the tutorial example applications 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 the tutorial example applications 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.
 
 __Forms and A Java Model Class__
 
 For this tutorial let's say we need to provide a form that a user may submit to register for a prize drawing. Our business rules state the user must provide his/her first name, last name, email address, and age.
 
-To encapsulate this data, we'll use a simple Java class that follows the basic Java Bean specifications (public set/get methods for each instance field). If you're following along add this class to package org.apache.struts.register.model in the _Coding Struts 2 Actions_  example.
+To encapsulate this data, we'll use a simple Java class that follows the basic Java Bean specifications (public set/get methods for each instance field). If you're following along add this class to package org.apache.struts.register.model in the [Coding Struts 2 Actions](coding-actions.html) example.
 
 **Person.java**
 
-
-~~~~~~~
-public class Person
-{
+```java
+public class Person {
     private String firstName;
     private String lastName;
     private String email;
     private int age;
 
-    public String getFirstName()
-    {
+    public String getFirstName() {
         return firstName;
     }
 
-    public void setFirstName(String firstName)
-    {
+    public void setFirstName(String firstName) {
         this.firstName = firstName;
     }
 
-    public String getLastName()
-    {
+    public String getLastName() {
         return lastName;
     }
 
-    public void setLastName(String lastName)
-    {
+    public void setLastName(String lastName) {
         this.lastName = lastName;
     }
 
-    public String getEmail()
-    {
+    public String getEmail() {
         return email;
     }
 
-    public void setEmail(String email)
-    {
+    public void setEmail(String email) {
         this.email = email;
     }
 
-    public int getAge()
-    {
+    public int getAge() {
         return age;
     }
 
-    public void setAge( int age)
-    {
+    public void setAge(int age) {
         this.age = age;
     }
 
-
-    public String toString()
-    {
+    public String toString() {
         return "First Name: " + getFirstName() + " Last Name:  " + getLastName() + 
         " Email:      " + getEmail() + " Age:      " + getAge() ;
     }
 }
-
-~~~~~~~
+```
 
 Note a few points about the above class. There is a public set/get method for each instance field. The age attribute is of type integer. We've defined a public toString method that returns a String representing the state of the object. Since we haven't specified a constructor, Java will provide a default constructor that will set all instance fields to their null values.
 
@@ -95,36 +78,33 @@ To collect the above information we'll use a Struts 2 form. When creating this f
 
 **register.jsp**
 
-
-~~~~~~~
+```xml
 <?xml version="1.0" encoding="ISO-8859-1" ?>
 <%@ taglib prefix="s" uri="/struts-tags" %>
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
-    pageEncoding="ISO-8859-1"%>
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
-<title>Register</title>
-</head>
-<body>
-<h3>Register for a prize by completing this form.</h3>
-
-<s:form action="register">
-
- 	  <s:textfield name="personBean.firstName" label="First name" />
- 	  <s:textfield  name="personBean.lastName" label="Last name" />
- 	  <s:textfield name="personBean.email"  label ="Email"/>  
- 	  <s:textfield name="personBean.age"  label="Age"  />
- 	  
-   	  <s:submit/>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
+    <title>Register</title>
+  </head>
+  <body>
+    <h3>Register for a prize by completing this form.</h3>
+
+    <s:form action="register">
+
+      <s:textfield name="personBean.firstName" label="First name" />
+      <s:textfield  name="personBean.lastName" label="Last name" />
+      <s:textfield name="personBean.email"  label ="Email"/>  
+      <s:textfield name="personBean.age"  label="Age"  />
+ 
+      <s:submit/>
    	  
-</s:form>	
+    </s:form>	
  
-</body>
+  </body>
 </html>
-
-~~~~~~~
+```
 
 Since we are using Struts 2 tags, at the top of the page we need the Struts tag library declaration.
 
@@ -134,18 +114,17 @@ Note the four Struts 2 textfield tags. Each tag has a name value that includes a
 
 The complete name value, personBean.firstName, instructs Struts 2 to use the input value for that textfield as the argument to the personBean object's setFirstName method. So if the user types "Bruce" in the textfield that has the label "First name", the personBean's firstName instance field will have a value of "Bruce".
 
-Note we have a Struts 2 textfield for each instance field of the class Person. Remember that Person class's age attribute is of type integer. All form field input values are Strings. Struts 2 will automatically convert the String value ("25") the user entered for the age form field to 25 when calling the setAge method of object personBean.
+Note that we have a Struts 2 textfield for each instance field of the class Person. Remember that Person class's age attribute is of type integer. All form field input values are Strings. Struts 2 will automatically convert the String value ("25") the user entered for the age form field to 25 when calling the setAge method of object personBean.
 
 __Creating the Action Class To Handle the Form Submission__
 
-When the user clicks on the submit button of the above form, the action "register" and the form data will be sent to the Struts 2 framework. We need an Action class to process this form. If you recall from the tutorial _Coding Struts 2 Actions_  our Action class should extends the Struts 2 ActionSupport class.
+When the user clicks on the submit button of the above form, the action "register" and the form data will be sent to the Struts 2 framework. We need an Action class to process this form. If you recall from the tutorial [Coding Struts 2 Actions](coding-actions.html) our Action class should extend the Struts 2 ActionSupport class.
 
 Here is the Action class used for this example. Place it in package org.apache.struts.register.action.
 
 **Register.java Struts 2 Action Class**
 
-
-~~~~~~~
+```java
 package org.apache.struts.register.action;
 
 import org.apache.struts.register.model.Person;
@@ -154,35 +133,28 @@ import com.opensymphony.xwork2.ActionSupport;
 
 public class Register extends ActionSupport {
 	
-	private static final long serialVersionUID = 1L;
+    private static final long serialVersionUID = 1L;
 	
-	private Person personBean;
+    private Person personBean;
 	
 
-	@Override
-	public String execute() throws Exception {
-		
-		//call Service class to store personBean's state in database
-		
-		return SUCCESS;
-		
-	}
-	
-	public Person getPersonBean() {
-		
-		return personBean;
-		
-	}
-	
-	public void setPersonBean(Person person) {
-		
-		personBean = person;
-		
-	}
+    @Override
+    public String execute() throws Exception {
+        //call Service class to store personBean's state in database
 
-}
+        return SUCCESS;
+    }
 
-~~~~~~~
+    public Person getPersonBean() {
+        return personBean;
+    }
+
+    public void setPersonBean(Person person) {
+        personBean = person;
+    }
+
+}
+```
 
 In the Register class note that we've declared an attribute named personBean of type Person and there is a public get and set method for this object.
 
@@ -200,33 +172,27 @@ When SUCCESS is returned by the execute method we want to display a simple thank
 
 **thankyou.jsp**
 
-
-~~~~~~~
+```xml
 <?xml version="1.0" encoding="ISO-8859-1" ?>
 <%@ taglib prefix="s" uri="/struts-tags" %>
-<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
-    pageEncoding="ISO-8859-1"%>
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
-<title>Registration Successful</title>
-</head>
-<body>
-<h3>Thank you for registering for a prize.</h3>
-
-<p>Your registration information: <s:property value="personBean" /> </p>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
+    <title>Registration Successful</title>
+  </head>
+  <body>
+    <h3>Thank you for registering for a prize.</h3>
 
-<p><a href="<s:url action='index' />" >Return to home page</a>.</p>
+    <p>Your registration information: <s:property value="personBean" /> </p>
 
-</body>
+    <p><a href="<s:url action='index' />" >Return to home page</a>.</p>
+  </body>
 </html>
+```
 
-
-
-~~~~~~~
-
-If you don't recall how the Struts 2 property and url tags work consult the [Using Struts 2 Tags](#PAGE_14811875) tutorial.
+If you don't recall how the Struts 2 property and url tags work consult the [Using Struts 2 Tags](using-tags.html) tutorial.
 
 __Create action Node In struts.xml__
 
@@ -234,14 +200,11 @@ To specify the relationship between the form submission page, the Struts 2 Actio
 
 **action node for struts.xml**
 
-
-~~~~~~~
+```xml
 <action name="register" class="org.apache.struts.register.action.Register" method="execute">
   <result name="success">/thankyou.jsp</result>
 </action>
-
-
-~~~~~~~
+```
 
 The above action tells Struts 2 that when the register action is provided to call method execute of class Register. If that method returns result "success" return to the browser the thankyou.jsp.
 
@@ -253,11 +216,9 @@ So that the user can find the registration page, add this link to index.jsp
 
 **Link to register.jsp**
 
-
-~~~~~~~
+```html
 <p><a href="register.jsp">Please register</a> for our prize drawing.</p>
-
-~~~~~~~
+```
 
 __Run The Example__