使用eclipse创建Struts2项目

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

[plain]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>Struts2</display-name>  
  4.   <filter>  
  5.         <filter-name>struts2</filter-name>  
  6.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  7.     </filter>  
  8.   
  9.     <filter-mapping>  
  10.         <filter-name>struts2</filter-name>  
  11.         <url-pattern>/*</url-pattern>  
  12.     </filter-mapping>  
  13.   <welcome-file-list>  
  14.     <welcome-file>index.html</welcome-file>  
  15.     <welcome-file>index.htm</welcome-file>  
  16.     <welcome-file>index.jsp</welcome-file>  
  17.     <welcome-file>default.html</welcome-file>  
  18.     <welcome-file>default.htm</welcome-file>  
  19.     <welcome-file>default.jsp</welcome-file>  
  20.   </welcome-file-list>  
  21. </web-app>  

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

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

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

所有先要切换到Package Explorer

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

struts.xml文件

[plain]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  9.     <constant name="struts.devMode" value="true" />  
  10.   
  11.     <package name="default" namespace="/" extends="struts-default">  
  12.   
  13.         <default-action-ref name="index" />  
  14.   
  15.         <global-results>  
  16.             <result name="error">/error.jsp</result>  
  17.         </global-results>  
  18.   
  19.         <global-exception-mappings>  
  20.             <exception-mapping exception="java.lang.Exception" result="error"/>  
  21.         </global-exception-mappings>  
  22.   
  23.         <action name="index">  
  24.             <result type="redirectAction">  
  25.                 <param name="actionName">HelloWorld</param>  
  26.                 <param name="namespace">/example</param>  
  27.             </result>  
  28.         </action>  
  29.     </package>  
  30.   
  31.     <include file="example.xml"/>  
  32.   
  33.     <!-- Add packages here -->  
  34.   
  35. </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文件,增加

[plain]  view plain  copy
  1. <action name="hello2">  
  2.             <result>/hello.jsp</result>  
  3.         </action>  

在WebContent下新建hello.jsp文件

[java]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. HelloWorld! By http://blog.csdn.net/unix21  
  11. </body>  
  12. </html>  

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

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


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

Eclipse怎样配置struts2?    文章较新

 网页地址:http://blog.csdn.net/unix21/article/details/18903021

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值