---
title: "Struts2的配置和一个简单的例子"
author: "nnngu"
date: "2018-02-10T13:08:34Z"
tags: []
categories: ["编程语言"]
slug: "struts2"
draft: false
lastmod: "2018-02-10T13:08:34Z"
id: 2410743
url: "https://nnngu.iteye.com/blog/2410743"
---

笔记仓库：[https://github.com/nnngu/LearningNotes](https://github.com/nnngu/LearningNotes)

* * *

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E7%AE%80%E4%BB%8B)简介

这篇文章主要讲如何在 IntelliJ IDEA 中使用 Struts2，文章使用的 Struts2 的版本是2.5.14.1，与其他的版本有一点差别，在文章里已经说明。

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E7%8E%AF%E5%A2%83)环境

IntelliJ IDEA 2017.2.6

jdk1.8.0\_101

Tomcat 8.0.38

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96)添加依赖

### [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E4%BE%9D%E8%B5%96%E7%9A%84-jar-%E5%8C%85%E6%9C%89%E5%A6%82%E4%B8%8B%E5%87%A0%E4%B8%AA)依赖的 jar 包有如下几个：

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

### [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E4%BD%BF%E7%94%A8-maven-%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE%E5%B9%B6%E4%B8%94%E5%9C%A8-pomxml-%E6%B7%BB%E5%8A%A0%E5%A6%82%E4%B8%8B%E4%BE%9D%E8%B5%96)使用 Maven 构建一个项目，并且在 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>
```

注意：Struts2.5 与之前的版本有点不同，之前的版本还需要xwork-core.jar。Struts2.5不需要它，因为Struts2.5把xwork的源码合并到了struts2-core中。Struts2.5之前使用logging API，而 Struts2.5 用 log4j API 取代。

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E5%9C%A8webxml%E4%B8%AD%E9%85%8D%E7%BD%AEstruts2%E6%A1%86%E6%9E%B6%E7%9A%84%E6%A0%B8%E5%BF%83%E6%8B%A6%E6%88%AA%E5%99%A8strutsprepareandexexutefilter)在web.xml中配置Struts2框架的核心拦截器StrutsPrepareAndExexuteFilter

[![](https://camo.githubusercontent.com/e963b505d635084b64b351e32bf839d3e3ed6b55/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233373338343037392e6a7067)](https://camo.githubusercontent.com/e963b505d635084b64b351e32bf839d3e3ed6b55/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233373338343037392e6a7067)

```
<?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">

    <!--配置struts2的核心拦截器-->
    <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-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
```

注意：Filter的完整类名是：org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

Struts2.5 以前是：org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA%E4%B8%9A%E5%8A%A1%E6%8E%A7%E5%88%B6%E7%B1%BB-helloworldaction-%E7%BB%A7%E6%89%BF%E8%87%AAcomopensymphonyxwork2actionsupport---%E5%86%85%E5%AE%B9%E5%A6%82%E4%B8%8B)新建一个业务控制类 HelloWorldAction ，继承自com.opensymphony.xwork2.ActionSupport ， 内容如下：

[![](https://camo.githubusercontent.com/ee91357f0a721ec278b92e9807fa6c6050d947cd/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233373737313030372e6a7067)](https://camo.githubusercontent.com/ee91357f0a721ec278b92e9807fa6c6050d947cd/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233373737313030372e6a7067)

```
package com.nnngu.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("正在执行的Action");
        // 返回视图 SUCCESS，这是框架定义的
        return SUCCESS;
    }
}
```

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E5%88%9B%E5%BB%BA%E5%A5%BD%E7%9A%84-action-%E7%B1%BB%E9%9C%80%E8%A6%81%E5%9C%A8-struts2-%E7%9A%84%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E8%BF%9B%E8%A1%8C%E9%85%8D%E7%BD%AE)创建好的 Action 类需要在 Struts2 的核心配置文件中进行配置

Struts2 的核心配置文件为`struts.xml`

[![](https://camo.githubusercontent.com/81da13e4843de95f0f0e6773be6055545baeae45/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383033393839382e6a7067)](https://camo.githubusercontent.com/81da13e4843de95f0f0e6773be6055545baeae45/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383033393839382e6a7067)

```
<?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">
        <!--访问时使用 localhost:8080/helloworld 访问-->
        <action name="helloworld" class="com.nnngu.action.HelloWorldAction">
            <!--结果集，即action类中 SUCCESS 返回的视图-->
            <result>
                /result.jsp
            </result>
        </action>
    </package>
</struts>
```

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA-resultjsp-%E6%96%87%E4%BB%B6%E7%94%A8%E6%9D%A5%E6%98%BE%E7%A4%BA-action-%E8%BF%94%E5%9B%9E%E7%9A%84%E8%A7%86%E5%9B%BE)新建一个 result.jsp 文件，用来显示 action 返回的视图

[![](https://camo.githubusercontent.com/f920284e7b490e01051619ab0eb17fb45237ffb3/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383237303332362e6a7067)](https://camo.githubusercontent.com/f920284e7b490e01051619ab0eb17fb45237ffb3/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383237303332362e6a7067)

```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Action Result</title>
</head>
<body>
<h1>恭喜！成功配置好基本的struts2 环境</h1>
<h2>Hello nnngu！</h2>
</body>
</html>
```

## [](https://github.com/nnngu/LearningNotes/blob/master/Struts2/01%20Struts2%E7%9A%84%E9%85%8D%E7%BD%AE%E5%92%8C%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E4%BE%8B%E5%AD%90.md#%E6%9C%80%E5%90%8E%E8%BF%90%E8%A1%8C%E9%A1%B9%E7%9B%AE%E5%9C%A8%E6%B5%8F%E8%A7%88%E5%99%A8%E8%AE%BF%E9%97%AE)最后运行项目，在浏览器访问

在浏览器访问 [http://localhost:8080/helloworld](http://localhost:8080/helloworld)

展现出来的内容是 result.jsp 的内容。

[![](https://camo.githubusercontent.com/e1089e1229f0f561ac6b6c62870891a897279e19/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383436333539322e6a7067)](https://camo.githubusercontent.com/e1089e1229f0f561ac6b6c62870891a897279e19/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383436333539322e6a7067)

控制台输出 Action 的打印内容

[![](https://camo.githubusercontent.com/71dc7826ebfd67f5318c648ecf3e9d1d7b840799/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383631373138312e6a7067)](https://camo.githubusercontent.com/71dc7826ebfd67f5318c648ecf3e9d1d7b840799/68747470733a2f2f7777772e6769746875622e636f6d2f6e6e6e67752f4669677572654265642f7261772f6d61737465722f323031382f322f31302f313531383233383631373138312e6a7067)

到此，Struts2 就配置完成了。

Struts2 官方文档：[http://struts.apache.org/getting-started/](http://struts.apache.org/getting-started/)