Struts2 configuration and a simple example

Struts2 configuration and a simple example

Note warehouse:

brief introduction

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

Environmental Science

IntelliJ IDEA 2017.2.6


Tomcat 8.0.38

Add dependency

The dependent jar packages are as follows:


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


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=""

    <!--To configure struts2 Core interceptor of-->
        <!--Intercept all url-->


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 {
    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" ?>
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"

    <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-->

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

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

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:

Tags: Java Struts Apache xml log4j

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