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

          --

          項目基本架構(gòu) 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找不到對應(yīng)的bean id,其實沒有對應(yīng)關(guān)系,注解標簽會自動注入類型為AppUserServiceBean,根據(jù)實現(xiàn)類可以找到)

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



          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"/> (構(gòu)造器注入,多個參數(shù)從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時候可以省略掉父類已經(jīng)注入的屬性


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


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

          <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連接數(shù)據(jù)庫


          protected JdbcTemplate jdbcTemplate;





          (jdbcTemplatebean id 相對應(yīng))



          public
          void setJdbcTemplate(JdbcTemplate jdbcTemplate) {



          this.jdbcTemplate
          = jdbcTemplate;



          }





          public
          void setPersistType(Class persistType) {



          this.persistType
          = persistType;



          }





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



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



          獲取系統(tǒng)配置的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";
          成功跳轉(zhuǎn)頁面





          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";
          成功跳轉(zhuǎn)頁面





          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());



          從這里重新設(shè)置json



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



          /**



          * 顯示詳細信息



          * @return



          */



          public String get(){



          AppRole appRole=appRoleService.get(roleId);



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



          //將數(shù)據(jù)轉(zhuǎn)成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 @趙 閱讀(397) 評論(0)  編輯  收藏

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


          網(wǎng)站導航:
           
          哥哥最近不是很忙
          主站蜘蛛池模板: 新宾| 万山特区| 宜宾市| 平远县| 长岭县| 武川县| 南部县| 富锦市| 浮梁县| 体育| 海林市| 蕉岭县| 和硕县| 曲周县| 博客| 泽州县| 海盐县| 彩票| 嘉义县| 大兴区| 淳化县| 华宁县| 璧山县| 宁德市| 荣成市| 天祝| 台山市| 油尖旺区| 漳浦县| 神池县| 宁强县| 行唐县| 襄城县| 大安市| 永仁县| 科技| 奉贤区| 临邑县| 绥滨县| 通城县| 南丹县|