tapestry+spring+hibernate 用maven整合(二:tapestry篇)

tapestry要设置的主要有:

一、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>



发布了20 篇原创文章 · 获赞 1 · 访问量 3920
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览