Learn Notes Eclipse Struts2 - Create a web Project

Article Summary
1. How to create a Struts 2 Web application
2. Configure struts 2 jar
3. Add Struts 2 Servlet Filter
4. Configure struts.xml, configure URL s, java classes, etc.
5. Build and run

English Literature Please click here~

1. Prerequisites

Struts 2 requires Servlet API version 2.4 or higher, JSP 2.0 or higher, and Java 7 or higher.

2. Create the first Maven program

To get started with Struts 2, we'll use Maven to create a Web application to manage artifact dependencies.
You can use the struts-examples Check out all sample applications from the Struts 2 GitHub repository.

  • 1. Click New-Select Maven Project:

  • 2. Select webapp

  • 3. Set Group Id, Artifact Id and other information.


  • 4. Once created, the project directory structure is as follows


  • 5. Configure pom.xml
    To run applications using maven, add jetty maven-plugin to your pom.xml

    <build>
      <finalName>basic-struts</finalName>
      <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 plug-ins will enable you to run the application mvn jetty:run
    At this point, the web project has been created.

3. Configuring index.jsp

Our next step is index.jsp, which adds a simple content to this Web application.Create a basic Struts 2 application for the title src/main/webapp under index.jsp - Welcome, and add Welcome Struts 2 to the H1 title of body!
Here we configure HelloWorld.

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

Run mvn jetty:run to run the application.
Browse in the browser: http://localhost:8080/basic-struts/index.jsp And you can see the results.

4. Add Struts 2 Jar file to class path

Now we know that we have a working Java Web application that can add at least the necessary Struts 2 framework Jar files to the class path of the Web application.Add the following dependent nodes to pom.xml:

  <dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.12</version>
    </dependency>
  </dependencies>

Of course, replace XXXX with the current version of Struts 2.Maven will get what struts2-core jar and other jar files need (pass dependencies) for struts2-core.

5. Add Logging

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.2</version>
    </dependency>
  </dependencies>

Using both log4j-core and log4j-api allows the latest version of Log4j2 without conflict with the version provided by the framework.log4j2.xml sets the configuration in the folder src/main/resources contains
log4j2.xml

<?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 that the log4j2 configuration above specifies the console as the log target.

6. Add Struts 2 Servlet Filter

web.xml

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

7. Configuring struts.xml

Struts 2 can use an XML configuration file or comment (or both) to specify relationships between URL s, Java classes, and view pages such as index.jsp.For our basic Struts 2 application, we will use the smallest XML configuration.Note that the file name is struts.xml, which should be in the src/main/resources folder (struts.xml must be on the root class path of the Web application).

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="basicstruts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>

This smallest Struts2 profile tells the framework that if the URL ends, index.action redirects the browser to index.jsp.

For more information on the struts.xml configuration file, see struts.xml.

8. Build and run applications

Run mvn jetty:run to run the Web application using jetty maven-plugin.

Looking at the console, you should see many debug messages telling you that the Struts 2 framework is being included in the basic-struts2 Web application.Open a Web browser and go to http://localhost:8080/basic-struts/index.action (Note that index.action is not at the end of the index.jsp URL).You will see and visit http://localhost:8080/basic-struts/index.jsp Same web page.

Struts 2 log message:

2017-07-17 18:12:13,506 WARN Worker-73 org.eclipse.m2e.core.internal.embedder.EclipseLogger - Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
2017-07-17 18:12:13,511 WARN Worker-73 org.eclipse.m2e.core.internal.embedder.EclipseLogger - Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

Keywords: Struts xml JSP Maven

Added by hiprakhar on Wed, 12 Jun 2019 20:39:06 +0300