0
我想創建一個全局變量,所有控制器都可以訪問。爲此,我創建了一個FrontController
類,它從Controller
延伸。我所有常用的控制器都是從這個FrontController
延伸出來的。在PlayFramework中訪問HTTP上下文
現在我想創建一個變量country
在FrontController
這是基於主機設置。我試圖從當前的請求中獲取這些信息。
我現在的問題是:如何訪問當前的HTTP上下文?
package controllers;
import play.mvc.Controller;
public class FrontController extends Controller {
// Country-Code of currenty country --> "ch", "de", "at"
private String currentCountry;
public FrontController() {
this.init();
}
private void init() {
this.initCountry();
}
private void initCountry() {
String host = request().host();
// then get country from host
}
}
因爲當我嘗試這個,我得到的錯誤信息:
Error injecting constructor, java.lang.RuntimeException: There is no HTTP Context available from here
我認爲問題可能與「要求()調用。