Struts2全局结果集详解与配置教程

需积分: 3 2 下载量 200 浏览量 更新于2024-08-23 收藏 1.79MB PPT 举报
"Struts2是Java Web开发中的一个开源MVC框架,它的核心是基于拦截器的架构。本文主要讲解了Struts2的全局结果集(Global Results)以及相关的基础知识,包括Struts2的初步配置、Action类的创建、配置文件struts.xml的设置、登录与主页面的创建,以及Struts2请求-响应的工作流程。" **1. Struts2初识** 在Struts2框架中,首先需要将Struts2的相关jar包放入`WEB-INF/lib`目录,并在`web.xml`中配置Struts2的过滤器`StrutsPrepareAndExecuteFilter`,以便拦截所有请求并进行处理。配置包括定义filter-name和filter-class,以及映射URL模式。 **2. Action类创建** Action类是业务逻辑的载体,它通常继承自Struts2提供的基类,如`ActionSupport`,并实现执行方法,例如`execute()`。Action类的返回值对应不同的结果,这些结果可以被映射到特定的视图页面。 **3. struts.xml配置** `struts.xml`是Struts2的核心配置文件,它定义了Action、结果(Result)、拦截器等信息。全局结果集(Global Results)可以在`struts.xml`中定义,这样可以避免每个Action重复定义相同的返回结果。 **4. 登录和主页面** 登录页面`login.jsp`通常包含表单元素,用于收集用户输入。提交后,这些数据会通过Action传递,验证成功后跳转到主页面`index.jsp`。主页面显示登录成功的界面,可能包含用户的个性化信息或导航链接。 **5. 请求-响应流程** Struts2请求-响应流程大致如下: 1. 用户发送请求到达过滤器。 2. 过滤器识别请求并调用Struts2框架。 3. 框架解析Action名称,找到对应的Action类。 4. 创建Action实例并调用其execute方法。 5. execute方法返回的结果被映射到相应的视图页面。 6. 视图页面渲染后返回给用户。 **6. struts.xml中的智能提示** 在开发环境中,如果在`struts.xml`文件中没有代码提示,可以通过调整IDE的XML Catalog设置来启用。 **7. Namespace(命名空间)** Namespace是Struts2中的一个重要概念,用于区分不同的Action,提供了一种组织Action的方式,避免了Action名称的冲突。它在配置文件中通过`<package>`标签定义,并可以嵌套使用。 **8. 全局结果集(Global Results)** 全局结果集允许在`struts.xml`中定义一个通用的结果,这个结果可以被多个Action共享。当Action执行完毕后,如果没有匹配到特定的Action结果,那么系统会查找全局结果集,为Action提供一个默认的处理方式。 总结,Struts2提供了一个强大的MVC框架,简化了Web应用的开发,通过Action和结果集的组合实现了灵活的控制流,命名空间提供了更好的组织结构,而全局结果集则减少了代码的冗余。此外,其拦截器机制使得扩展和定制更加便捷,是Java Web开发中不可或缺的工具之一。

我用Spring5的aop应用时报这个错误Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\ecliple\web5\WEB-INF\applicationContext.xml]; nested exception is java.nio.file.NoSuchFileException: WEB-INF\applicationContext.xml,他说我的applicationContext.xml文件不存在,可是我明明有这个文件,另外我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <display-name>Struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>

2023-05-30 上传