第一天:初识struts2

一、struts2概述

struts2是一个比较优秀和流行的MVC框架,现实开发中似乎值把它当做controller即代替传统的servlet。struts2透过一系列的选择过滤器来实现控制“转发”。

百度百科上的是struts2工作流程:

流程图

如果了解详细的话请点击:百度百科-struts2

二、搭建struts2开发环境

去官网下载struts2,我们采用最新版,struts-2.3.1.2。

官方地址:struts2

1、引入相应jar包(总共8个)

commons-fileupload-1.2.2.jar     

commons-io-2.0.1.jar            

commons-lang-2.5.jar

/以上三个jar是上传组件/

freemarker-2.3.18.jar             Struts 2UI标签的模板使用FreeMarker编写

ognl-3.0.4jar                            对象图导航语言(Object GraphNavigation Language

struts2-core-2.3.1.2.jar          Struts 2框架的核心类库

xwork-core-2.3.1.2jar              XWork类库,Struts2在其上构建

javassist-3.11.0.GA.jar            

2、 编写struts.xml文件(struts.xml必须放在WEB-INF的classes目录下(即放在src目录下)):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts></struts>

3、 在web.xml中加入Struts2 MVC框架启动配置
在web.xml中加入:
<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-2.3.dtd文件的解读

<?xml version="1.0" encoding="UTF-8"?>
<!--
   Struts configuration DTD.
   Use the following DOCTYPE

   <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
-->

<!ELEMENT struts ((package|include|bean|constant)*, unknown-handler-stack?)>
<!ATTLIST struts
    order CDATA #IMPLIED
>

<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
<!ATTLIST package
    name CDATA #REQUIRED
    extends CDATA #IMPLIED
    namespace CDATA #IMPLIED
    abstract CDATA #IMPLIED
    strict-method-invocation CDATA #IMPLIED
    externalReferenceResolver NMTOKEN #IMPLIED
>

<!ELEMENT result-types (result-type+)>

<!ELEMENT result-type (param*)>
<!ATTLIST result-type
    name CDATA #REQUIRED
    class CDATA #REQUIRED
    default (true|false) "false"
>

<!ELEMENT interceptors (interceptor|interceptor-stack)+>

<!ELEMENT interceptor (param*)>
<!ATTLIST interceptor
    name CDATA #REQUIRED
    class CDATA #REQUIRED
>

<!ELEMENT interceptor-stack (interceptor-ref*)>
<!ATTLIST interceptor-stack
    name CDATA #REQUIRED
>

<!ELEMENT interceptor-ref (param*)>
<!ATTLIST interceptor-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-interceptor-ref (#PCDATA)>
<!ATTLIST default-interceptor-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-action-ref (#PCDATA)>
<!ATTLIST default-action-ref
    name CDATA #REQUIRED
>

<!ELEMENT default-class-ref (#PCDATA)>
<!ATTLIST default-class-ref
    class CDATA #REQUIRED
>

<!ELEMENT global-results (result+)>

<!ELEMENT global-exception-mappings (exception-mapping+)>

<!ELEMENT action ((param|result|interceptor-ref|exception-mapping)*,allowed-methods?)>
<!ATTLIST action
    name CDATA #REQUIRED
    class CDATA #IMPLIED
    method CDATA #IMPLIED
    converter CDATA #IMPLIED
>

<!ELEMENT param (#PCDATA)>
<!ATTLIST param
    name CDATA #REQUIRED
>

<!ELEMENT result (#PCDATA|param)*>
<!ATTLIST result
    name CDATA #IMPLIED
    type CDATA #IMPLIED
>

<!ELEMENT exception-mapping (#PCDATA|param)*>
<!ATTLIST exception-mapping
    name CDATA #IMPLIED
    exception CDATA #REQUIRED
    result CDATA #REQUIRED
>

<!ELEMENT allowed-methods (#PCDATA)>

<!ELEMENT include (#PCDATA)>
<!ATTLIST include
    file CDATA #REQUIRED
>

<!ELEMENT bean (#PCDATA)>
<!ATTLIST bean
    type CDATA #IMPLIED
    name CDATA #IMPLIED
    class CDATA #REQUIRED
    scope CDATA #IMPLIED
    static CDATA #IMPLIED
    optional CDATA #IMPLIED
>

<!ELEMENT constant (#PCDATA)>
<!ATTLIST constant
    name CDATA #REQUIRED
    value CDATA #REQUIRED
>

<!ELEMENT unknown-handler-stack (unknown-handler-ref*)>
<!ELEMENT unknown-handler-ref (#PCDATA)>
<!ATTLIST unknown-handler-ref
    name CDATA #REQUIRED
> 

<struts>根标签里面可出现(package|include|bean|constant)*

package(包):在struts2框架中使用包来管理Action,包的作用和java中的类包是非常类似的,它主要用于管理一组业务功能相关的action。在实际应用中,我们应该把一组业务功能相关的Action放在同一个包下。配置包时必须指定name属性,该name属性值可以任意取名,但必须唯一,他不对应java的类包,如果其他包要继承该包,必须通过该属性进行引用。包的namespace属性用于定义该包的命名空间,命名空间作为访问该包下的Action的路径的一部分。通常每个包都应该继承struts-default包,因为Struts2很多核心的功能都是拦截器来实现。


<!ATTLIST package

    name CDATA #REQUIRED

    extends CDATA #IMPLIED

    namespace CDATA #IMPLIED

    abstract CDATA #IMPLIED

    strict-method-invocation CDATA #IMPLIED

    externalReferenceResolver NMTOKEN #IMPLIED

package下的标签:<!ELEMENT package (result-types?, interceptors?,default-interceptor-ref?, default-action-ref?, default-class-ref?,global-results?, global-exception-mappings?, action*)>

常用的action:一个antion相当于一个servlet name属性:相当于servlet的映射,class属性:相当于servlet的class类的路径method:相当于doGet()、doPost方法,这里需要自己写方法,如果省略,默认调用execute方法


<!ELEMENT action((param|result|interceptor-ref|exception-mapping)*,allowed-methods?)>

<!ATTLIST action

    name CDATA #REQUIRED

    class CDATA #IMPLIED

    method CDATA #IMPLIED

    converter CDATA #IMPLIED

action里面的result标签:result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、redirectAction 、 plainText。


<!ELEMENT result(#PCDATA|param)*>

<!ATTLIST result

    name CDATA #IMPLIED

    type CDATA #IMPLIED

当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。使用<global-results>标签。

<global-results>

        <resultname="message">/message.jsp</result>

</global-results>



你可能感兴趣的:(struts,servlet,Interceptor,Class,action,include)