日期:2014-05-16 浏览次数:20415 次
回想起当年使用struts的时候,对他的tag真是又爱又恨。盼星星盼月亮,迎来了JSF。
然而,JSF的tag库虽然强大,却失去了灵活性,反而让人望而生畏。
S2JSF在通过seasar2的DI功能,把JSF标签注入到HTML页面,实现了类似模板引擎的功能。
示例如下:
如下图所示新建工程:
代码依次为:
AddAction.java
packageexamples.jsf.action;
publicinterfaceAddAction{
publicStringcalculate();
}
AddActionImpl.java
packageexamples.jsf.action.impl;
importexamples.jsf.action.AddAction;
importexamples.jsf.dto.AddDto;
importexamples.jsf.logic.AddLogic;
publicclassAddActionImplimplementsAddAction{
privateAddDtoaddDto;
privateAddLogicaddLogic;
publicvoidsetAddDto(AddDtoaddDto){
this.addDto=addDto;
}
publicvoidsetAddLogic(AddLogicaddLogic){
this.addLogic=addLogic;
}
publicStringcalculate(){
intresult=addLogic.calculate(addDto);
addDto.setResult(result);
returnnull;
}
}
allaop.dicon
<?xmlversion="1.0"encoding="Shift_JIS"?>
<!DOCTYPEcomponentsPUBLIC"-//SEASAR2.1//DTDS2Container//EN"
"http://www.seasar.org/dtd/components21.dtd">
<components>
<includepath="aop.dicon"/>
<includepath="dao.dicon"/>
<includepath="j2ee.dicon"/>
<component
name="actionThrowsInterceptor"
class="examples.jsf.interceptor.ActionThrowsInterceptor"
/>
<component
name="actionInterceptorChain"
class="org.seasar.framework.aop.interceptors.InterceptorChain"
>
<initMethodname="add"><arg>aop.traceInterceptor</arg></initMethod>
<initMethodname="add"><arg>actionThrowsInterceptor</arg></initMethod>
</component>
<component
name="logicInterceptorChain"
class="org.seasar.framework.aop.interceptors.InterceptorChain"
>
<initMethodname="add"><arg>aop.traceThrowsInterceptor</arg></initMethod>
<initMethodname="add"><arg>aop.traceInterceptor</arg></initMethod>
<initMethodname="add"><arg>j2ee.requiredTx</arg></initMethod>
</component>
<component
name="daoInterceptorChain"
class="org.seasar.framework.aop.interceptors.InterceptorChain"
>
<initMethodname="add"><arg>aop.traceInterceptor</arg></initMethod>
<initMethodname="add"><arg>dao.interceptor</arg></initMethod>
</component>
</components>
?
AddDto.java
packageexamples.jsf.dto;
importjava.io.Serializable;
publicclassAddDtoimplementsSerializable{
privatestaticfinallongserialVersionUID=1L;
privateintarg1;
privateintarg2;
privateintresult;
publicAddDto(){
}
publicintgetArg1(){
returnarg1;
}
publicvoidsetArg1(intarg1){
this.arg1=arg1;
}
publicintgetArg2(){
returnarg2;
}
publicvoidsetArg2(intarg2){
this.arg2=arg2;
}
publicintgetResult(){
returnresult;
}
publicvoidsetResult(intresult){
this.result=result;
}
}
HelloDto.java
packageexamples.jsf.dto;
publicclassHelloDto{
privateStringname="S2JSF";
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
AppRuntimeException.java
packageexamples.jsf.exception;
importjava.text.MessageFormat;
importjava.util.ResourceBundle;
publicclassAppRuntimeExceptionextendsRuntimeException{
privatestaticfinallongserialVersionUID=1L;
privatestaticfinalObject[]EMPTY_ARGS=newObject[0];<