西沙璞玉
爱不容易
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 @赵 阅读(391) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
哥哥最近不是很忙