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 2022/09/30 09:58:03 UTC

[struts-examples] branch master updated: Extends example with using multiple actions per form

This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts-examples.git


The following commit(s) were added to refs/heads/master by this push:
     new 629cca9  Extends example with using multiple actions per form
629cca9 is described below

commit 629cca9013a565800b4e052df92991ccc8ddf532
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Fri Sep 30 11:57:41 2022 +0200

    Extends example with using multiple actions per form
---
 .../apache/struts/register/action/Register.java    | 20 +++++++-----------
 .../org/apache/struts/register/model/Person.java   | 12 ++++-------
 form-processing/src/main/resources/log4j2.xml      |  4 ++--
 form-processing/src/main/resources/struts.xml      | 19 ++++++++++++-----
 .../src/main/webapp/{ => WEB-INF}/HelloWorld.jsp   |  0
 .../src/main/webapp/{ => WEB-INF}/index.jsp        |  2 +-
 .../src/main/webapp/WEB-INF/register.jsp           | 21 +++++++++++++++++++
 .../src/main/webapp/{ => WEB-INF}/thankyou.jsp     |  0
 form-processing/src/main/webapp/WEB-INF/web.xml    | 24 ++++++++++++++--------
 form-processing/src/main/webapp/register.jsp       | 20 ------------------
 10 files changed, 64 insertions(+), 58 deletions(-)

diff --git a/form-processing/src/main/java/org/apache/struts/register/action/Register.java b/form-processing/src/main/java/org/apache/struts/register/action/Register.java
index 2a2a3d6..98eb692 100644
--- a/form-processing/src/main/java/org/apache/struts/register/action/Register.java
+++ b/form-processing/src/main/java/org/apache/struts/register/action/Register.java
@@ -3,29 +3,23 @@ package org.apache.struts.register.action;
 import com.opensymphony.xwork2.ActionSupport;
 import org.apache.struts.register.model.Person;
 
