西沙璞玉
          愛不容易
          posts - 0,comments - 4,trackbacks - 0

          --

          項目基本架構 ext+action+service+dao+josn



          1,action


          spring<bean id="LoginAction"
          class="com.htsoft.oa.action.system.LoginAction"
          scope="prototype"/>


          struts <action name="login"
          class="LoginAction" method="login">


          classbean id相同


          下面是注入的service


          struts @Resource


          private
          AppUserService userService;
          (這個userService找不到對應的bean id,其實沒有對應關系,注解標簽會自動注入類型為AppUserServiceBean,根據實現類可以找到)

          @Resource(注解方式注入,默認是type類型相同,也可以用name=“”與 bean id 對應)



          private
          SysConfigService sysConfigService;


          spring




          <bean id="appUserService" class="com.htsoft.oa.service.system.impl.AppUserServiceImpl"> <constructor-arg index="0"ref="appUserDao"/></bean>




          <bean id="sysConfigService"class="com.htsoft.oa.service.system.impl.SysConfigServiceImpl"><constructor-arg index="0"ref="sysConfigDao"/>


          </bean>



          2service



          spring <beanid="appUserService"class="com.htsoft.oa.service.system.impl.AppUserServiceImpl">


          <constructor-arg index="0" ref="appUserDao"/> (構造器注入,多個參數從0開始)

          </bean>



          <bean id="indexDisplayService"class="com.htsoft.oa.service.system.impl.IndexDisplayServiceImpl">



          <constructor-arg index="0" ref="indexDisplayDao"/>

          </bean>

          <bean id="appRoleService"class="com.htsoft.oa.service.system.impl.AppRoleServiceImpl">


          <constructor-arg
          index="0" ref="appRoleDao"/>
          </bean>



          struts



          @Resource



          IndexDisplayService
          indexDisplayService;


          @Resource



          AppRoleService
          appRoleService;


          public AppUserServiceImpl(AppUserDao dao) {

          super(dao);

          this.dao
          = dao;


          }



          3dao



          spring


          <bean id="appUserDao"
          class="com.htsoft.oa.dao.system.impl.AppUserDaoImpl"
          parent="baseDao"/>

          parent

          表示繼承的父類如果有很多繼承同一個父類的BEAN


          那么在配置文件中實例那些BEAN時候可以省略掉父類已經注入的屬性


          bean定義繼承父bean定義,它可以覆蓋父bean的一些值,或者它需要的值。


          那么在配置文件中實例那些BEAN時候可以省略掉父類已經注入的屬性

          <bean id="baseDao"
          abstract="true" class="com.htsoft.core.dao.impl.BaseDaoImpl"
          parent="genericDao"/>



          <bean id="genericDao"
          abstract="true"
          class="com.htsoft.core.dao.impl.GenericDaoImpl">



          <property
          name="jdbcTemplate" ref="jdbcTemplate"/>(getter
          setter方式注入)


          <property name="sessionFactory"
          ref="sessionFactory"/>



          </bean>

          abstract="true" 抽象bean,作為父類 ,子類bean parent



          app-resources.xml



          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

          <property name="dataSource" ref="dataSource"/>

          </bean>


          struts



          GennericDaoImpl.java 這個類選用了springjdbcTemplate連接數據庫


          protected JdbcTemplate jdbcTemplate;





          (jdbcTemplatebean id 相對應)



          public
          void setJdbcTemplate(JdbcTemplate jdbcTemplate) {



          this.jdbcTemplate
          = jdbcTemplate;



          }





          public
          void setPersistType(Class persistType) {



          this.persistType
          = persistType;



          }





          用注解標簽,看不到依賴關系,沒有顯示注入明了



          /////////////////////////////////////////////////////////////////////////////



          獲取系統配置的bean 這里的sysConfigService是個bean id的名字 (目錄查找方式)





          private static ApplicationContext
          appContext;



          下面的getBean封裝了這個appContext.getBean(beanId)這個可以獲得bean實例





          public
          void setApplicationContext(ApplicationContext applicationContext) throws
          BeansException {



          this.appContext=applicationContext;



          }





          public static void reloadSysConfig(){



          //configMap.clear();



          SysConfigService
          sysConfigService=(SysConfigService)getBean("sysConfigService");



          List<SysConfig>
          list=sysConfigService.getAll();



          for(SysConfig
          conf:list){



          configMap.put(conf.getConfigKey(),conf.getDataValue());



          }



          }



          //////////////////////////////////////////



          <action name="*SalesChance"
          class="SalesChanceAction" method="{1}">



          <result>${successResultValue}</result>



          <result
          name="input">/error.jsp </result>



          *號是通配符,就是說這個actionname為任意名稱。而class中的{1}是取第一個通配符的值。



          ${successResultValue}BaseAction.java



          private String
          successResultValue="/jsonString.jsp";
          成功跳轉頁面





          jsonString.jsp頁面
          <s:property value="jsonString" escape="false" />









          action到返回頁面的配置:



          <action name="*SalesChance"
          class="SalesChanceAction" method="{1}">



          <result>${successResultValue}</result>



          <result
          name="input">/error.jsp </result>



          *號是通配符,就是說這個actionname為任意名稱。而class中的{1}是取第一個通配符的值。



          ${successResultValue}



          BaseAction.java



          private String
          successResultValue="/jsonString.jsp";
          成功跳轉頁面





          jsonString.jsp頁面



          <s:if test="#request.isExport==null
          || #request.isExport==false"
          >



          <s:property value="jsonString" escape="false" />



          </s:if>





          protected String jsonString=JSON_SUCCESS;



          public static final String JSON_SUCCESS="{success:true}";





          具體action



          msg.append(",failure:true}");



          setJsonString(msg.toString());



          jsonString="{success:false,msg:'該用戶賬號不存在!'}";





          setJsonString(msg.toString());



          從這里重新設置json



          Gson
          是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。可以將一個 JSON 字符串轉成一個 Java 對象,或者反過來。



          /**



          * 顯示詳細信息



          * @return



          */



          public String get(){



          AppRole appRole=appRoleService.get(roleId);



          Gson gson=new
          GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();



          //將數據轉成JSON格式



          StringBuffer sb = new StringBuffer("{success:true,data:");



          sb.append(gson.toJson(appRole));



          sb.append("}");



          setJsonString(sb.toString());





          return SUCCESS;



          }









          posted on 2012-05-04 13:50 @趙 閱讀(395) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          哥哥最近不是很忙
          主站蜘蛛池模板: 灯塔市| 遂平县| 上栗县| 河西区| 北海市| 黎平县| 孝昌县| 淮安市| 怀柔区| 会宁县| 麻阳| 湘阴县| 延庆县| 佛学| 交口县| 随州市| 德令哈市| 宽甸| 乌兰县| 平昌县| 石阡县| 辉县市| 宁武县| 宽甸| 铜鼓县| 孟津县| 涡阳县| 镇赉县| 昌都县| 仪征市| 固阳县| 婺源县| 永德县| 翼城县| 常德市| 弋阳县| 枝江市| 宁明县| 克拉玛依市| 洱源县| 宜兰县|