Struts2教程:URL标签与优势解析

需积分: 9 6 下载量 112 浏览量 更新于2024-08-18 收藏 365KB PPT 举报
"这篇教程主要介绍了Struts2框架中的URL标签用法以及Struts2框架的核心优势和搭建环境的步骤。" 在Struts2框架中,`<s:url>`标签用于生成URL,它可以与`<s:param>`标签配合使用,传递参数。例如,`<s:url action="helloworld_add" namespace="/test"><s:param name="personid" value="23"/></s:url>`会生成一个URL:`/struts/test/helloworld_add.action?personid=23`。这里的红色部分表示内容路径,`namespace`定义了动作的命名空间,`action`指定了要执行的动作,`<s:param>`则用来添加请求参数。 在处理字符串类型的属性值时,`%`符号在OGNL(Object-Graph Navigation Language)表达式中起到计算值的作用。例如,`<s:set name="myurl" value="'http://www.foshanshop.net'"/>`设置了一个变量`myurl`,然后`<s:url value="#myurl" />`和`<s:url value="%{#myurl}" />`都会输出这个变量的值,即`http://www.foshanshop.net`。 Struts2框架是在WebWork2的基础上发展起来的,与Struts1相比,它具有许多优势: 1. **非侵入式设计**:Struts2的控制器并不直接依赖Servlet API和Struts API,使得应用程序更易于测试和维护,与Struts1的侵入式设计形成鲜明对比。例如,Struts1的Action类需要继承自`Action`或`ActionSupport`,而Struts2的Action类可以是任何实现了`execute`方法的类。 2. **拦截器**:Struts2引入了拦截器机制,允许开发者实现AOP(面向切面编程),如权限控制、日志记录等,增强了灵活性和可扩展性。 3. **类型转换器**:Struts2内置了类型转换器,能自动将请求参数转换为指定类型,无需像Struts1那样手动注册转换器。 4. **多视图技术支持**:Struts2支持多种视图技术,如JSP、FreeMarker、Velocity等,提供了更多选择。 5. **输入验证**:Struts2的验证机制可以针对特定方法进行,比Struts1的全局验证更加灵活。 6. **国际化管理**:Struts2提供了不同级别的国际化资源文件管理,包括全局、包和Action范围。 要搭建Struts2的开发环境,通常需要完成以下步骤: 1. 获取所需的JAR文件,可以从Apache Struts官方网站下载最新版本的Struts2框架包。 2. 编写Struts2的配置文件,包括struts.xml,定义Action、拦截器等配置。 3. 在`web.xml`中配置Struts2的前端控制器(Filter),以启动Struts2 MVC框架。 通过以上步骤,开发者可以快速建立起基于Struts2的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 上传