struts知识点总结

一、Action

1、namespace

namespace决定了action的访问路径,默认为""namespace可以不写。囊括了其他action处理不了的action

。namespace最好也用模块来进行命名。

 

 2、路径问题

  struts2中的路径问题是根据action的路径而不是jsp路径来确定,jsp中用request.getContextRoot方式来拿到webapp的路径。或者使用myeclipse经常用的,指定basePath。

3、动态方法调用DMI 

   

    Action执行的时候并不一定要执行execute方法可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)(推荐),前者会产生太多的action,所以不推荐使用。

4、通配符的使用

<action name="*_*" class="com.test.action.{1}Action" method="{2}">
            <result>/{1}_{2}_success.jsp</result>
     </action>

 

5、属性接收参数

<!--EndFragment-->

<!--EndFragment-->
<a href="user/user!add?name=a&age=8">

 

6、Domain Model接收参数

<!--EndFragment-->

<a href="user/user!add?user.name=a&user.age=8">

7、使用ModelDriven接收参数

    

<a href="user/user!add?name=a&age=8">

 

  对应的action类,要实现ModelDriven接口

 8、简单数据校验

      使用addFieldError.

 9、获取网页元素
     有四种解决方案

  (1

<!--EndFragment-->

<!--EndFragment-->
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();

 

2)需要实现三个接口:RequestAware,SessionAware, ApplicationAware

      覆盖三个set方法;然后就可以直接使用了

 (3)使用ServletActionContext 

<!--EndFragment-->

<!--EndFragment-->
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();

  (4)实现ServletRequestAware接口,覆盖对应方法后就可以直接使用了。

  取值方法

<!--EndFragment-->

<!--EndFragment-->
<s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
<s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
<s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />

 

 10、结果类型

   基本结果类型:  常用的有dispatcher、redirect,chain,redirectAction类型

    全局结果类型,在对应的action映射中找不到时就会访问全局结果映射

<global-results>
      <result name="mainpage">/main.jsp</result>
 </global-results>

 

   动态结果类型:action类中保存映射

 

public String execute() throws Exception {
      if(type == 1) r="/user_success.jsp";
      else if (type == 2) r="/user_error.jsp";
      return "success";
}
<result>${r}</result> 
<a href="user/user?type=1">

 

   向结果传参数

      

<result type="redirect">/user_success.jsp?t=${type}</result>

 

二、ognl表达式

   访问值栈中的action的普通属性: username = <s:property value="username"/> 
    访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%>
    访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/>
    访问值栈中对象的普通方法:<s:property value="password.length()"/><
    访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" />
    访问值栈中action的普通方法:<s:property value="m()" />
    
    访问静态方法:<s:property value="@com.test.ognl.S@s()"/>
    访问静态属性:<s:property value="@com.test.ognl.S@STR"/>
    访问Math类的静态方法:<s:property value="@@max(2,3)" />
    
    访问普通类的构造方法:<s:property value="new com.test.ognl.User(8)"/>
    
    访问List:<s:property value="users"/>
    访问List中某个元素:<s:property value="users[1]"/>
    访问List中元素某个属性的集合:<s:property value="users.{age}"/>
    访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/>
    访问Set:<s:property value="dogs"/>
    访问Set中某个元素:<s:property value="dogs[1]"/>
    访问Map:<s:property value="dogMap"/>
    访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/>
    访问Map中所有的key:<s:property value="dogMap.keys"/>
    访问Map中所有的value:<s:property value="dogMap.values"/>
    访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> 
    
    投影(过滤):<s:property value="users.{?#this.age==1}[0]"/>
    投影:<s:property value="users.{^#this.age>1}.{age}"/>
    投影:<s:property value="users.{$#this.age>1}.{age}"/>
    投影:<s:property value="users.{$#this.age>1}.{age} == null"/>
    
    []:<s:property value="[0].username"/>

 

三、struts标签

 property: <s:property value="username"/> 
     property 取值为字符串: <s:property value="'username'"/> 
     property 设定默认值: <s:property value="admin" default="管理员"/> 
     property 设定HTML: <s:property value="'<hr/>'" escape="false"/> 
          
     set 设定adminName值(默认为request 和 ActionContext): <s:set var="adminName" value="username" />
        
     set 从request取值: <s:property value="#request.adminName" />
     set 从ActionContext取值: <s:property value="#adminName" />
        
     set 设定范围: <s:set name="adminPassword" value="password" scope="page"/>
     set 从相应范围取值: <%=pageContext.getAttribute("adminPassword")

     set 设定var,范围为ActionContext: <s:set var="adminPassword" value="password" scope="session"/>
     set 使用#取值: <s:property value="#adminPassword"/> 
     set 从相应范围取值: <s:property value="#session.adminPassword"/> 
    
      bean 定义bean,并使用param来设定新的属性值:
     <s:bean name="com.test.tags.Dog" >
       <s:param name="name" value="'pp'"></s:param>
       <s:property value="name"/>
    </s:bean>
    <s:bean name="com.test.tags.Dog" var="myDog">
       <s:param name="name" value="'oudy'"></s:param>
    </s:bean>
    <s:property value="#myDog.name"/>
        
    if elseif else: 
    age = <s:property value="#parameters.age[0]" /> <br />
    <s:set var="age" value="#parameters.age[0]" />
    <s:if test="#age < 0">wrong age!</s:if>
    <s:elseif test="#parameters.age[0] < 20">too young!</s:elseif>
    <s:else>yeah!</s:else><br />
    <s:if test="#parameters.aaa == null">null</s:if>

    遍历集合:<br />
    <s:iterator value="{1, 2, 3}" >
    <s:property/> |
    </s:iterator>
    
    自定义变量:
   <s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
    <s:property value="#x.toUpperCase()"/> |
     </s:iterator>
    
    使用status:
   <s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
   <s:property/> | 
     遍历过的元素总数:<s:property value="#status.count"/> |
     遍历过的元素索引:<s:property value="#status.index"/> |
     当前是偶数?:<s:property value="#status.even"/> |
     当前是奇数?:<s:property value="#status.odd"/> |
     是第一个元素吗?:<s:property value="#status.first"/> |
    是最后一个元素吗?:<s:property value="#status.last"/>
   <br />
   </s:iterator>
        
    <s:iterator value="#{1:'a', 2:'b', 3:'c'}" >
    <s:property value="key"/> | <s:property value="value"/> <br />
    </s:iterator>
    

    <s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x">
    <s:property value="#x.key"/> | <s:property value="#x.value"/> <br />
   </s:iterator> 

 

四 struts2高级属性

  

 1、拦截器(Interceptor

         (1)实现 Interceptor接口,实现init(),destory(),intercept()方法

        (2)继承AbstractInterceptor类,覆盖intercept()方法。

        (3)继承MethodFilterInterceptor类,覆盖doIntercept方法

        (4struts.xml文件中的配置

       

<!--EndFragment-->

<!--EndFragment--><!--EndFragment--><!--EndFragment--><!--EndFragment-->

<!--EndFragment-->

<!--EndFragment-->

<!--EndFragment-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值