項目基本架構 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">
class與bean id相同
下面是注入的service
struts @Resource
private
AppUserService userService;(這個userService找不到對應的bean id,其實沒有對應關系,注解標簽會自動注入類型為AppUserService的Bean,根據實現類可以找到)
@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>
2,service層
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;
}
3,dao層
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 這個類選用了spring的jdbcTemplate連接數據庫
protected JdbcTemplate jdbcTemplate;
(jdbcTemplate與bean 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>
*號是通配符,就是說這個action的name為任意名稱。而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>
*號是通配符,就是說這個action的name為任意名稱。而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
@趙 閱讀(394)
評論(0) 編輯 收藏