JAVA EE的存档

Callback & Template –Spring

2008-07-21 1:34 am

WRCommons

2008-07-19 1:11 am

纪念一下WRCommons诞生了。

话说OGNL

2008-06-25 8:45 pm

 都说OGNL很强大,强大的无缝不入,无所不能。

早就发现确实很强大,但今天才发现没有传说中的强大。

就说说这个标签吧:<s:iterator var=”" value=”" status=”"/>  ,今天我想做一个动态的selected,很简单就根据action里取来的一个Integer型的数据生成options。 可是value中只认实现了Iterable<T> 接口的那些集合类。 如果是固定的循环得到是好办,比如说我想循环4次那可以这样<s:iterator var=”iter” value=”#{0,1,2,3}” status=”index”/>。问题是我现在不能确定多少次,我只想要一个for循环的功能,可惜强大的OGNL居然不能办到。

老大叫我用js处理,可是想想很多地方要用这个东西,于是写了一个Integer转换成LIST的公用方法,这个方法很简单先for在add,放在action的基类里面。

现在可以在iterator就有了foreach的感觉了~~~

Hibernate 缓存问题

8:22 pm

   今天做项目的时候,碰到了一个Hibernate二级缓存的问题。 在进行一个更新操作后,再想从数据库里取东西就会出现NullPointerException。相当郁闷,明明那个对象有封装了那个id。可是就是取不出来,开始以为是mapping的问题,结果查映射文件,没有问题。而如果不进行那个更新操作,就可以取到id,然后正常的使用那个po。苦苦调试,后来居然还以为是spring事务控制上的问题,苦查无果。

十分钟过后:

原来在UPDATE的时候,因为更新的只是部分数据所有没有把他上级的id保存下来,当然也没有设置hidden。然后我刚好要做的另外一个操作,也是用同一个POJO。这个时候问题就来了,因为update的时候,Hibernian缓存了那个对象。而我在此去取得时候,取得就是缓存的的东西,当然找不到id。

 解决的办法有两种,一种是在update表单的时候加上一个隐藏的字段。

另外一种是在update这个动作最后显示的clear缓存。各有优缺点,从安全性上我选择了第二种。

完了总结一下Hibrnate的缓存机制:

hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但是session关闭的时候,一级缓存就失效了。

二级缓存是SessionFactory级别的全局缓存,我们项目用的是ehcache,所有设置如下:hibernate.cache.provider_class=net.sf.hibernate.cache.EhCacheProvider如果使用查询缓存,加上hibernate.cache.use_query_cache=true 。

 。。。。另外还有Class的缓存,查询缓存,Collection缓存 ,缓存策略太多了。要下班回家了。值得一提的是N+1的问题,有时间再具体写~~~

struts2 Servlet API 解耦

2008-06-13 8:00 pm

struts2提供了三种和servlet解耦的方式:

1: ActionContext  这个接口对应的ServletAPI是HttpServletRequest。其中有两个方法,get(),set()对于于HttpServletRequest的getAttribute(),setAttribute().使用方法为:

ActionContext.getContext().put(key,value);

ActionContext.getContext().get(key);

相当于:

HttpServletRequest.setAttribute(key,value);

HttpServletRequest.getAttribute(key);

另外还直接获得session.   ActionContext.getContext().getSession();

2:是基于IOC的方法实现的,在Action中必须实现ServletRequestAware,ServletResponseAware,ServletSessionAware. 不推荐使用。

3:ServletActionContext继承了ActionContext。

HttpServletRequest request = ServletActionContext.getRequest();

HttpServletResponse response = ServletActionContext.getPesponse();

HttpSession session = request.getSession();

总结:第二种方法因为要实现接口不推荐使用,一般如果要获得session,用第一种方法。但是ActionContext不能获得request和response所有要用第三种。。。

Exadel Flamingo - Flex with Seam and Spring

2008-06-09 11:46 am

The current state and direction of the software development seems to be going towards agile approaches.  This is mostly a resulted mostly from resurgence of the Internet itself with novel applications supporting Web 2.0 principles. In order to provide a quick response to market demands, the agile development approach seems to be displacing others as being the most efficient and effective. While agile development is not technology specific, there are technologies which are more conducive to agile development than others. Quite a few technologies exist for rapid development of traditional applications, but nothing to aid creation of rich internet application.

官方网站:http://www.exadel.com/web/portal/flamingo

开始关注这个框架!

webwork2 architecture

2008-05-30 10:11 am

整理电脑的时候,翻出一本很久以前下的书,匆匆看了一下。

snap1.gif

struts2 Hibernate3 spring2整合出现的问题

2008-05-20 12:36 pm

