posts - 13, comments - 7, trackbacks - 0, articles - 0

          導(dǎo)航

          <2007年2月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728123
          45678910

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          action mapping中的attribute 屬性到底是干什么用的, 很早以前一直不太了解,今天突然又想到了這個(gè)問題,感覺不能再扔一邊不管了, 一定要解決它.


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

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


          在struts實(shí)例化actionform的時(shí)候,struts是根據(jù)attribute的值來查找并創(chuàng)建actionform,有兩種情況:如果已經(jīng)存在,那么從內(nèi)存中取回;如果第一次實(shí)例化,那么創(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才會(huì)從name屬性里讀出要?jiǎng)?chuàng)建的formbean 的名字,并創(chuàng)建一下實(shí)例,看下邊的源代碼就知道了, 呵呵.


          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: 關(guān)于struts-config里action元素里的attribute屬性用途的理解  回復(fù)  更多評(píng)論   

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

          # re: 關(guān)于struts-config里action元素里的attribute屬性用途的理解  回復(fù)  更多評(píng)論   

          2008-10-20 22:30 by java Primer
          很好,比起亂七八糟的書籍資料,源代碼的學(xué)習(xí)才是王道
          主站蜘蛛池模板: 乌拉特前旗| 射洪县| 平定县| 安乡县| 通辽市| 开平市| 旬阳县| 嘉义市| 辽宁省| 滨海县| 厦门市| 建宁县| 长泰县| 林口县| 大港区| 静宁县| 威远县| 万安县| 兴隆县| 安多县| 当阳市| 呼伦贝尔市| 巢湖市| 精河县| 苍山县| 新巴尔虎右旗| 新乐市| 邵阳市| 高尔夫| 上饶县| 定日县| 海兴县| 芜湖县| 苍溪县| 门源| 饶阳县| 阿拉善盟| 鄢陵县| 安义县| 江都市| 东丰县|