updated page to current example application
Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/4ca26c06 Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/4ca26c06 Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/4ca26c06 Branch: refs/heads/master Commit: 4ca26c060faa4c1ccf5765837dd0062b6161eb2a Parents: 2423050 Author: Stefaan Dutry <stefaan.du...@gmail.com> Authored: Mon Apr 17 11:37:29 2017 +0200 Committer: Stefaan Dutry <stefaan.du...@gmail.com> Committed: Mon Apr 17 11:37:29 2017 +0200 ---------------------------------------------------------------------- .../att14974992_Basic_Struts2_Mvn.png | Bin 18234 -> 0 bytes .../attachments/basic_struts2_maven_jsp.png | Bin 0 -> 8387 bytes .../how-to-create-a-struts2-web-application.md | 151 +++++++++++-------- 3 files changed, 88 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-site/blob/4ca26c06/source/getting-started/attachments/att14974992_Basic_Struts2_Mvn.png ---------------------------------------------------------------------- diff --git a/source/getting-started/attachments/att14974992_Basic_Struts2_Mvn.png b/source/getting-started/attachments/att14974992_Basic_Struts2_Mvn.png deleted file mode 100644 index 4b2bdb3..0000000 Binary files a/source/getting-started/attachments/att14974992_Basic_Struts2_Mvn.png and /dev/null differ http://git-wip-us.apache.org/repos/asf/struts-site/blob/4ca26c06/source/getting-started/attachments/basic_struts2_maven_jsp.png ---------------------------------------------------------------------- diff --git a/source/getting-started/attachments/basic_struts2_maven_jsp.png b/source/getting-started/attachments/basic_struts2_maven_jsp.png new file mode 100644 index 0000000..48b6a79 Binary files /dev/null and b/source/getting-started/attachments/basic_struts2_maven_jsp.png differ http://git-wip-us.apache.org/repos/asf/struts-site/blob/4ca26c06/source/getting-started/how-to-create-a-struts2-web-application.md ---------------------------------------------------------------------- diff --git a/source/getting-started/how-to-create-a-struts2-web-application.md b/source/getting-started/how-to-create-a-struts2-web-application.md index d608b99..7e95cba 100644 --- a/source/getting-started/how-to-create-a-struts2-web-application.md +++ b/source/getting-started/how-to-create-a-struts2-web-application.md @@ -40,33 +40,77 @@ at [struts-examples](https://github.com/apache/struts-examples). > This tutorial assumes you know how to create a Java web application that > uses Maven to manage artifacts and build the web application archive (war) file. -__Step 1 - Create A Java Web Application__ +#### Step 1 - Create A Java Web Application In your Java IDE create a Java web application with a project name of basic_struts that follows the standard Maven -project folder structure. In your pom.xml include the following: +project folder structure. In your `pom.xml` include the following: **pom.xml build node** ```xml <build> - <finalName>basic_struts</finalName> + <finalName>basic-struts</finalName> </build> ``` -{:title="Final name"} -__Step 2 - Add index.jsp__ +###### to run the application using maven, add the jetty maven-plugin to your pom.xml + +**pom.xml jetty plugin** + +```xml +<build> + ... + <plugins> + <plugin> + <groupId>org.mortbay.jetty</groupId> + <artifactId>jetty-maven-plugin</artifactId> + <version>8.1.16.v20140903</version> + <configuration> + <webApp> + <contextPath>/${build.finalName}</contextPath> + </webApp> + <stopKey>CTRL+C</stopKey> + <stopPort>8999</stopPort> + <scanIntervalSeconds>10</scanIntervalSeconds> + <scanTargets> + <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget> + </scanTargets> + </configuration> + </plugin> + </plugins> +</build> +``` +The above plugin will enable you to run the application using `mvn jetty:run` + +#### Step 2 - Add index.jsp Our next step is to add a simple `index.jsp` to this web application. Create an `index.jsp` under `src/main/webapp` with a title of __Basic Struts 2 Application - Welcome__ and in the body add an h1 heading of __Welcome to Struts 2!__ -Run `mvn clean package` to create the war file. Copy the war file into your Servlet container so that it will deploy the war file. +**index.jsp** + +```jsp +<!DOCTYPE html> +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> +<html> + <head> + <meta charset="UTF-8"> + <title>Basic Struts 2 Application - Welcome</title> + </head> + <body> + <h1>Welcome To Struts 2!</h1> + </body> +</html> +``` -Start up your Servlet container and in a web browser go to [http://localhost:8080/Basic_Struts2_Mvn/index.jsp](http://localhost:8080/Basic_Struts2_Mvn/index.jsp). +Run `mvn jetty:run` to run the application. + +In a web browser go to [http://localhost:8080/basic-struts/index.jsp]([http://localhost:8080/basic-struts/index.jsp). You should see the following: - + -__Step 3 - Add Struts 2 Jar Files To Class Path__ +#### Step 3 - Add Struts 2 Jar Files To Class Path Now that we know we have a working Java web application, lets add the minimal required Struts 2 framework Jar files to our web application's class path. In `pom.xml` add the following dependency node: @@ -87,58 +131,45 @@ files struts2-core requires (transitive dependencies). <i class="glyphicon glyphicon-info-sign alert-info" aria-hidden="true"></i> Beginning with Struts version 2.2.3 you do not need to specify a separate dependency node for javassist. -__Step 4 - Add Logging__ +#### Step 4 - Add Logging -To see what's happening under the hood, the example application for this tutorial uses log4j. You'll need to add to pom.xml a dependency node for the log4j jar file: +To see what's happening under the hood, the example application for this tutorial uses log4j2. You'll need to add a dependency node for log4j2 to the pom: **pom.xml log4j dependency node** ```xml <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.14</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.8.2</version> </dependency> ``` -Setup a `log4j.xml` configuration in the `src/main/resources` folder. You can copy the one from the example application, -which contains the following +Setup a `log4j2.xml` configuration in the `src/main/resources` folder which contains the following -**log4j.xml** +**log4j2.xml** ```xml -<?xml version="1.0" encoding="UTF-8" ?> -<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> - -<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> - - <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> - <layout class="org.apache.log4j.PatternLayout"> - <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/> - </layout> - </appender> - - <!-- specify the logging level for loggers from other libraries --> - <logger name="com.opensymphony"> - <level value="DEBUG" /> - </logger> - - <logger name="org.apache.struts2"> - <level value="DEBUG" /> - </logger> - - <!-- for all other loggers log only info and above log messages --> - <root> - <priority value="INFO"/> - <appender-ref ref="STDOUT" /> - </root> - -</log4j:configuration> +<?xml version="1.0" encoding="UTF-8"?> +<Configuration> + <Appenders> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> + </Console> + </Appenders> + <Loggers> + <Logger name="com.opensymphony.xwork2" level="debug"/> + <Logger name="org.apache.struts2" level="debug"/> + <Root level="warn"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> ``` -Note the above log4j configuration specifies the console as the log target. +Note the above log4j2 configuration specifies the console as the log target. -__Step 5 - Add Struts 2 Servlet Filter__ +#### Step 5 - Add Struts 2 Servlet Filter To enable the Struts 2 framework to work with your web application you need to add a Servlet filter class and filter mapping to `web.xml`. Below is the filter and filter-mapping nodes you should add to `web.xml`. @@ -160,7 +191,7 @@ mapping to `web.xml`. Below is the filter and filter-mapping nodes you should ad For more information about configuring the deployment descriptor for Struts 2 see `web.xml`. Note the url-pattern node value is `/*` meaning the Struts 2 filter will be applied to all URLs for this web application. -__Step 6 - Create struts.xml__ +#### Step 6 - Create struts.xml Struts 2 can use either an XML configuration file or annotations (or both) to specify the relationship between a URL, a Java class, and a view page (such as `index.jsp`). For our basic Struts 2 application, we'll use a minimal xml @@ -180,11 +211,9 @@ must be on the web application's root class path). <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> - <action name="index"> <result>/index.jsp</result> </action> - </package> </struts> @@ -195,32 +224,28 @@ the browser to `index.jsp`. For more information about the struts.xml configuration file see `struts.xml`. -__Step 7 - Build and Run the Application__ +#### Step 7 - Build and Run the Application -With all of the above in place run mvn clean package to create the war file. Remove the previously created war file -and exploded web application folder from your Servlet container's webapps folder. Copy to your Servlet container's webapps -folder the new war you just created. +Run `mvn jetty:run` to run the web-application using the jetty maven-plugin. -Start up the Servlet container. View the console where you should see numerous debug messages that tell you -the Struts 2 framework is being included in the `Basic_Struts2_Mvn` web application. +View the console where you should see numerous debug messages that tell you the Struts 2 framework is being included in the `basic-struts2` web application. -Open a web browser and go to [http://localhost:8080/Basic_Struts2_Mvn/index.action](http://localhost:8080/Basic_Struts2_Mvn/index.action) +Open a web browser and go to [http://localhost:8080/basic-struts/index.action](http://localhost:8080/basic-struts/index.action) (note that's `index.action` not `index.jsp` at the end of the URL). You should see the same web page as when going to -[http://localhost:8080/Basic_Struts2_Mvn/index.jsp](http://localhost:8080/Basic_Struts2_Mvn/index.jsp). View the log +[http://localhost:8080/basic-struts/index.jsp](http://localhost:8080/basic-struts/index.jsp). View the log messages written to the console and you should find several that discuss `index.action` and `index.jsp`: **Struts 2 Log Messages** ``` -com.opensymphony.xwork2.DefaultActionProxy.debug:57 - Creating an DefaultActionProxy for namespace / and action name index ... -org.apache.struts2.dispatcher.ServletDispatcherResult.debug:57 - Forwarding to location /index.jsp +2017-04-17 11:16:01,084 DEBUG [qtp1723848804-22] xwork2.DefaultActionProxy (DefaultActionProxy.java:89) - Creating an DefaultActionProxy for namespace [/] and action name [index] +... +2017-04-17 11:16:01,172 DEBUG [qtp1723848804-22] result.ServletDispatcherResult (ServletDispatcherResult.java:131) - Forwarding to location: /index.jsp +... ``` -Note that the xwork2 artifact is one of the Jar files that is a transitive dependency for the struts2-core Jar file. -The xwork2 library is used heavily by the Struts 2 framework. - -__Getting Help__ +### Getting Help The [Struts 2 user mailing list](/mail.html) is an excellent place to get help. If you are having a problem getting this Basic Struts 2 application to work search the Struts 2 mailing list. If you don't find an answer to your problem,