Struts2.0入门指南:从配置到HelloWorld

需积分: 3 3 下载量 121 浏览量 更新于2024-07-31 收藏 2.67MB DOC 举报
"struts2基本配置使用手册,适合新手学习,涵盖了Struts2.0的环境搭建、配置和基础应用。" Struts2是Java Web开发中的一个热门MVC框架,它是在Struts1的基础上发展起来的,针对Struts1的一些不足进行了改进,如减少了代码量、增强了可测试性等。Struts2提供了更加灵活和强大的功能,包括使用MVC2模型、丰富的标签库和开源特性。 在开始使用Struts2之前,首先需要进行环境搭建。这个过程包括以下几个步骤: 1. 下载Struts2框架:从Apache官方网站获取Struts2的最新版本,这通常包含了一套完整的jar包,用于支持框架的运行。 2. 创建Web工程:在Eclipse中新建一个Dynamic Web Project,按照提示设置项目名称和选择服务器环境,例如Apache Tomcat v5.5。 3. 添加Struts2依赖:将Struts2的lib目录下的所有jar文件复制到工程的WebContent/WEB-INF/lib目录下,并确保这些jar包被添加到项目的构建路径中,以便于编译和运行。 4. 配置web.xml:这是Struts2应用的关键步骤。需要修改web.xml文件,引入Struts2的核心过滤器,配置Struts2的前端控制器。例如,添加以下代码: ```xml <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> ``` 这样,所有进入应用的请求都将通过Struts2的过滤器处理。 5. 创建Action类:Struts2的核心是Action类,它是业务逻辑的载体。你需要创建一个Action类,继承自Struts2提供的基类,例如`ActionSupport`,并实现相应的方法。 6. 编写配置文件:Struts2使用XML配置文件(通常命名为struts.xml)来定义Action及其结果页面、参数映射等。例如: ```xml <package name="hello" namespace="/" extends="struts-default"> <action name="hello" class="com.example.HelloWorldAction"> <result name="success">/HelloWorld.jsp</result> </action> </package> ``` 在这个例子中,`hello`是Action的名称,`com.example.HelloWorldAction`是对应的Action类,`success`是操作成功后的跳转结果,指向`HelloWorld.jsp`页面。 7. 创建视图:最后,编写JSP页面作为视图层,展示Action执行后返回的数据。 通过以上步骤,你就完成了Struts2的基础配置,可以运行并测试你的第一个Struts2应用程序。在实践中,Struts2还提供了拦截器(Interceptor)、插件(Plug-in)等功能,以及OGNL表达式语言,使得应用程序的开发更加便捷和模块化。对于开发者来说,理解并熟练掌握Struts2的配置和使用,是提升开发效率和代码质量的重要途径。

192.168.85.1 - - [26/Jun/2022:06:07:07 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 24 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 12925 192.168.85.1 - - [26/Jun/2022:06:07:11 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 200 14 192.168.85.1 - - [26/Jun/2022:06:08:06 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 15 192.168.85.1 - - [26/Jun/2022:06:08:16 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 1227 192.168.85.1 - - [26/Jun/2022:06:10:15 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 79 192.168.85.1 - - [26/Jun/2022:06:13:25 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 404 752 192.168.85.1 - - [26/Jun/2022:06:16:42 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:16:57 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:18:55 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:02 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 35 192.168.85.1 - - [26/Jun/2022:06:19:09 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:19:34 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 400 192.168.85.1 - - [26/Jun/2022:06:20:37 -0400] "POST /struts2-showcase/index.action HTTP/1.1" 500 5 192.168.85.1 - - [26/Jun/2022:06:20:42 -0400] "GET //struts2-showcase/hhh1.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:46 -0400] "GET //struts2-showcase/hhh.jsp HTTP/1.1" 403 642 192.168.85.1 - - [26/Jun/2022:06:20:51 -0400] "GET /struts2-showcase/hhh.jsp HTTP/1.1" 403 642

2023-07-12 上传