[Maven 学习] Maven 整合 Struts2

[Maven 学习] Maven 整合 Struts2

前言

SSH 的基础学习已经告一段落,也好久没发文章了。想来 Maven 还没好好学习一下呢,先使用 Maven 整合一下 SSH,再将之前 SSH 的小 Demo 改为 Maven 项目。之后则是转为 SSM。


环境

操作系统 :MacOS High Sierra 10.13.2

Eclipse :4.7.2

Maven :3.5.2

Tomcat : 9.0.0

Struts2 : 2.5.13


流程

新建 Maven 项目

设置 JDK 版本,解决项目报错问题

导入 jar 包

在 web.xml 配置 struts2 的核心过滤器

配置 struts.xml

创建 Action 类

创建页面

运行

新建 Maven 项目

File —— new —— Maven —— MavenProject,点击 Next

勾选 Create a simple project(skip archetype selection),点击 Next

Group Id 一般填写公司域名倒过来,Artifact Id 填写项目名称,Packaging 打包方式选择 war,点击 Finish。

设置 JDK 版本,解决项目报错问题

右键项目名 —— Properties —— Project Facets,Dynamic Web Module 选择 3.1 或以上,Java 选择 1.8。点击 Apply。

此时,项目还缺少 web.xml 文件,右键项目名称 —— Java EE Tools —— Generate Deployment Descriptor tub,则会自动在 webapp 下创建 WEB-INF 文件夹,并在 WEB-INF 文件夹下创建好 web.xml。

右键项目名称 —— Maven —— Update Project… 此时报错信息就没有了。

配置 JDK 版本也可以在 pom.xml 之中配置,配置如下:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <!-- 设置 JDK 版本以及编码 -->
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>utf-8</encoding>
        </configuration>
    </plugin>
</plugins>
导入相关 jar 包

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>01_maven_struts2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>xx</name>
    <description>xxx</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <!-- 设置 JDK 版本以及编码 -->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.13</version>
        </dependency>
    </dependencies>
</project>
在 web.xml 配置 struts2 的核心过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>01_maven_struts2</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置 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>

注意:以上的配置是 Struts2.5 版本的配置,2.3 版本及其以下的版本的 filter-class 有点不同,为 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。具体以官方参考资料为准。

配置 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="customerAction_save" class="test.action.CustomerAction" method="save">
                <result name="success">/index.jsp</result>
            </action>
    </package>
</struts>
创建 Action 类

在 src/main/java 目录下创建包:test.action,在包下创建 CustomerAction.java。CustomerAction.java 代码如下:

package test.action;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {
    public String save() {
        System.out.println("测试");
        return SUCCESS;
    }
}
创建页面

在 webapp 下创建 index.jsp 页面,页面中输入些文本

运行

之后使用 Eclipse 中的 Tomcat 运行即可,在浏览器中输入:http://localhost:8080/项目名称/customerAction_save.action

可以在控制台中看到输出了:测试

Maven 整合 Struts2 到此结束


后记

在这之后,则会配置 Maven 中的 Tomcat 插件,以及进一步整合 Spring 和 Hibernate。

在实践中成长!

HochenChong

时间:2018-01-19

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值