转自: http://www.blogjava.net/jzone/articles/321071.html
一、Struts2 主要几句话:
1、 Struts2 是由webwork2发展来的而非Struts1,相比Struts1,Struts2编码规范跟类似与webwork2
2、Struts2 非侵入式设计、Struts1属于侵入式设计
3、Struts1与ServletAPI、strutsAPI紧密耦合;Struts1则不
4、Struts2 利用拦截器进行AOP编程,实现如权限拦截功能
5、Struts2 提供了类型转换器
6、Struts2 提供多种表现层技术,如JSP/Freemarker/Velocity等
7、Struts2 的输入验证可以对指定方法进行验证,解决Struts1之痛
8、Struts2 提供了全局范围、包范围和Action范围的国际化资源文件管理实现
二、Struts2 需要的最少jar:
Struts-core-2.xx.jar
Xwork-2.xx.jar
Ognl-2.6.x.jar 对象图导航语言(Object Graph Navigation Language),Struts2通过其读写对象属性
Freemarker-2.3.x.jar
Commons-logging.jar
Commons-fileupload.jar
切勿全选所以jar,否则你会很痛苦,Struts2提供其他框架的支持,所以依赖其他jar、一些配置文件。
三、环境搭建三部曲:
1. 找到开发Struts2应用需要使用的jar文件
2. 编写Struts2配置文件
3. 在web.xml中加入Struts2MVC框架启动配置
附:2.1.8版本的配置
<!-- struts2 -->
<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>
四、Struts.xml配置文件中包的介绍
<package name="default" namespace="/test" extends="struts-default">
<action name="index" class=”cn.itcast.action.HelloWorldAction” methed=”execute”>
<result name="succcess">/page/hello.jsp</result>
</action>
</package>
管理一组业务功能相关的action,在实际应用中把一组应用功能相关的action放在一个包下
1.name必须,其他包要继承该包,必须经过该属性进行引用
2.namespace定义该包的命名空间,命名空间做为访问该包下action路径的一部分,如上action访问路径为:/test/index.action
3. extends 默认继承了struts-default包,可使这个包下面默认应用了struts2一些新功能,如拦截器等
4.abstract 当 abstract=”true” 则该包内不能有action
五、Struts2里Action名称的搜索顺序
1. 获取请求的URL路径,如下:
http://localhost:8080/struts2/path1/path2/path3/helloworld //配置可访问
http://localhost:8080/struts2/path1/path2/path3/as/helloworld //也可访问的
2. 首先寻找namespace为/path1/path2/path3的package,如果不存在这个package则执行步骤3;如果存在这个package,则在这个package中寻找名字为test的action,当在该package下寻找action时就会直接跑到默认namespace的package里面寻找action(默认的命名空间为空字符串””),如果在默认namespace的package里面还寻找不到action,页面提示找不到action
3. 寻找namespace为/path1/path2的package,如果不存在这个package,则转至步骤4;如果存在这个package,则在这个package中寻找名字为test的action,当在该package中寻找不到action时就会直接跑到默认namespace的package中寻找名字为test的action,在默认namespace的package里面还寻找不到该action,页面提示找不到action
4. 寻找namespace为/path1的package,如果不存在这个package则执行步骤5;如果存在这个package,则在这个package中寻找名字为test的action,当在该package中寻找不到action时就会直接跑到默认的namespace的package里卖弄去寻找名字为test的action,在默认namespace(<package name="default" extends="struts-default">)的package里面还寻找不到该action,页面提示不到action
5. 寻找namespace为/的package,如果存在这个package,则在这个package中寻找名字为test的action,当在package中寻找不得action或者不存在这个package时,都会去默认namespace的package里面寻找action,如果还找不到,页面提示找不到action
六、Action配置中各项默认值
1. 如果没有给action配置class,则默认的是ActionSupport
2. 如果没有给action配置method,则默认的是execute方法
3. 如果没有给result配置name,则默认的是success
例如:可以利用各种默认项的搭配,完成action转发
Struts1:
<action path=”/control/employee/addUI” >
<!—Struts1的转发 -->
<forward name=”addUI”>/WEB-INF/page/employeeAdd.jsp</forward>
<!— Struts1的重定向 -->
<!--
<forward name=”addUI” redirect=”true”>/WEB-INF/page/employeeAdd.jsp</forward>
-->
</action>
Struts2:
<action name=”addUI”>
<result>/WEB-INF/page/employeeAdd.jsp”</result>
</action>
同样访问http://localhost:8080/XX/addUI.action 就可访问到employeeAdd.jsp
七、Struts2 中result常用的视图转发类型(dispatcher(默认)、redirect、redirectAction、plainText、chain)
1. dispatcher-转发上面已经介绍了
(1)为缺省的result类型,一般情况下我们在struts.xml会这么写:
<result name="success">/main.jsp</result>
以上写法使用了两个默认,其完整的写法为:
<result name="success" type="dispatcher">
<param name="location">/maini.jsp</param>
</result>
第一个默认:type="dispatcher";第二个默认:设置的为location参数,location只能是页面,不能是另一个action(可用type="chain"解决)。
(2)实现方式
从doExecute方法看出,有三个出口(finalLocation为要跳转的地址):
pageContext.include(finalLocation);
dispatcher.forward(request, response); (dispatcher是根据finalLocation创建的)
dispatcher.include(request, response);
而我们知道,forward与include都是转发到context内部的资源。
2.redirect-重定向
a).<action name="redirect">
<result type="redirect">/index.jsp</result>
</action>
b).ognl表达式,在struts.xml中获取action中的变量
<action name="redirect">
<result type="redirect">
/index.jsp?username=${username}</result>
</action>
http://localhost:8080/struts2/redirect.action 将重定向到如下URL
http://localhost:8080/struts2/index.jsp?username=”action中定义的值”
传中文时候请进行编码:
username = URLEncoder.encode(“李显武”, “UTF-8”);
注意用get方式传的中文,在页面显示还需要解密:
URLDecoder.decode[new String(request.getParameter(“username”).getBytes(“ISO8859-1”), “UTF-8”]
注:action中要提供该变量的getter、setter方法
3. redirectAction-重定向到action(action在同一个包)
注: redirect和redirectAction两种结果类型在使用上其实并没有什么区别,只是写法不同而已。
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
<action name="redirectAction">
<result type="redirectAction">helloworld</result>
</action>
当action不在同一个包时候
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
<action name="redirectAction">
<result type="redirectAction">
<param name=”actionName”>action名</param>
<param name=”namespace”>需要重定向的action所在的包</param>
</result>
</action>
4. plainText-显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,我们可以使用此类型
<action name=”plainText”>
<result name=”souce” type=”plainText”>
<param name=”location”>/xxx.jsp</param>
<param name=”charSet”>UTF-8</param><!—指定读取文件的编码 -->
</result>
</action>
5. chain-action链
(1)主要用于把相关的几个action连接起来,共同完成一个功能。
<action name="step1" class="test.Step1Action">
<result name="success" type="chain">step2.action</result>
</action>
<action name="step2" class="test.Step2Action">
<result name="success">finish.jsp</result>
</action>
(2)实现方式:
查看execute()方法,主要思想如下:
// 根据Action名称finalActionName及要调用的方法finalMethodName来new一个代理对象proxy,并执行之
proxy = actionProxyFactory.createActionProxy(finalNamespace,
finalActionName, finalMethodName, extraContext);
proxy.execute();
(3)多个action间数据的传递
主要有两种方式:
1、由于处于chain中的action属于同一个http请求,共享一个ActionContext,故可以在上下文中获取,在页面上可以直接使用。手动获取的方法如下:
HttpServletRequest request = ServletActionContext.getRequest();
String s=(String)request.getAttribute("propName");
2、实现ModelDriven接口
在Step1Action中,加入getModel:
public Object getModel() {
return message;
}
在Step2Action中,加入setModel:
public void setModel(Object o){
System.out.println("message is:"+o);
}
注意,setModel的调用先于execute()方法后于构造方法。
八、在Struts2中公用的视图
<package ……>
<global-result>
<result name=”pub”>/public.jsp</result>
</global-result>
</package>
九、为Action属性注入值
<action name=”list” class=”cn.itcast.action.HelloWorldAction”>
<param name=”savepath”>/images</param>
<result>/WEB-INF/page/message.jsp</result>
</action>
从配置文件将value植入action的属性,再用EL表达式将action的属性值显示出来
注意变量的命名、getter、setter方法
十、指定Struts2处理的请求后缀
默认使用的是处理.action的请求,其实也可以通过常量”struts.action.extension”进行修改,如我们可以配置Struts2只处理以.do为后缀的请求路径:
<?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>
<constant name="struts.action.extension" value="do"></constant>
</struts>
也可以指定处理多个后缀请求 value之间以”,”分开如: value=”do,action,jhtml”
十一、细说常量的配置
Struts2中常量可以在多个配置文件中配,建议在struts.xml中配置方式如下:
<struts>
<constant name=”struts.action.extension” value=”do”/>
</struts>
因为常量可以在多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多个文件中配置了同一个常量,则后一个配置文件中的常量值将会覆盖前面文件中配置的常量值
分享到:
相关推荐
在"Struts 之旅 - 多国语言"这个主题中,我们将深入探讨Struts框架如何支持多语言国际化(i18n)功能,这对于构建全球化应用程序至关重要。 1. **国际化(i18n)基础**: 国际化是指设计和实现软件,使其能够适应...
7. **Plug-in配置**:插件扩展了Struts的功能,例如 strutstags-tiles 插件用于集成Tiles布局框架,`<plug-in>`元素下的`<set-property>`可以设置插件的属性。 8. **Exception处理**:`<global-exceptions>`允许...
在Struts1.2中,文件上传功能通常通过ActionForm类来实现。ActionForm是业务逻辑和表现层之间的桥梁,它可以接收用户的表单数据,包括上传的文件。首先,你需要创建一个继承自Struts的ActionForm类,并定义一个File...
在较旧版本的Struts中,`controller`元素用于配置控制器组件的属性,但在现代Struts版本中,这一元素的使用已减少,因为很多功能已被其他组件取代。 7. **message-resources 元素** 这个元素用于配置应用程序的...
插件扩展了 Struts 的功能,例如拦截器、过滤器等。配置插件需要指定插件类和相关的配置。 注意,`struts-config.xml` 文件的结构和元素顺序是固定的,错误的顺序可能导致 Struts 容器无法正常解析和启动。此外,`...
<message-resources parameter="struts/ApplicationResources"/> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <!-- Tiles配置 --> </plug-in> </struts-config> ``` 在实际开发中,还需要创建...
<action path="/myAction" type="org.springframework.web.struts.DelegatingActionProxy" parameter="myAction"/> ``` 4. **编写Action**:在Action类中,可以专注于处理HTTP请求,将业务逻辑委托给由Spring管理...
Struts的国际化(Internationalization,简称i18n)功能允许开发者为不同地区的用户提供本地化的界面和内容,使得同一应用在不同语言环境中能够自然地展现。下面我们将深入探讨Struts如何实现国际化功能。 首先,...
以上是赖家材老师的 Struts 传智播客笔记中关于 Struts 的基础和高级知识点的详细介绍,涵盖了 Struts 的入门概念、工作原理以及高级功能等方面的内容。这些知识点对于初学者来说非常宝贵,能够帮助他们快速掌握 ...
Struts是一个开源的MVC(Model-View-Controller)框架,它简化了Java Web应用的开发过程,并且支持多种功能,包括表单验证、国际化等。对于需要支持多语言的应用场景来说,Struts提供了非常方便的国际化支持。 ####...
通过以上步骤和知识点,你可以了解并实现Struts1框架下的国际化功能,为用户提供多语言支持,提升应用的全球适用性。在实际开发中,确保对各种语言环境的支持,能提高用户体验,并有助于拓宽应用的市场覆盖范围。
Struts 1.x 支持数据类型的自动转换,但也可以通过自定义转换器来扩展这种功能。 **1. 自定义数据转换器** - 创建一个实现 `org.apache.struts.util.LabelValueConverter` 接口的类。 - 实现 `convert` 方法来完成...
Struts-bean:message 使用详解 在Java Web开发中,Struts框架是一个广泛应用的MVC(Model-View-Controller)框架,它提供了一种组织应用...在实际项目中,结合源码阅读和相关工具,可以更深入地理解和利用这一功能。
Struts1.x是一个经典的Java Web开发框架,由Apache软件基金会维护,它提供了MVC(Model-View-Controller)设计模式的实现,使得开发者...记得在学习过程中,结合官方文档和其他教程,以深入掌握每个组件的功能和用法。
- **功能**: 在开发者、发布者与组装者之间传递配置信息。 - **作用**: 容器启动时从中读取配置信息,并据此装载和配置Web应用。 - **元素结构**: - `<web-app>`: 根元素,所有其他元素均需嵌套在此元素内部。 - `...
总的来说,你的"自己写的struts程序"是一个典型的Web应用实例,通过Struts框架实现了用户注册功能,涉及了Action、Form Bean、配置文件、视图展示等多个方面。这个练习有助于理解MVC模式和Struts的工作原理,对...
Struts提供了丰富的功能来支持国际化(Internationalization, i18n),即在应用程序中支持多种语言的能力,这对于面向全球用户的网站或应用来说非常重要。 #### 二、Struts国际化的实现步骤 ##### 1. 建立服务器...
在Struts 1.x版本中,配置文件主要包括`web.xml`和`struts-config.xml`,它们是实现Struts框架功能的核心配置。 **一、web.xml配置文件** `web.xml`是每个Web应用程序的部署描述符,它告诉服务器如何启动和运行...