Struts2 configuration and a simple example

Struts2 configuration and a simple example

Note warehouse: https://github.com/nnngu/LearningNotes

brief introduction

This article mainly talks about how to use Struts2 in IntelliJ IDEA. The version of Struts2 used in this article is 2.5.14.1, which is a little different from other versions. It has been explained in the article.

Environmental Science

IntelliJ IDEA 2017.2.6

jdk1.8.0_101

Tomcat 8.0.38

Add dependency

The dependent jar packages are as follows:

commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
freemarker-2.3.26.jar
log4j-api-2.9.1.jar
ognl-3.1.15.jar
struts2-core-2.5.14.1.jar
javassist-3.20.0-GA.jar

Build a project using Maven, and add the following dependencies in pom.xml:

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.26-incubating</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.1.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.14.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>

Note: struts 2.5 is a little different from the previous version, which also requires xwork-core.jar. Struts 2.5 doesn't need it, because it incorporates the source code of xwork into struts 2-core. Before struts 2.5, the logging API was used, while struts 2.5 was replaced by the log4j API.

Configuring the core interceptor of the struts 2 framework in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--To configure struts2 Core interceptor of-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <!--Intercept all url-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Note: the full class name of Filter is: org.apache.struts 2.dispatcher.Filter.struts prepareandexecutefilter

Before struts 2.5: org. Apache. Struts 2. Dispatcher. Ng. Filter. Struts prepareandexecutefilter

Create a new business control class HelloWorldAction, which inherits from com.opensymphony.xwork2.ActionSupport. The content is as follows:

package com.nnngu.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("Executing Action");
        // Return to view SUCCESS, which is defined by the framework
        return SUCCESS;
    }
}

The created Action class needs to be configured in the core configuration file of struts 2

The core configuration file of struts 2 is 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>
    <package name="default" namespace="/" extends="struts-default">
        <!--Use when accessing localhost:8080/helloworld Visit-->
        <action name="helloworld" class="com.nnngu.action.HelloWorldAction">
            <!--Result set, i.e action In class SUCCESS View returned-->
            <result>
                /result.jsp
            </result>
        </action>
    </package>
</struts>

Create a new result.jsp file to display the view returned by action

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Action Result</title>
</head>
<body>
<h1>Congratulations! Successfully configured basic struts2 Environmental Science</h1>
<h2>Hello nnngu!</h2>
</body>
</html>

Finally, run the project and access the

Access in browser http://localhost:8080/helloworld

The content displayed is the content of result.jsp.

The console outputs the printed content of Action

At this point, the Struts2 configuration is complete.

Official documents of Struts2: http://struts.apache.org/getting-started/

Tags: Java Struts Apache xml log4j

Posted on Sun, 05 Apr 2020 06:39:25 -0700 by radhoo