使用eclipse创建Struts2项目

本文详细介绍了如何在Eclipse Kepler Service Release 1中创建一个Struts2项目。首先新建一个Web项目,然后拷贝Struts2的jar包到项目的WEB-INF/lib目录下,接着复制web.xml中的filter部分到项目的web.xml。接着,复制Struts2-blank项目的源代码和struts.xml文件,以及jsp文件到相应位置。最后,启动项目并展示运行结果,完成一个简单的Struts2 HelloWorld项目。

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

eclipse版本: Kepler Service Release 1    http://www.eclipse.org/downloads/

struts版本:2.3.16     http://struts.apache.org/


1.新建web项目

打开Eclipse,新建一个web项目"Struts2"


项目名字


勾选 web.xml选项


建好的项目


2.拷贝struts的jar包

在struts2文件包中,找到struts-2.3.16\apps\struts2-blank.war文件,并将其用解压,这个项目完全是一个空白项目。

复制\WEB-INF\lib\ 下的所有jar包

将前面复制的所有jar包复制到eclipse所建立的项目 -- WebContent -- WEB-INF --lib下

已经复制成功后的lib目录视图

在Libraries下的Web App Libraries下也可以引用成功的jar包


3.拷贝web.xml

在struts的WEB-INF下有web.xml,可以直接拷贝到Eclipse的WEB-INF下,也可以只取其中的filter复制到Eclipse中已经的web.xml

拷贝filter

下面是一个完整的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts2</display-name>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <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>
</web-app>

4.拷贝Struts.xml文件以及相关的java文件和其他的xml配置文件

复制struts2-blank\WEB-INF\src\java目录下的所有文件

因为需要在Package Explorer中进行粘贴,在Project Explorer中不能正确粘贴。

所有先要切换到Package Explorer

拷贝好Struts.xml文件以及相关的java文件以后的视图

struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>

    <!-- Add packages here -->

</struts>


4.拷贝jsp文件

选择struts2-blank目录下的index.htm文件以及jsp目录

复制上述文件到WebContent下


5.启动项目

在需要启动的文件上右键点击运行

选择对应的server来运行项目


6.运行的页面

http://localhost:8080/Struts2/index.html

http://localhost:8080/Struts2/example/Welcome.jsp页面

点击“Sign On”以后


7.新增文件

修改struts.xml文件,增加

<action name="hello2">
            <result>/hello.jsp</result>
        </action>

在WebContent下新建hello.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
HelloWorld! By http://blog.youkuaiyun.com/unix21
</body>
</html>

重新Run文件,使用http://localhost:8080/Struts2/hello2就可以访问

至此一个基本的struts的helloworld项目就完成了。


更多参考:在Eclipse中配置Struts2项目(二)    很老的文章,不过每一步很详细,可以看看特别是JDK和Tomcat的配置

Eclipse怎样配置struts2?    文章较新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值