struts2 (一) web.xml/struts.xml/Action 配置
来源:互联网 发布:那个软件电影票便宜 编辑:程序博客网 时间:2024/05/20 20:47
从网上下的传智播客 黎活明老师 struts2的ppt,摘录点 以备忘:
在struts2中,struts框架是通过Filter启动的。他在web.xml中的配置如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 自从Struts 2.1.3以后,下面的FilterDispatcher已经标注为过时
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。
注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件
在默认的配置文件struts.xml 中加入如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="xxx" namespace="/test" extends="struts-default">
<action name="helloworld" class="xxx.HelloWorldAction" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>
包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下Action的路径的一部分,如访问上面例子的Action,访问路径为:/test/helloworld.action。 namespace属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。
通常每个包都应该继承struts-default包, 因为Struts2很多核心的功能都是拦截器来实现。如:从请求中把请求参数封装到action、文件上传和数据验证等等都是通过拦截器实现的。 struts-default定义了这些拦截器和Result类型。可以这么说:当包继承了struts-default才能使用struts2提供的核心功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。 struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。
包还可以通过abstract=“true”定义为抽象包,抽象包中不能包含action。
Action配置中的各项默认值
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
1>如果没有为action指定class,默认是ActionSupport。
2>如果没有为action指定method,默认执行action中的execute() 方法。
3>如果没有指定result的name属性,默认值为success。
全局results
<global-results>
<result name="message">/message.jsp</result>
</global-results>
Action中注入值
Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。
public class HelloWorldAction{
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" >
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
上面通过<param>节点为action的savePath属性注入“/images”
<body>
${savepath }
</body>
- struts2 (一) web.xml/struts.xml/Action 配置
- struts2 配置web.xml struts.xml
- Struts2 在struts.xml配置Action
- Struts2 Action与超链接 struts.xml配置
- Struts2-action在struts.xml中的配置
- Struts2的配置 struts.xml Action详解
- (一)Struts2中struts.xml配置
- struts2 struts.xml配置
- Struts2-配置struts.xml
- struts2中web.xml配置struts.xml文件
- Struts2中的web.xml和struts.xml配置内容
- struts2.3.24用web.xml配置多个struts.xml
- struts2随笔(一)Action、struts.xml、Interceptor细节
- struts2随笔(一)Action、struts.xml、Interceptor细节
- Struts2中struts.xml的Action配置详解
- Struts2中struts.xml的Action配置详解
- Struts2中struts.xml的Action、namespace配置详解
- [转]Struts2中struts.xml的Action配置详解
- asp.net导航控件
- 让linux定时执行秒级任务
- CString截取
- 为什么越来越多的用户放弃了Linux?
- [How To]在Linux下设置无线网络桥接
- struts2 (一) web.xml/struts.xml/Action 配置
- 一个程序员的哲学思考(关于编程、关于人生)
- write readln(tempStr); writeln(tempStr,'---!');
- 基于单文档的记事本开发 .
- mount nfs ubuntu10.04
- 编程技巧整理
- Android SQLite基本操作
- Hibernate——脏检查和缓存清理机制
- 在 UML 静态结构图中使用类和对象