tapestry+spring+hibernate 用maven整合(二:tapestry篇)
2011-09-26 16:50
295 查看
tapestry要设置的主要有:
final Logger logger, final ResponseRenderer renderer,
final ComponentSource componentSource) {
return new RequestExceptionHandler() {
public void handleRequestException(Throwable exception)
throws IOException {
logger.error(
"Unexpected runtime exception: "
+ exception.getMessage(), exception);
ExceptionReporter exceptionReport = (ExceptionReporter) componentSource
.getPage("ExceptionReport");
if(exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null&&exception.getCause().getCause().getCause()!=null){
exceptionReport.reportException(exception.getCause().getCause().getCause());
}else if(exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null){
exceptionReport.reportException(exception.getCause().getCause());
}else{
exceptionReport.reportException(exception.getCause());
}
renderer.renderPageMarkupResponse("ExceptionReport");
}
};
}
public void contributeServiceOverride(
MappedConfiguration<Class, Object> configuration,
@Local RequestExceptionHandler handler) {
configuration.add(RequestExceptionHandler.class, handler);
}它会捕获所有未处理的异常,首先它用logger.error打印异常到log中,log4j相关配置:
log4j.logger.com.vision.rcp.web.weborder.services.AppModule.AppRequestExceptionHandler=DEBUG,exception
log4j.appender.exception=org.apache.log4j.RollingFileAppender
log4j.appender.exception.file=d:/exception.log
log4j.appender.exception.MaxFileSize=500KB
log4j.appender.exception.MaxBackupIndex=5
log4j.appender.exception.layout=org.apache.log4j.PatternLayout
log4j.appender.exception.layout.ConversionPattern=%d %5p %c{2}:%L - %m%n这个是为了找到用户自己封装好的异常:
exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null&&exception.getCause().getCause().getCause()!=null
因为用户封装好的异常会被tapestry封装好几层。
显示异常页面:在page目录下新建异常页面ExceptionReport ,java如下:
public class ExceptionReport implements ExceptionReporter {
@Property
private Throwable exception;
@Property
private ProjectException exceptionTrue;
public void reportException(Throwable exception) {
this.exception = exception;
if(exception instanceof HibernateOptimisticLockingFailureException){
//事务回滚错误
exceptionTrue = new ProjectException(ExceptAll.Project_00006,exception.getCause());
}else if (exception instanceof ProjectException) {
exceptionTrue = (ProjectException) exception;
}
}
}tml文件
<display-name>weborder Tapestry 5 Application</display-name>
<context-param>
<!-- The only significant configuration for Tapestry 5, this informs Tapestry
of where to look for pages, components and mixins. -->
<param-name>tapestry.app-package</param-name>
<param-value>com.vision.rcp.web.weborder</param-value>
</context-param>
<context-param><!--spring的配置 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/config/*.xml
</param-value>
</context-param>
<filter>
<filter-name>app</filter-name>
<filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>app</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.16</version>
<configuration>
<!-- Log to the console. -->
<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
<!-- This doesn't do anything for Jetty, but is a workaround for a Maven
bug that prevents the requestLog from being set. -->
<append>true</append>
</requestLog>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
<contextPath>/</contextPath>
<scanIntervalSeconds>10</scanIntervalSeconds>
<jettyEnvXml>src/main/resources/jetty-env.xml</jettyEnvXml>
</configuration>
</plugin>
一、AppModule.app配置异常管理:
public RequestExceptionHandler buildAppRequestExceptionHandler(final Logger logger, final ResponseRenderer renderer,
final ComponentSource componentSource) {
return new RequestExceptionHandler() {
public void handleRequestException(Throwable exception)
throws IOException {
logger.error(
"Unexpected runtime exception: "
+ exception.getMessage(), exception);
ExceptionReporter exceptionReport = (ExceptionReporter) componentSource
.getPage("ExceptionReport");
if(exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null&&exception.getCause().getCause().getCause()!=null){
exceptionReport.reportException(exception.getCause().getCause().getCause());
}else if(exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null){
exceptionReport.reportException(exception.getCause().getCause());
}else{
exceptionReport.reportException(exception.getCause());
}
renderer.renderPageMarkupResponse("ExceptionReport");
}
};
}
public void contributeServiceOverride(
MappedConfiguration<Class, Object> configuration,
@Local RequestExceptionHandler handler) {
configuration.add(RequestExceptionHandler.class, handler);
}它会捕获所有未处理的异常,首先它用logger.error打印异常到log中,log4j相关配置:
log4j.logger.com.vision.rcp.web.weborder.services.AppModule.AppRequestExceptionHandler=DEBUG,exception
log4j.appender.exception=org.apache.log4j.RollingFileAppender
log4j.appender.exception.file=d:/exception.log
log4j.appender.exception.MaxFileSize=500KB
log4j.appender.exception.MaxBackupIndex=5
log4j.appender.exception.layout=org.apache.log4j.PatternLayout
log4j.appender.exception.layout.ConversionPattern=%d %5p %c{2}:%L - %m%n这个是为了找到用户自己封装好的异常:
exception!=null&&exception.getCause()!=null&&exception.getCause().getCause()!=null&&exception.getCause().getCause().getCause()!=null
因为用户封装好的异常会被tapestry封装好几层。
显示异常页面:在page目录下新建异常页面ExceptionReport ,java如下:
public class ExceptionReport implements ExceptionReporter {
@Property
private Throwable exception;
@Property
private ProjectException exceptionTrue;
public void reportException(Throwable exception) {
this.exception = exception;
if(exception instanceof HibernateOptimisticLockingFailureException){
//事务回滚错误
exceptionTrue = new ProjectException(ExceptAll.Project_00006,exception.getCause());
}else if (exception instanceof ProjectException) {
exceptionTrue = (ProjectException) exception;
}
}
}tml文件
<t:if test="exceptionTrue"> <strong>${exceptionTrue.errorMessage}</strong> <p:else> <strong>${exception.message}</strong> </p:else> </t:if>
二:web.xml配置
<web-app><display-name>weborder Tapestry 5 Application</display-name>
<context-param>
<!-- The only significant configuration for Tapestry 5, this informs Tapestry
of where to look for pages, components and mixins. -->
<param-name>tapestry.app-package</param-name>
<param-value>com.vision.rcp.web.weborder</param-value>
</context-param>
<context-param><!--spring的配置 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/config/*.xml
</param-value>
</context-param>
<filter>
<filter-name>app</filter-name>
<filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>app</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
三:jetty插件配置
<plugin><groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.16</version>
<configuration>
<!-- Log to the console. -->
<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
<!-- This doesn't do anything for Jetty, but is a workaround for a Maven
bug that prevents the requestLog from being set. -->
<append>true</append>
</requestLog>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
<contextPath>/</contextPath>
<scanIntervalSeconds>10</scanIntervalSeconds>
<jettyEnvXml>src/main/resources/jetty-env.xml</jettyEnvXml>
</configuration>
</plugin>
相关文章推荐
- tapestry+spring+hibernate 用maven整合(一:maven篇)
- maven+Spring+Struts2+Hibernate 整合
- struts2+hibernate-jpa+Spring+maven 整合(1)
- 用maven整合struts+spring+hibernate之四 添加事务管理
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-5]
- maven+springMVC+hibernate整合
- struts2+hibernate-jpa+Spring+maven 整合(2)
- Maven整合Spring,Spring mvc4.1.6,Hibernate4.3.10项目搭建总结
- 用Maven整合SpringMVC+Spring+Hibernate 框架
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十四:hibernate关于缓冲机制的巨坑!
- Maven 整合 SpringMvc Spring Hibernate +oracle
- spring4 hibernate4 struts2 maven的整合
- Spring+JPA+Hibernate+Junit Test + maven Demo 整合配置
- Maven中整合Struts2、Spring3与Hibernate
- (Spring)springdata、spring、jpa、hibernate、springsecurity、maven整合
- Maven学习笔记(九)-Maven整合SSH(Spring+SpringMVC+Hibernate)框架
- Spring+SpringMVC+Hibernate+Maven的整合
- [置顶] maven 整合spring+springmvc+hibernate
- SpringMVC+Hibernate 项目开发之二 (STS整合Maven)
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十:Spring mvc & hibernate 事务处理(回滚)