struts2 <s:action>标签的用法及Spring在web.xml中的配置
一、走近struts2 <s:action>标签
使用action标签,可以允许在jsp页面中直接调用Action,(类似AJAX页面调用)在调用Action时候,可以指定需要被调用的Action的name和namespace。如果指定了executeResult参数的属性值为true,该标签会把Action的处理结果(视图资源)包含到本页面中。 使用action标签指定属性有:
-
id: 可选属性,作为该Action的引用ID; -
name: 必选属性,指定调用Action; -
namespace: 可选属性,指定该标签调用Action所属namespace; -
executeResult: 可选属性,指定是否将Action的处理结果包含到本页面中。默认值为false,不包含。 -
ignoreContextParam: 可选参数,指定该页面的请求参数是否需要传入调用的Action中,默认值是false,即传入参数。
传递参数
<s:action name="actinName" executeResult="true" namespace="/">
<s:param name="currentPage" value="传递的值"></s:param>
</s:action>
<s:param ></s:param>是以request方式的值的,而不是以参数传值。所以request.getParameter("")会获取不到值。 通过request.getAttribute("")方式可以获取
另外:使用<s:param ></s:param> value指代的是action定义的对象的名称,而不是一个值。和 <s:textfield>中的name是一个意思,所以要要传递字符串的不能把值写value中而应该写成:<s:param >写到这里</s:param>。
二、Spring在web.xml中的配置
在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中。在web项目中,配置文件加载到web容器中进行解析。目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet。这两种在功能上完全相同,只是前一种是基于Servlet2.3版本中新引入的Listener接口实现,而后一种是基于Servlet接口实现,以下是这两种加载器在web.xml中的配置应用:
ContextLoaderListener
<listener>
<listener-class>org.springframework.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderServlet
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
通过上面的配置,web容器会自动加载applicationcontext.xml初始化。
如果需要指定配置文件的位置,可通过context-param加以指定:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
之后,可以通过WebApplicationContextUtils.getWebApplicationContext方法在web应用中获取applicationcontext的引用。
- 点赞
- 收藏
- 关注作者
评论(0)