`

Play framework 2.0 -应用程序全局设置

 
阅读更多

#应用程序全局设置

 

1.全局对象

 

在工程中定义全局对象可以允许你操作你的应用程序的全局设置。这个全局对象必须定义在根包下。

import play.*;

	public class Global extends GlobalSettings {

	}
 

 

2.应用程序启动和关闭的拦截

 

你可以重写onStart和onStop操作,通知对应的应用程序命周期事件。

import play.*;

	public class Global extends GlobalSettings {

	  @Override
	  public void onStart(Application app) {
	    Logger.info("Application has started");
	  }  
	  
	  @Override
	  public void onStop(Application app) {
	    Logger.info("Application shutdown...");
	  }  
	    
	}
 

 

3.提供一个应用程序错误页

 

当你的程序有异常发生时,onError操作会被调用。默认使用框架内部错误页,你可以重写它:

 

import play.*;
import play.mvc.*;

	import static play.mvc.Results.*;

	public class Global extends GlobalSettings {

	  @Override
	  public Result onError(Throwable t) {
	    return internalServerError(
	      views.html.errorPage(t)
	    );
	  }  
	    
	}
 

 

4.处理action未找到

 

当框架找不到请求的action时,onActionNotFound操作会被调用:

import play.*;
	import play.mvc.*;

	import static play.mvc.Results.*;

	public class Global extends GlobalSettings {

	  @Override
	  public Result onActionNotFound(String uri) {
	    return notFound(
	      views.html.pageNotFound(uri)
	    );
	  }  
	    
	}
 

如果路由能找到,但是不能绑定请求参数时,onBadRequest操作会被调用。

 


import play.*;
	import play.mvc.*;

	import static play.mvc.Results.*;

	public class Global extends GlobalSettings {

	  @Override
	  public Result onBadRequest(String uri, String error) {
	    return badRequest("Don't try to hack the URI!");
	  }  
	    
	}
 

#拦截请求

 

1.重新onRequest

 

GlobalSettings类的一种重要概念是它提供了在请求被分发到action之前拦截请求,执行业务逻辑的方法。例如:

import play.*;

	public class Global extends GlobalSettings {

	@Override
	public Action onRequest(Request request, Method actionMethod) {
	   System.out.println("before each request..." + request.toString());
	   return super.onRequest(request, actionMethod);
	}

	}
 

 

通过Action组件,也可以实现对指定Action方法的拦截。


1
1
分享到:
评论
2 楼 zhouytao 2015-07-26  
string2020 写道
public class ApplicationContext extends GlobalSettings 
{
	 public void beforeStart(Application app)
	 {
		 System.out.println("============beforeStart=============Application has beforeStart");
	 }
	
	 public void onStart(Application app) 
	 {
	     System.out.println("============onStart===========Application has started");
	 }

	 public void onStop(Application app)
	 {
		 System.out.println("============onStop=============Application shutdown...");
	 }
	 
	 public Configuration onLoadConfig(Configuration config, File path, ClassLoader classloader) 
	 {
		 System.out.println("=======onLoadConfig========> " + config.asMap());
	     return null;
	 }
	 
	 public F.Promise<SimpleResult> onBadRequest(RequestHeader request, String error) 
	 {
		 System.out.println("=========================Application onBadRequest...");
	     return null;
	 }
	 
	 public F.Promise<SimpleResult> onHandlerNotFound(RequestHeader request) 
	 {
		 System.out.println("=========================Application onHandlerNotFound  ...");
	     return null;
	 }

	 public Action onRequest(Request request, Method actionMethod)
	 {
	        System.out.println("before each request..." + request.toString());
	        return super.onRequest(request, actionMethod);
	 }
}




没有package
我这样写了一个全局设置,但是一直没有执行。

请问,为什么


你需要在你的application.conf中加上application.global=Global
1 楼 string2020 2014-04-26  
public class ApplicationContext extends GlobalSettings 
{
	 public void beforeStart(Application app)
	 {
		 System.out.println("============beforeStart=============Application has beforeStart");
	 }
	
	 public void onStart(Application app) 
	 {
	     System.out.println("============onStart===========Application has started");
	 }

	 public void onStop(Application app)
	 {
		 System.out.println("============onStop=============Application shutdown...");
	 }
	 
	 public Configuration onLoadConfig(Configuration config, File path, ClassLoader classloader) 
	 {
		 System.out.println("=======onLoadConfig========> " + config.asMap());
	     return null;
	 }
	 
	 public F.Promise<SimpleResult> onBadRequest(RequestHeader request, String error) 
	 {
		 System.out.println("=========================Application onBadRequest...");
	     return null;
	 }
	 
	 public F.Promise<SimpleResult> onHandlerNotFound(RequestHeader request) 
	 {
		 System.out.println("=========================Application onHandlerNotFound  ...");
	     return null;
	 }

	 public Action onRequest(Request request, Method actionMethod)
	 {
	        System.out.println("before each request..." + request.toString());
	        return super.onRequest(request, actionMethod);
	 }
}




没有package
我这样写了一个全局设置,但是一直没有执行。

请问,为什么

相关推荐

Global site tag (gtag.js) - Google Analytics