引入多个配置文件

在Struts2配置文件中使用include可引入多个配置文件。

项目实例

1.项目结构

2、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ray</groupId>
  <artifactId>struts2Test</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>struts2Test Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.16</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>struts2Test</finalName>
  </build>

</project>

3、web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0" metadata-complete="true">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 过滤所有请求交给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-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

4、HelloWorldAction.java

package com.ray.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Ray on 2018/3/26 0026.
 **/
public class HelloWorldAction extends ActionSupport {

    /**
    * @Author: Ray
    * @Date: 2018/3/26 0026
    * @Description: Struts2默认执行的方法
    * @Return: SUCCESS
    */
    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}

5、fourth-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="hello" extends="struts-default" namespace="/">
        <action name="hello" class="com.ray.action.HelloWorldAction">
            <result>/hello.jsp</result>
        </action>
    </package>
</struts>

6、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">

        <action name="helloWorld" class="com.ray.action.HelloWorldAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>

    <include file="second-struts.xml"/>
    <include file="third-struts.xml"/>
    <include file="fourth-struts.xml"/>
</struts>

7、hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>指定多个配置文件测试</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
    指定多个配置文件测试成功
</body>
</html>

8、页面效果

访问:http://localhost:8080/struts2Test/hello.action

ok!

第四篇——Struts2的引入多个配置文件的更多相关文章

  1. 深入理解javascript作用域系列第四篇——块作用域

    × 目录 [1]let [2]const [3]try 前面的话 尽管函数作用域是最常见的作用域单元,也是现行大多数javascript最普遍的设计方法,但其他类型的作用域单元也是存在的,并且通过使用 ...

  2. C++第四篇--重载_指针_引用

    C++第四篇--重载_指针_引用 1. 基础知识 重载:函数名相同,根据参数不同(类型.数量.顺序不同)调用同名函数 指针和引用:引用就是别名,引用时必须初始化,引用你定义的变量. int a; in ...

  3. 深入理解javascript作用域系列第四篇

    前面的话 尽管函数作用域是最常见的作用域单元,也是现行大多数javascript最普遍的设计方法,但其他类型的作用域单元也是存在的,并且通过使用其他类型的作用域单元甚至可以实现维护起来更加优秀.简洁的 ...

  4. 【block第四篇】实现

    -------------------------------------------欢迎查看block连载博客[专栏]--------------------------------------[b ...

  5. SpringBoot第二十四篇:应用监控之Admin

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11457867.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章(S ...

  6. VueRouter爬坑第四篇-命名路由、编程式导航

    VueRouter系列的文章示例编写时,项目是使用vue-cli脚手架搭建. 项目搭建的步骤和项目目录专门写了一篇文章:点击这里进行传送 后续VueRouter系列的文章的示例编写均基于该项目环境. ...

  7. Spring Cloud第十四篇 | Api网关Zuul

    ​ 本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...

  8. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  9. 第四篇 Entity Framework Plus 之 Batch Operations

    用 Entity Framework  进行 增,删,改.都是基于Model进行的,且Model都是有状态追踪的.这样Entity Framework才能正常增,删,改. 有时候,要根据某个字段,批量 ...

随机推荐

  1. Python类和实例方法和属性的动态绑定

    python中实例创建后可以给实例绑定任何属性和方法 class Student(object): pass 给实例绑定一个属性: s=Student() s.name='Michel' print ...

  2. Mysql分组查询group by语句详解

    (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from employee; ...

  3. 【Excel】输出固定长文本

    '******************************************************************************* ' 固定長形式テキストファイル書き出す ...

  4. vscode 同步配置

    按照插件完成配置之后,将token保存在本地,编辑配置文件:ctrl+3.sync高级选项.编辑本地扩展设置.将token粘贴进去即可,团队所有vscode均应该按照此配置来保持统一:

  5. Go学习笔记(四)Go自动化测试框架

    上篇Go学习笔记(三)Go语言学习 Go自动化测试非常简单,在结合VSCode的,让测试完全自动化 一 .编辑器下测试 1.测试代码以xxx_test.go方式命名 2.测试函数要以 func Tes ...

  6. Cookiecutter: 更好的项目模板工具:(3)高级用法

    本文中的Hook就是钩子,钩子就是Hook 使用生成前/生成后钩子脚本 你可以在项目生成之前和/或之后运行Python或Shell脚本. 像这样把它们放进Hook里: cookiecutter-som ...

  7. python简述

    python男神:龟叔 三,python基础初识. 1,运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确 ...

  8. PHP函数之dirname()

    1. 定义和用法 dirname()函数返回路径中的目录部分.即返回去掉文件名后的目录名. 2. 参数 dirname(path): 参数 path是一个包含有指向一个文件的全路径的字符串. 3. 举 ...

  9. RabbitMQ之路由键转发消息

    RabbitMQ学习 参考:https://www.jianshu.com/p/6b62a0ed2491 消息队列:目前流行的有 Kafka.RabbitMQ.ActiveMQ等 功能:为了解决消息的 ...

  10. 剑指offer——python【第54题】字符流中第一个不重复的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...