-/**
- * Acts as a controller to handle actions
- * related to registering a user.
- * @author bruce phillips
- *
- */
 public class Register extends ActionSupport {
-    
-    private static final long serialVersionUID = 1L;
-    
-    private Person personBean;
 
+    private Person personBean;
 
     public String execute() throws Exception {
         //call Service class to store personBean's state in database
-        
         return SUCCESS;
     }
-    
+
+    public String cancel() throws Exception {
+        return SUCCESS;
+    }
+
     public Person getPersonBean() {
         return personBean;
     }
-    
+
     public void setPersonBean(Person person) {
         personBean = person;
     }
diff --git a/form-processing/src/main/java/org/apache/struts/register/model/Person.java b/form-processing/src/main/java/org/apache/struts/register/model/Person.java
index 5393460..ade6961 100644
--- a/form-processing/src/main/java/org/apache/struts/register/model/Person.java
+++ b/form-processing/src/main/java/org/apache/struts/register/model/Person.java
@@ -1,11 +1,5 @@
 package org.apache.struts.register.model;
 
-
-/**
- * Models a Person who registers.
- * @author bruce phillips
- *
- */
 public class Person {
     private String firstName;
     private String lastName;
@@ -45,7 +39,9 @@ public class Person {
     }
 
     public String toString() {
-        return "First Name: " + getFirstName() + " Last Name:  " + getLastName() + 
-        " Email:      " + getEmail() + " Age:      " + getAge() ;
+        return "First Name: " + getFirstName() +
+                " Last Name: " + getLastName() +
+                " Email: " + getEmail() +
+                " Age: " + getAge();
     }
 }
diff --git a/form-processing/src/main/resources/log4j2.xml b/form-processing/src/main/resources/log4j2.xml
index ba1d132..8d07e5f 100644
--- a/form-processing/src/main/resources/log4j2.xml
+++ b/form-processing/src/main/resources/log4j2.xml
@@ -7,9 +7,9 @@
     </Appenders>
     <Loggers>
         <Logger name="com.opensymphony.xwork2" level="info"/>
-        <Logger name="org.apache.struts2" level="debug"/>
+        <Logger name="org.apache.struts2" level="info"/>
         <Root level="warn">
             <AppenderRef ref="STDOUT"/>
         </Root>
     </Loggers>
-</Configuration>
\ No newline at end of file
+</Configuration>
diff --git a/form-processing/src/main/resources/struts.xml b/form-processing/src/main/resources/struts.xml
index 528774d..2addd1a 100644
--- a/form-processing/src/main/resources/struts.xml
+++ b/form-processing/src/main/resources/struts.xml
@@ -4,23 +4,32 @@
         "http://struts.apache.org/dtds/struts-2.5.dtd">
 <struts>
     <constant name="struts.devMode" value="true" />
+    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
+    <constant name="struts.mapper.action.prefix.enabled" value="true"/>
 
     <package name="basicstruts2" extends="struts-default">
-        <!-- If no class attribute is specified the framework will assume success and 
+        <default-action-ref name="index"/>
+        <!-- If no class attribute is specified the framework will assume success and
         render the result index.jsp -->
         <!-- If no name value for the result node is specified the success value is the default -->
         <action name="index">
-            <result>/index.jsp</result>
+            <result>/WEB-INF/index.jsp</result>
         </action>
 
         <!-- If the URL is hello.action then call the execute method of class HelloWorldAction.
         If the result returned by the execute method is success render the HelloWorld.jsp -->
         <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
-            <result name="success">/HelloWorld.jsp</result>
+            <result name="success">/WEB-INF/HelloWorld.jsp</result>
         </action>
 
-        <action name="register" class="org.apache.struts.register.action.Register" method="execute">
-            <result name="success">/thankyou.jsp</result>
+        <action name="register" class="org.apache.struts.register.action.Register" method="input">
+            <result name="input">/WEB-INF/register.jsp</result>
+        </action>
+        <action name="register-cancel" class="org.apache.struts.register.action.Register" method="cancel">
+            <result type="redirectAction">index</result>
+        </action>
+        <action name="register-submit" class="org.apache.struts.register.action.Register" method="execute">
+            <result>/WEB-INF/thankyou.jsp</result>
         </action>
     </package>
 </struts>
diff --git a/form-processing/src/main/webapp/HelloWorld.jsp b/form-processing/src/main/webapp/WEB-INF/HelloWorld.jsp
similarity index 100%
rename from form-processing/src/main/webapp/HelloWorld.jsp
rename to form-processing/src/main/webapp/WEB-INF/HelloWorld.jsp
diff --git a/form-processing/src/main/webapp/index.jsp b/form-processing/src/main/webapp/WEB-INF/index.jsp
similarity index 91%
rename from form-processing/src/main/webapp/index.jsp
rename to form-processing/src/main/webapp/WEB-INF/index.jsp
index 18a3bb5..135e664 100644
--- a/form-processing/src/main/webapp/index.jsp
+++ b/form-processing/src/main/webapp/WEB-INF/index.jsp
@@ -20,6 +20,6 @@
       <s:submit value="Submit" />
     </s:form>
 
-    <p><a href="register.jsp">Please register</a> for our prize drawing.</p>
+    <p><a href="register">Please register</a> for our prize drawing.</p>
   </body>
 </html>
diff --git a/form-processing/src/main/webapp/WEB-INF/register.jsp b/form-processing/src/main/webapp/WEB-INF/register.jsp
new file mode 100644
index 0000000..7750abb
--- /dev/null
+++ b/form-processing/src/main/webapp/WEB-INF/register.jsp
@@ -0,0 +1,21 @@
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
+<!DOCTYPE html>
+<html>
+<head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+    <title>Register</title>
+</head>
+<body>
+<h3>Register for a prize by completing this form.</h3>
+
+<s:form action="register-submit">
+    <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:submit action="register-cancel" value="Cancel" />
+</s:form>
+</body>
+</html>
diff --git a/form-processing/src/main/webapp/thankyou.jsp b/form-processing/src/main/webapp/WEB-INF/thankyou.jsp
similarity index 100%
rename from form-processing/src/main/webapp/thankyou.jsp
rename to form-processing/src/main/webapp/WEB-INF/thankyou.jsp
diff --git a/form-processing/src/main/webapp/WEB-INF/web.xml b/form-processing/src/main/webapp/WEB-INF/web.xml
index 1ab7137..90088a1 100644
--- a/form-processing/src/main/webapp/WEB-INF/web.xml
+++ b/form-processing/src/main/webapp/WEB-INF/web.xml
@@ -1,19 +1,25 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-<display-name>Form_Processing_Struts2_Mvn</display-name>
-  <welcome-file-list>
-    <welcome-file>index.jsp</welcome-file>
-  </welcome-file-list>
-  
-  					 
+<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+    <display-name>Form_Processing_Struts2_Mvn</display-name>
+    <welcome-file-list>
+        <welcome-file>index.jsp</welcome-file>
+    </welcome-file-list>
+
+
     <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
     </filter>
 
-     <filter-mapping>
+    <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
-    
+
+    <welcome-file-list>
+        <welcome-file>/index</welcome-file>
+    </welcome-file-list>
+
 </web-app>
diff --git a/form-processing/src/main/webapp/register.jsp b/form-processing/src/main/webapp/register.jsp
deleted file mode 100644
index e6e697f..0000000
--- a/form-processing/src/main/webapp/register.jsp
+++ /dev/null
@@ -1,20 +0,0 @@
-<%@ taglib prefix="s" uri="/struts-tags" %>
-<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-    <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>	
-  </body>
-</html>