当前位置: 代码网 > it编程>编程语言>Java > IDEA配置Tomcat运行Struts2项目之环境搭建与功能测试

IDEA配置Tomcat运行Struts2项目之环境搭建与功能测试

2026年09月23日 Java 我要评论
前言本指南将帮助您在 intellij idea 中配置 apache tomcat 来运行一个 struts2 项目,包括环境搭建和功能测试的完整步骤。struts2 是一个基于 mvc 模式的 j

前言

本指南将帮助您在 intellij idea 中配置 apache tomcat 来运行一个 struts2 项目,包括环境搭建和功能测试的完整步骤。struts2 是一个基于 mvc 模式的 java web 框架,tomcat 是常用的 web 服务器。整个过程分为环境搭建和功能测试两部分,确保您逐步操作。以下内容基于 intellij idea(建议 2022.3 或更高版本)、apache tomcat 9.x 和 jdk 8+。

1. 环境搭建

环境搭建包括安装必要软件、配置 tomcat 服务器、创建 struts2 项目并添加依赖。确保所有软件已下载并安装:

  • jdk:从 oracle 官网下载并安装(例如 jdk 17)。
  • apache tomcat:从 apache 官网下载 tomcat 9.x,解压到本地目录(如 c:\tomcat9)。
  • intellij idea:安装最新版,并确保已启用 java ee 插件(在设置中检查:file > settings > plugins,搜索 "java ee" 并启用)。

步骤 1: 在 idea 中配置 tomcat 服务器

  1. 打开 intellij idea。
  2. 进入 run > edit configurations...
  3. 点击左上角 "+" 按钮,选择 "tomcat server > local"。
  4. 在 "application server" 区域,点击 "configure...",然后选择 tomcat 的安装目录(如 c:\tomcat9)。
  5. 设置端口(默认 http 端口为 8080),确保无冲突。
  6. 点击 "apply" 保存配置。

步骤 2: 创建 struts2 项目

  1. 在 idea 中,选择 file > new > project...
  2. 选择 "maven" 作为项目类型,确保勾选 "create from archetype",然后选择 "maven-archetype-webapp"。
  3. 输入项目名称(如 struts2demo)和位置,点击 "finish"。
  4. 等待项目创建完成后,打开 pom.xml 文件,添加 struts2 核心依赖:
    <dependencies>
        <!-- struts2 core -->
        <dependency>
            <groupid>org.apache.struts</groupid>
            <artifactid>struts2-core</artifactid>
            <version>2.5.30</version> <!-- 使用最新稳定版 -->
        </dependency>
        <!-- servlet api -->
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>javax.servlet-api</artifactid>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
  5. 保存 pom.xml,idea 会自动下载依赖(如果未自动下载,右键点击项目,选择 "maven > reload project")。

步骤 3: 配置 struts2 文件

  1. src/main/webapp/web-inf 目录下,创建 web.xml 文件,配置 struts2 过滤器:
    <?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_4_0.xsd"
             version="4.0">
        <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>
  2. src/main/resources 目录下,创建 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" extends="struts-default">
            <action name="hello" class="com.example.helloaction">
                <result>/hello.jsp</result>
            </action>
        </package>
    </struts>

2. 功能测试

功能测试涉及创建一个简单的 struts2 动作,并验证项目是否能正常运行。

步骤 4: 创建动作类和 jsp 视图

  1. src/main/java 下创建包 com.example,然后新建 helloaction.java 类:
    package com.example;
    import com.opensymphony.xwork2.actionsupport;
    public class helloaction extends actionsupport {
        private string message;
        public string execute() {
            message = "hello, struts2!";
            return success;
        }
        public string getmessage() {
            return message;
        }
    }
    
  2. src/main/webapp 下创建 hello.jsp 文件,显示动作结果:
    <%@ page contenttype="text/html;charset=utf-8" language="java" %>
    <html>
    <head>
        <title>struts2 test</title>
    </head>
    <body>
        <h1>${message}</h1> <!-- 使用 el 表达式显示消息 -->
    </body>
    </html>
    

步骤 5: 运行项目并测试

  1. 在 idea 中,确保 tomcat 配置已关联项目:
    • 进入 run > edit configurations...,选择之前配置的 tomcat。
    • 在 "deployment" 标签页,点击 "+" 添加 "artifact",选择项目的 war 文件(如 struts2demo:war exploded)。
    • 设置应用上下文路径(如 /struts2demo)。
    • 点击 "apply" 保存。
  2. 运行项目:点击工具栏的绿色运行按钮(或 run > run 'tomcat')。
  3. 测试功能:
    • 打开浏览器,访问 http://localhost:8080/struts2demo/hello.action
    • 预期输出:页面显示 "hello, struts2!",表明动作执行成功。
  4. 检查日志:在 idea 的 "run" 控制台查看 tomcat 启动日志,确保无错误(如 info: server startup in [x] milliseconds)。

3. 常见问题与解决

  • tomcat 启动失败:检查端口是否被占用(如 8080),在 run > edit configurations... 中修改端口。
  • 404 错误:确保 web.xmlstruts.xml 路径正确,动作类包名与 struts.xml 匹配。
  • 依赖缺失:在 pom.xml 中确认依赖版本,运行 mvn clean install 更新。
  • 类找不到错误:检查 jdk 版本是否兼容(建议 jdk 8+),在 idea 中设置项目 sdk(file > project structure > project sdk)。

总结

通过以上步骤,您已成功在 idea 中配置 tomcat 运行 struts2 项目。环境搭建包括软件安装、服务器配置和项目创建;功能测试通过一个简单的动作验证了框架的正常工作。如果一切正常,您可以扩展项目,添加更多动作和业务逻辑。遇到问题时,参考 tomcat 日志或 idea 的错误提示进行调试。

到此这篇关于idea配置tomcat运行struts2项目之环境搭建与功能测试的文章就介绍到这了,更多相关idea配置tomcat运行struts2项目内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com