日系框架之seasar2(S2JSF)---(上)

本文介绍了如何通过S2JSF和seasar2的DI功能,将JSF标签注入到HTML页面,从而实现类似于模板引擎的功能。通过示例代码展示了从创建Action类、Action实现类、DTO类、异常处理类到配置文件的整合过程。
摘要由CSDN通过智能技术生成

回想起当年使用struts的时候,对他的tag真是又爱又恨。盼星星盼月亮,迎来了JSF。

  然而,JSF的tag库虽然强大,却失去了灵活性,反而让人望而生畏。

  S2JSF在通过seasar2的DI功能,把JSF标签注入到HTML页面,实现了类似模板引擎的功能。

  示例如下:

  如下图所示新建工程:

日系框架之seasar2(S2JSF)---(上)

日系框架之seasar2(S2JSF)---(上)

  代码依次为:

  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());
  }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值