2.导入所需要的jar包到/webroot/WEB-INF/lib下:
struts2-core-x.x.x.jar:Struts2的核心库;
xwork-x.x.x.jar:WebWork的核心库,需要它的支持;
ognl-x.x.x.jar:OGNL表达式语言,Struts 2支持该EL;
freemarker-x.x.x.jar:表现层框架,定义了Struts 2的可视组件主题;
commons-logging-x.x.x.jar:日志管理;
内部配置文件由Struts 2 框架自动加载,对其自身进行配置。如:struts-default.xml和struts-plugin.xml等;外部配置由开发人员进行配置,用于对web应用进行配置。如:web.xml,struts.xml,struts.properties文件。![]()
1).需要添加的内容有:2).filter元素:其中两个<filter-name>值需要一致。<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
3).指定初始化参数:在<filter>元素中使用<init-param>元素。如:4).<init-param>的参数可以有以下几个:<init-param> <param-name>configProviders</param-name> <param-value>lee.MyConfigurationProvider</param-value> </init-param>
config:要加载的xml配置文件的列表,用","分隔,如果没有这个参数,默认使用struts.xml,struts-default.xml,struts-plugin.xml三个参数;
actionPackage:需要加载的action所在的java包名列表,用","分隔;configProviders:实现configProviders接口的java类名列表,用","分隔;*:任何其他的参数都会被当做struts2的常量;
5.struts.xml:所有常量,Action和拦截器等,几乎都配置在该文件中。文件通常放在src下,在该目录下的struts.xml文件可以内struts2框架自动加载。文件定义见struts-core文件中的dtd文件。
1.形式如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.i18n.encoding" value="gbk" /> <include file ="struts-default.xml" /> <package name="test" namespace="/" extends="struts-default"> <global-results> <result name="error">/error.html</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.RuntimeException"> </exception-mapping> </global-exception-mappings> <action name="helloworld" class="com.itzcn.action.HelloWorld"> <result name="index">/index.jsp</result> </action> </package> </struts>
2.元素类型和结构关系:带*表示可以出现0次或者多次,带?表示出现0次或者1次,带+表示出现1次或多次,不带符号(struts)表示必不可少。
6.struts-properties:位于src目录下。是属性定义文件,在该文件正定义一些属性和常量。可以实现struts中的参数进行配置。(必须严格遵照key-value形式)
7.struts-default.xml:位于struts2-core中,是Struts框架的基础文件。
例如,在前面的struts.xml中,有如下代码:
<package name="default" extends="struts-default">就是继承了struts-default