posts - 13, comments - 7, trackbacks - 0, articles - 0
          action mapping中的attribute 屬性到底是干什么用的, 很早以前一直不太了解,今天突然又想到了這個問題,感覺不能再扔一邊不管了, 一定要解決它.


          這是在網(wǎng)上查到的關于attribute的解釋.
          1)應用前提,attribute只有在設置了name后才有意義。
          2)attribute可以實現(xiàn)對象的重用,即如果設置了attribute屬性,在創(chuàng)建actionform是,會先去查找相應的scope中是否有此對象,如果有,則重用,否則創(chuàng)建新的對象。
          3)當你將創(chuàng)建的acitonForm保存到相應的scope中時,你想用一個更有意義的名字來訪問它時,它就有意義了。

          可是,看到"一個更有意義的名字的時候", 我好像有點理解了
          <action
          attribute="newLoginForm"
          name="loginForm"
          type="loginAction"
          scope="request"
          path="/login">


          在struts實例化actionform的時候,struts是根據(jù)attribute的值來查找并創(chuàng)建actionform,有兩種情況:如果已經(jīng)存在,那么從內(nèi)存中取回;如果第一次實例化,那么創(chuàng)建,并放入內(nèi)存。

          org.apache.struts.util.RequestUtils中的源代碼

          ???public static Actionform createActionform(
          ? ? ? ?HttpServletRequest request,
          ? ? ? ?ActionMapping mapping,
          ? ? ? ?ModuleConfig moduleConfig,
          ? ? ? ?ActionServlet servlet) {
          ?????? ............
          ??????............

          ???????String attribute = mapping.getAttribute();
          ??????......
          ???????Actionform instance = null;
          ? ? ? ?HttpSession session = null;

          ???????if ("request".equals(mapping.getScope())) {
          ? ? ? ? ? ?instance = (Actionform) request.getAttribute(attribute);
          ? ? ? ?} else {
          ? ? ? ? ? ?session = request.getSession();
          ? ? ? ? ? ?instance = (Actionform) session.getAttribute(attribute);
          ? ? ? ?}
          ??????................
          ????????................
          ???????
          ???????}
          ???????
          如果沒有配置attribute屬性的話, struts才會從name屬性里讀出要創(chuàng)建的formbean 的名字,并創(chuàng)建一下實例,看下邊的源代碼就知道了, 呵呵.


          org.apache.struts.config.ActionConfig

          ???protected String attribute = null;

          ? ?public String getAttribute() {
          ?//就是這里了.
          ???????if (this.attribute == null) {
          ? ? ? ? ? ?return (this.name);
          ? ? ? ?} else {
          ? ? ? ? ? ?return (this.attribute);
          ? ? ? ?}
          ? ?}

          ? ?public void setAttribute(String attribute) {
          ? ? ? ?if (configured) {
          ? ? ? ? ? ?throw new IllegalStateException("Configuration is frozen");
          ? ? ? ?}
          ? ? ? ?this.attribute = attribute;
          ? ?}

          Feedback

          # re: 關于struts-config里action元素里的attribute屬性用途的理解  回復  更多評論   

          2008-08-07 16:58 by chenhong
          好厲害!

          # re: 關于struts-config里action元素里的attribute屬性用途的理解  回復  更多評論   

          2008-10-20 22:30 by java Primer
          很好,比起亂七八糟的書籍資料,源代碼的學習才是王道
          主站蜘蛛池模板: 阳原县| 清苑县| 无为县| 巢湖市| 临海市| 汉中市| 刚察县| 临夏市| 江城| 宣汉县| 东兴市| 郯城县| 探索| 芷江| 林州市| 灵璧县| 麟游县| 汪清县| 新兴县| 朝阳区| 谷城县| 精河县| 东平县| 蒙山县| 大足县| 甘泉县| 石楼县| 夏津县| 泸溪县| 赤水市| 雷波县| 乡城县| 儋州市| 揭东县| 虞城县| 大安市| 海门市| 三原县| 遂平县| 山西省| 大兴区|