前言
本指南将帮助您在 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 服务器
- 打开 intellij idea。
- 进入
run > edit configurations...。 - 点击左上角 "+" 按钮,选择 "tomcat server > local"。
- 在 "application server" 区域,点击 "configure...",然后选择 tomcat 的安装目录(如
c:\tomcat9)。 - 设置端口(默认 http 端口为 8080),确保无冲突。
- 点击 "apply" 保存配置。
步骤 2: 创建 struts2 项目
- 在 idea 中,选择
file > new > project...。 - 选择 "maven" 作为项目类型,确保勾选 "create from archetype",然后选择 "maven-archetype-webapp"。
- 输入项目名称(如
struts2demo)和位置,点击 "finish"。 - 等待项目创建完成后,打开
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> - 保存
pom.xml,idea 会自动下载依赖(如果未自动下载,右键点击项目,选择 "maven > reload project")。
步骤 3: 配置 struts2 文件
- 在
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> - 在
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 视图
- 在
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; } } - 在
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: 运行项目并测试
- 在 idea 中,确保 tomcat 配置已关联项目:
- 进入
run > edit configurations...,选择之前配置的 tomcat。 - 在 "deployment" 标签页,点击 "+" 添加 "artifact",选择项目的 war 文件(如
struts2demo:war exploded)。 - 设置应用上下文路径(如
/struts2demo)。 - 点击 "apply" 保存。
- 进入
- 运行项目:点击工具栏的绿色运行按钮(或
run > run 'tomcat')。 - 测试功能:
- 打开浏览器,访问
http://localhost:8080/struts2demo/hello.action。 - 预期输出:页面显示 "hello, struts2!",表明动作执行成功。
- 打开浏览器,访问
- 检查日志:在 idea 的 "run" 控制台查看 tomcat 启动日志,确保无错误(如
info: server startup in [x] milliseconds)。
3. 常见问题与解决
- tomcat 启动失败:检查端口是否被占用(如 8080),在
run > edit configurations...中修改端口。 - 404 错误:确保
web.xml和struts.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项目内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论