1. 兼容的问题,在家用的jdk6.0的,公司的jdk没有更新用的还是5.0,结果出了一个很恶心的错误。。。更新jdk.
2.然后就是struts2  的action在spring中的一个托管问题,在struts.xml中配置action的class的时候,把包名也加上去了,这样的话struts2就不会用到spring容器,那struts2就会直接去找那个actopm而不是通过Spring的BeanFactory,也就是说bean不在控制范围之内,当然Spring中声名显赫的IOC和AOP就不能用了。。结果在Service中用注入DAO的时候就失效了,抛出一个NullPointerException的异常。

3.还是struts2和spring整合的问题,配置action要加上namespace,不能用默认的否则要加上action后缀。

总结:因为spring本身很好的整合了Hibernagte,所有基本上在持久层上不会出现整合上的问题,对于struts2和spring会出现一些问题,自己太粗心没有注意。。。

struts2 文件上传问题

2008-05-07 9:53 am

今天碰到一个小问题。忘记在web.xml 配置struts2-cleanup了,如果要用到上传,这个一定要配置上。。。

 <filter >
        < filter-name > struts-cleanup </ filter-name >
        < filter-class >
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </ filter-class >
    </ filter > 
    
< filter-mapping >
        < filter-name > struts-cleanup </ filter-name >
        < url-pattern > /* </ url-pattern >
    </ filter-mapping >

这样,那个未知的错误就没有了。。

struts2拦截器学习心得

2008-05-06 4:04 pm

Struts2的拦截器是基于AOP思想的,采取可配置的方式控制(可插销式)。 拦截器的使用解耦了对servlet的依赖,不在依赖Servlet API。而Struts1.x Action中的execut中有HttpServletRequest和HttpSerletResponse方法。对于Struts2的Action来说其实就是一个POJO。。。

话题回到拦截器,Strut.xml中可以定义拦截器,也可以定义一个拦截器栈,格式是:

<interceptors>

<interceptor name=”"/>

<interceptor-stack name=”">

<interceptor-ref name=”"/>

</interceptor-stack>

</interceptors>

配置好之后就可以在具体的Action引用改拦截器,引用的格式是<interceptor-ref name=”"> </interceptor-ref>在定义和引用的时候都可以配置默认的参数<param name=”">value</param> 值得注意的是如果在定义和引用的时候都配置了相同的参数,那么Action中的引用参数将会覆盖默认的配置。 还有一个要注意的地方是,如果Action中指定了自己要用的拦截器,那么就必须显示的指定默认的拦截器<interceptor-ref name=”defalutstack”/>。 有的拦截器是每个Action都要用到的,那么一个个去配置的话,就显得太麻烦了,也不符合软件复用这个指导思想。这个时候可以配置包下默认拦截器,既可以是拦截器也可以是拦截器栈,<default-interceptor-ref name=”"/> 也可以在配置参数param。这样的话这些拦截器就应用到了包中的每一个Action。虽然Struts2中的拦截器基本是帮我们完成了70%的拦截任务,但是我们很多的时候还是要自己写一个拦截器的。

怎么写一个拦截器呢,庆幸的是在struts2中这是一件简单而且愉快的事情。

首先要实现com.opensymphony.xwork2.interceptor.Interceptor接口。

public Interface Interceptor extends Serializable {

//销毁该拦截器之前的回调方法

void destory();

//初始化该拦截器的回掉方法(主要是打开一些资源)

void init();

// 拦截器实现拦截的逻辑方法

String intercept(ActionInvocation invocation) throws Exception;

}

intercept这个方法是我们需要实现的拦截动作。就像Action的execute一样,intercept方法会返回一个字符串的逻辑视图。ActionInvocation参数包含了被拦截的Action的应用,调用invoke方法,将控制权交给下一个拦截器,或者转个Action的execute方法。

跟java的API一样,大家都知道很多时候一个接口对应的会有一个抽象类实现这个接口,然后在抽象类提供接口一些方法的空实现,这样在我们在用的时候,就可以直接继承那个abstract的类,不用在实现那些我们不要的方法。 那么Struts2也提供了这样的一个方法AstractIntercepor类,这个类对init()和destory()方法进行了空实现,以为并不是我们每次实现拦截器的时候都要申请资源。ActionInvocation可以做的事情很多,我复制一段API

Method Summary
void addPreResultListener(PreResultListener listener)
Register a com.opensymphony.xwork2.interceptor.PreResultListener to be notified after the Action is executed and before the Result is executed.
Object getAction()
Get the Action associated with this ActionInvocation
ActionContext getInvocationContext()
Gets the ActionContext associated with this ActionInvocation.
ActionProxy getProxy()
Get the ActionProxy holding this ActionInvocation
Result getResult()
If the ActionInvocation has been executed before and the Result is an instance of ActionChainResult, this method will walk down the chain of ActionChainResults until it finds a non-chain result, which will be returned.
String getResultCode()
Gets the result code returned from this ActionInvocation
ValueStack getStack()

String invoke()
Invokes the next step in processing this ActionInvocation.
String invokeActionOnly()
Invokes only the action (not interceptors or results).
boolean isExecuted()
阅读全文…