eclipse 编写struct2的 demo

本文详细介绍了如何使用Eclipse、Apache Tomcat等工具搭建Struts2框架环境,并具体讲解了配置过程、创建Web项目、配置Struts2所需库文件、修改web.xml文件、定义表单JSP页面及实现登录功能的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、eclipse,mysql,apache tomcat的安装和配置见前面几篇文章

建一个Dynamic Web Project,取名叫OATest

2、struts2配置

下载struts2需要的的完整包,将其中的以下文件复制到eclipse的对应web工程的WebContent/WEB-INF/lib的lib目录下:

            1)xwork-core-
            2)struts2-core-
            3)ognl-
            4)freemarker-
            5)commons-io-
            6)commons-fileupload-
            7)javassist-
            8)commons-lang3-
3、struts2修改web.xml

     修改web.xml增加过滤器filter,最终配置文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="GBK"></constant>
	<package name="default" extends="struts-default">
		<action name="loginAction" class="com.oa.action.LoginAction">
			<result name="success">welcome.jsp</result>
			<result name="error">err.jsp</result>
		</action>
	</package>
</struts>


 
4、定义表单的JSP页面

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<c:form action="loginAction">
		<c:textarea name="username" key="username"></c:textarea>
		<c:textarea name="password" key="password"></c:textarea>
		<input type="submit" value="提交"/>
	</c:form>
</body>
</html>

5、定义拦截类com.oa.action.LoginAction

注意,包为com.oa.action

package com.oa.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;

	public String execute() {
		if (username.equals("a") && password.equals("a"))
			return SUCCESS;
		return ERROR;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}











                
<think>嗯,用户想了解如何在Eclipse中配置Struts2。首先,我得回想一下Struts2的基本配置步骤。首先需要安装Eclipse,然后配置Java环境,对吧?因为Struts2是基于Java的Web框架,所以用户应该已经安装了JDK和Tomcat之类的服务器。不过可能需要确认一下。 接下来,用户需要下载Struts2的库文件。通常是从Apache官网下载,可能需要指导用户找到正确的版本,比如最新的稳定版。解压后,会有一些必需的JAR包,比如struts2-core、xwork-core这些。需要把这些库添加到项目的构建路径中,或者在WEB-INF/lib目录下。 然后,配置web.xml文件,添加Struts2的过滤器。这里需要确认过滤器的类名是否正确,比如org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter。可能还要注意不同版本之间的差异,避免类路径错误。 接着是创建struts.xml配置文件,放在src目录下,或者类路径中。这个文件定义了Action和结果映射。需要确保用户正确配置包名、命名空间以及Action类的位置。如果用户项目结构复杂,可能需要调整这些设置。 另外,可能涉及到其他配置,比如常量设置,例如开发模式或者国际化设置。需要提醒用户在struts.xml中使用<constant>标签来配置这些选项。 在Eclipse中创建动态Web项目时,需要确保项目属性中的Java Build Path包含所有必需的StrAR文件。或者,用户可以使用Maven管理依赖,这样更简单。如果用户使用Maven,需要在pom.xml中添加Struts2的依赖项,这样自动下载JAR包。 可能还需要测试配置是否正确,比如创建一个简单的Action类,返回success,并对应一个JSP页面。然后部署到Tomcat服务器上,启动后访问对应的URL,看是否能正确跳转。 用户可能会遇到的问题包括:404错误(路径不正确)、类找不到(JAR包未正确添加)、过滤器配置错误等。需要提醒用户检查日志信息,尤其是Tomcat的启动日志,查看是否有初始化失败的信息。 另外,Eclipse的版本可能影响项目配置,比如较新的Eclipse可能需要不同的方式添加服务器运行时环境。需要确保用户安装了正确的服务器适配器,并将项目部署到服务器上。 总结步骤应该是:下载Struts2库,创建Web项目,添加依赖,配置web.xml和struts.xml,创建Action和JSP,部署测试。过程中需注意路径、版本兼容性以及日志排查错误。</think>### Eclipse 配置 Struts2 的步骤 #### 1. **环境准备** - **Eclipse IDE**:确保已安装 Eclipse Enterprise Edition(支持 Java EE)。 - **JDK**:安装 JDK 1.8 或更高版本,并配置环境变量。 - **Tomcat**:下载 Apache Tomcat(如 9.x),并在 Eclipse 中配置为服务器运行时环境。 --- #### 2. **下载 Struts2 框架** - 访问 [Apache Struts2 官网](https://struts.apache.org/),下载最新稳定版(如 Struts 2.5.30)。 - 解压后,获取以下核心 JAR 文件(位于 `struts-2.x.x\lib` 目录): ``` struts2-core-x.x.x.jar xwork-core-x.x.x.jar ognl-x.x.x.jar log4j-api-x.x.x.jar ``` --- #### 3. **创建动态 Web 项目** - **步骤**: 1. 打开 Eclipse,选择 `File > New > Dynamic Web Project`。 2. 输入项目名称(如 `Struts2Demo`),选择 Tomcat 服务器。 3. 保持默认配置,完成项目创建。 --- #### 4. **添加 Struts2 依赖** - **手动添加**: 1. 将步骤 2 中的 JAR 文件复制到项目的 `WebContent/WEB-INF/lib` 目录。 2. 右键项目,选择 `Build Path > Configure Build Path`,检查 JAR 是否自动加载。 - **Maven 方式**(推荐): 在 `pom.xml` 中添加依赖: ```xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.30</version> </dependency> ``` --- #### 5. **配置 `web.xml`** - 在 `WebContent/WEB-INF/web.xml` 中定义 Struts2 过滤器: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app> <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> ``` --- #### 6. **创建 `struts.xml`** - 在 `src` 目录下新建 `struts.xml`,配置 Action 映射: ```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> <constant name="struts.devMode" value="true" /> <!-- 开发模式 --> <package name="default" extends="struts-default"> <action name="hello" class="com.example.HelloAction"> <result>/hello.jsp</result> </action> </package> </struts> ``` --- #### 7. **编写 Action 类** - 创建 `HelloAction.java`: ```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; } // Getter & Setter public String getMessage() { return message; } } ``` --- #### 8. **创建 JSP 页面** - 在 `WebContent` 下新建 `hello.jsp`: ```jsp <%@ page contentType="text/html;charset=UTF-8" %> <html> <head><title>Struts2 Demo</title></head> <body> <h1>${message}</h1> </body> </html> ``` --- #### 9. **部署与测试** - 右键项目,选择 `Run As > Run on Server`,启动 Tomcat。 - 访问 URL:`http://localhost:8080/Struts2Demo/hello.action`,查看是否输出 `Hello Struts2!`。 --- ### 常见问题排查 1. **404 错误**: - 检查 URL 路径是否匹配 `struts.xml` 中的 `<action name>`。 - 确保 `web.xml` 中过滤器的 URL 模式为 `/*`。 2. **ClassNotFoundException**: - 确认所有 Struts2 JAR 文件已正确添加到 `WEB-INF/lib`。 3. **日志查看**: - 检查 Tomcat 控制台输出,定位初始化错误。 --- 通过以上步骤,即可在 Eclipse 中完成 Struts2 的基础配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值