strtus2笔记2

1.result组件
  在strtus.xml的文件里面:
  Action响应视图:
    响应视图的时候如果不声明,默认转发:type=dispacher
    重定向:<result name="ok" type="redirect">/ok.jsp</result>
  Action之间的:
    转发:<result name="ok" type="chain">/ok.jsp</result>
    重定向:<result name="ok" type="redirectAction">helloAction</result>


2.json
  1.在ajax的url项写的是strtus.xml里面的映射,如ajaxAction
  2.在配置文件strtus.xml里,需要继承json-default,
  3.<result type="json"></result>
  <package name="demo02" extends="json-default" namespace="/">
    <action name="ajaxAction" class="action.RegisterAction" method="ajax">
      <result type="json"></result>
    </action>
  </package>


3.在jsp页面使用UL标签
  1.导入:<%@ taglib prefix="s" uri="/struts-tags"@%>
  2.就可以使用UL标签写jsp页面了(使用UL写一个form表单)
  3.写实体类:注意:表单里面的name属性都要和实体类的属性名对应(一致)
  4.提交表单后数据还可以回显到页面上,便于用户修改,增加用户体验

4.拦截器的使用:
  1.创建一个类,实现interceptor接口,实现intercept方法
  2.intercept(ActionInvocation invocatoin)方法里面如何写?
    1.拦截器--前部分处理,满足条件走2,不满足根据可以return字符串(视图或action)
    2.invocatoin.invoke();继续执行action和result
    3.拦截器后续处理
  3.在struts.xml配置文件里配置拦截器:和action同级
    属性和action一样:name:名字 class:引用这个名字的时候执行哪个类(包名+拦截器类名)
    <interceptors>
      <interceptor name="first" class="interceptor.First"></interceptor>
    </interceptors>

  4.在action里使用拦截器:
    1.默认的ActionSupport检测不出拦截器的执行时间,所以要用下面自己定义的类测试2
    <action name="loginAction">
      <interceptor-ref name="first"></interceptor-ref>
      <result>/WEB-INF/login.jsp</result>
    </action>

    2.自己定义的类action.RegisterAction
      <action name="ajaxAction" class="action.RegisterAction" method="ajax">
        <interceptor-ref name="first"></interceptor-ref>
        <result type="json"></result>
      </action>

  5.最后:
    1.拦截器可以定义多个
    2.框架定义了很多栏机器,在strtus-default.xml文件里面
    如果我们要应用框架提供的拦截器/栈
      1.在我们的strtus.xml里面<interceptors></interceptors>里面添加
        <interceptor-ref name="defaultStack"/>
      2.就可以在action里面加拦截器/栈了
      3.如果自己定义的拦截器很多,又要重复使用,可以定义一个拦截器栈(和interceptor同级)
        <interceptor-stack name="myinterceptors">
          <interceptor-ref name="first"/>
          <interceptor-ref name="second"/>
        </interceptor-stack>
      4.直接调用拦截器栈就可以调用多个拦截器
        <interceptor-ref name="myinterceptors"></interceptor-ref>
      5.如果拦截器栈在<interceptors></interceptors>外面(和它同级),就表示定义的所有action都使用了拦截器栈

5.ActionSupport类的作用:
  1.在strtus.xml 配置文件中
    <action name="ajaxAction" class="action.RegisterAction" method="ajax">
      <result type="json"></result>
    </action>
  2.如果我们不写class,默认就是ActionSupport
    如果result不写name属性,默认是success
  3.关于使用:可以简化转发
    不用定义Action了,直接在配置文件文件里写就可以转发了
    <action name="loginAction">
      <result>/WEB-INF/login.jsp</result>
    </action>

猜你喜欢

转载自www.cnblogs.com/big-cut-cat/p/strtus2.html