回想起当年使用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];
privatestaticfinalStringBUNDLE_NAME="appMessages";
privateStringmessageId;
privateObject[]args;
publicAppRuntimeException(StringmessageId){
this(messageId,EMPTY_ARGS);
}
publicAppRuntimeException(StringmessageId,Object[]args){
this(messageId,args,null);
}
publicAppRuntimeException(StringmessageId,Object[]args,Throwablecause){
initCause(cause);
this.messageId=messageId;
this.args=args;
}
publicObject[]getArgs(){
returnargs;
}
publicStringgetMessageId(){
returnmessageId;
}
publicStringgetMessage(){
ResourceBundlebundle=ResourceBundle.getBundle(BUNDLE_NAME);
Stringpattern=bundle.getString(messageId);
returnMessageFormat.format(pattern,args);
}
}
ActionThrowsInterceptor.java
packageexamples.jsf.interceptor;
importjavax.faces.context.FacesContext;
importorg.aopalliance.intercept.MethodInvocation;
importorg.seasar.framework.aop.interceptors.ThrowsInterceptor;
importorg.seasar.jsf.util.MessageUtil;
importexamples.jsf.exception.AppRuntimeException;
publicclassActionThrowsInterceptorextendsThrowsInterceptor{
privatestaticfinallongserialVersionUID=1L;
publicStringhandleThrowable(AppRuntimeExceptionex,MethodInvocationinvocation)throwsThrowable{
FacesContextcontext=FacesContext.getCurrentInstance();
context.addMessage(null,MessageUtil.getErrorMessage(ex.getMessageId(),ex.getArgs()));
returnnull;
}
}
AddLogic.java
packageexamples.jsf.logic;
importexamples.jsf.dto.AddDto;
publicinterfaceAddLogic{
publicintcalculate(AddDtoaddDto);
}
AddLogicImpl.java
packageexamples.jsf.logic.impl;
importexamples.jsf.dto.AddDto;
importexamples.jsf.logic.AddLogic;
publicclassAddLogicImplimplementsAddLogic{
publicAddLogicImpl(){
}
publicintcalculate(AddDtoaddDto){
returnaddDto.getArg1()+addDto.getArg2();
}
}
RequestDumpFilter.java
packageexamples.jsf.util;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
publicfinalclassRequestDumpFilterimplementsFilter{
privatestaticfinalLoglog=LogFactory.getLog(RequestDumpFilter.class);
privateFilterConfigconfig=null;
publicvoidinit(finalFilterConfigfilterConfig)throwsServletException{
config=filterConfig;
}
publicvoiddestroy(){
config=null;
}
privatestaticfinalStringINDENT=" ";
privatestaticfinalStringLF=System.getProperty("line.separator");
publicvoiddoFilter(finalServletRequestrequest,
finalServletResponseresponse,finalFilterChainchain)
throwsIOException,ServletException{
if(config==null){
return;
}
if(requestinstanceofHttpServletRequest){
finalHttpServletRequesthrequest=(HttpServletRequest)request;
log.debug(LF+LF
+"**before*****************************************:"
+gerServletPath(request)+LF+dumpRequest(hrequest));
try{
chain.doFilter(request,response);
}finally{
finalStringBuffersb=newStringBuffer();
RequestDumpUtil.dumpRequestAttributes(sb,hrequest,LF,INDENT);
RequestDumpUtil.dumpCookies(sb,hrequest,LF,INDENT);
RequestDumpUtil.dumpSessionAttributes(sb,hrequest,LF,INDENT);
RequestDumpUtil.dumpContextAttributes(sb,config
.getServletContext(),LF,INDENT);
log
.debug(LF
+LF
+"**after*****************************************:"
+gerServletPath(request)+LF+sb.toString());
}
}else{
chain.doFilter(request,response);
}
}
privateStringdumpRequest(finalHttpServletRequestrequest){
finalStringBuffersb=newStringBuffer();
finalServletContextcontext=config.getServletContext();
RequestDumpUtil.dumpRequestAndContextProperties(sb,request,context,
LF,INDENT);
RequestDumpUtil.dumpRequestParameters(sb,request,LF,INDENT);
RequestDumpUtil.dumpRequestAttributes(sb,request,LF,INDENT);
RequestDumpUtil.dumpCookies(sb,request,LF,INDENT);
RequestDumpUtil.dumpRequestHeaders(sb,request,LF,INDENT);
RequestDumpUtil.dumpSessionAttributes(sb,request,LF,INDENT);
RequestDumpUtil.dumpContextAttributes(sb,context,LF,INDENT);
returnsb.toString();
}
privateStringgerServletPath(finalServletRequestrequest){
if(requestinstanceofHttpServletRequest){
return((HttpServletRequest)request).getServletPath();
}
returnnull;
}
publicStringtoString(){
if(config==null){
return("RequestDumperFilter()");
}
finalStringBuffersb=newStringBuffer("RequestDumperFilter(");
sb.append(config);
sb.append(")");
return(sb.toString());
}
}