Struts学习笔记要点

  一、 Struts2 与 Stuts:

1 Struts2是由webwork2发展来的而非Struts1,相比Struts1Struts2编码规范跟类似与webwork2

2Struts2非侵入式设计、Struts1属于侵入式设计

3Struts1ServletAPIstrutsAPI紧密耦合;Struts1则不

4Struts2利用拦截器进行AOP编程,实现如权限拦截功能

5Struts2提供了类型转换器

6Struts2提供多种表现层技术,如JSP/Freemarker/Velocity

7Struts2的输入验证可以对指定方法进行验证,解决Struts1之痛

8Struts2提供了全局范围、包范围和Action范围的国际化资源文件管理实现

9
Struts2核心组件:Action,ValueStack,Result,Interceptor


二、
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

 

 

三、环境搭建:

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放在一个包下

1name必须,其他包要继承该包,必须经过该属性进行引用

2namespace定义该包的命名空间,命名空间做为访问该包下action路径的一部分,如上action访问路径为:/test/index.action

3. extends 默认继承了struts-default包,可使这个包下面默认应用了struts2一些新功能,如拦截器等

4abstract abstract=”true”则该包内不能有action

 

五、Struts2Action名称的搜索顺序

1.获取请求的URL路径,如下:

http://localhost:8080/struts2/path1/path2/path3/helloworld    //配置可访问

http://localhost:8080/struts2/path1/path2/path3/as/helloworld    //也可访问的

2.首先寻找namespace/path1/path2/path3package,如果不存在这个package则执行步骤3;如果存在这个package,则在这个package中寻找名字为testaction,当在该package下寻找action时就会直接跑到默认namespacepackage里面寻找action(默认的命名空间为空字符串””),如果在默认namespacepackage里面还寻找不到action,页面提示找不到action

3.寻找namespace/path1/path2package,如果不存在这个package,则转至步骤4;如果存在这个package,则在这个package中寻找名字为testaction,当在该package中寻找不到action时就会直接跑到默认namespacepackage中寻找名字为testaction,在默认namespacepackage里面还寻找不到该action,页面提示找不到action

4.寻找namespace/path1package,如果不存在这个package则执行步骤5;如果存在这个package,则在这个package中寻找名字为testaction,当在该package中寻找不到action时就会直接跑到默认的namespacepackage里卖弄去寻找名字为testaction,在默认namespace<packagename="default"extends="struts-default">package里面还寻找不到该action,页面提示不到action

5.寻找namespace/package,如果存在这个package,则在这个package中寻找名字为testaction,当在package中寻找不得action或者不存在这个package时,都会去默认namespacepackage里面寻找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>


七、Struts2 result常用的视图转发类型(dispatcher(默认)、redirectredirectActionplainText、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);


 

而我们知道,forwardinclude都是转发到context内部的资源。

2redirect-重定向

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中传下来的值,只需要在下一个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及要调用的方法finalMethodNamenew一个代理对象proxy,并执行之

 

 proxy = actionProxyFactory.createActionProxy(finalNamespace,   

           finalActionName, finalMethodName, extraContext);   

    proxy.execute();


 

 


 

 

你可能感兴趣的:(Struts学习笔记要点)