您的位置:首页 > 编程语言 > Java开发

struts2的框架解析第一篇环境的搭建

2017-12-04 13:41 435 查看
struts2属于无侵入式设计(比struts1的耦合度低)

struts2提供了拦截器【利用拦截器进行AOP编程,实现如权限拦截等功能】;类型转换器(不需要底层实现BeanUtil注册类型转换器);提供支持多种表现层技术:JSP、free marker、velocity等;输入校验可以对指定方法进行校验;提供全局范围 、包范围和action范围的国际化资源文件管理实现。

开发环境的搭建:

1、struts2应用需要用到的依赖jar文件

http://struts.apache.org/download.cgi#struts2014 下载struts-2.x.x-all.zip

struts2-core-2.x.x.jar:struts2框架的核心类库

xwork-2.x.x.jar:XWork类库,struts2在其上构建

ognl-2.6.x.jar:对象图导航语音(object graph navigation language),struts2框架通过其读写对象的属性

freemarker-2.3.x.jar:struts2的UI标签模版使用freemarker编写

commons-logging-1.1.x.jar:ASF出品的日志包,struts2框架使用这个日志包来支持log4j和JDK1.4+的日志记录。

commons-fileupload-1.2.1.jar:文件上传组件,2.1.6版本必须加入此文件

2、编写struts2的配置文件

struts2默认的配置文件为struts.xml,该文件需要存放在WEB-INF/classes下,该文件的模版如下:(路径:/Users/apple/ywj_projects/struts2/struts-2.5.14.1/apps/struts2-showcase/WEB-INF/classes)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- START SNIPPET: xworkSample -->
<struts>
</struts>
<!-- END SNIPPET: xworkSample -->


3、在web.xml中加入struts2MVC框架启动配置

在struts1.x中,struts框架是通过servlet启动的,在struts2中struts框架是通过filter启动的。它在web.xml中的配置如下:(/Users/apple/my_projects/struts2/struts-2.5.14.1/apps/struts2-rest-showcase/WEB-INF)

<!-- Filters -->
<!-- START SNIPPET: filter -->
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->

<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


在strutsPrepareAndExecuteFilter的init()方法中将会读取类路径默认的配置文件struts.xml完成初始化操作。

注意:struts2读取到struts.xml的内容后,以javabean的形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: