`
desert3
  • 浏览: 2140820 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

Play framework Controllers控制器、action链式调用、返回的HTTP状态码

 
阅读更多
Playframework中的控制器放在controllers包中,并集成play.mvc.Controller类。每个public static方法都对应控制器的一个action操作,action方法没有return语句块,一般都通过调用框架的result方法处理Response返回。
package controllers;
 
import models.Client;
import play.mvc.Controller;
 
public class Clients extends Controller {
 
    public static void show(Long id) {
        Client client = Client.findById(id);
        render(client);
    }
 
    public static void delete(Long id) {
        Client client = Client.findById(id);
        client.delete();
    }
 
}

获取HTTP参数:
  • 简单类型参数
  • 高级Java类型参数
  • Date参数
  • Calendar参数
  • File参数
  • 数组、集合参数
  • POJO(简单Java对象)参数
  • JPA对象参数
  • 自定义参数绑定@play.data.binding.As @play.data.binding.NoBinding play.data.binding.TypeBinder @play.data.binding.Global

Response返回类型:action方法必须生成HTTP Response。最简单的方法就是发出一个Result对象。当Result对象被发出后,正常的代码执行流就会被中止,方法被返回。即发出Result对象后,后续的代码将不再被执行!
public static void show(Long id) {
    Client client = Client.findById(id);
    render(client);
    System.out.println("This message will never be displayed !");
}

  • renderText
  • renderBinary
  • 下载文件
  • 返回默认匹配的模板render 模板参数:renderArgs.put("client", client);
  • 指定返回特定的模板 renderTemplate("Clients/showClient.html", id, client);
  • 重定向redirect("http://www.zenexity.fr");
  • action链:链式调用

自定义response编码 response.encoding = "ISO-8859-1";
自定义request编码 accept-charset告诉浏览器使用什么编码发送数据 _charset_隐藏域告诉play框架发送的请求使用的是什么编码

HTTP拦截器:
  • @Before: 注解表明在控制器的每个action方法被调用之前,调用这个拦截器
  • @After: 注解表明在控制器的每个action方法被正常调用之后,调用这个拦截器
  • @Finally: 在每个action方法调用后(不管是正常调用还是异常结束),调用这个拦截器
  • @Catch: 特定异常拦截器,异常拦截优先顺序 priority = 1
  • @With: 由于Java不允许多重继承,因此使用继承层次的拦截器会有一定的限制,这时,你可以在不同的类中定义不同的拦截器方法,然后用@With注解来调用 @With(Secure.class)

继承的控制器会自动继承父类的拦截器
对于@Before,@After和@Finally拦截器,可以使用unless或者only参数来指定、排除特定的action方法  @Before(unless="login")  @Before(only={"login","logout"})
public class Admin extends Application {
 
    @Before
    static void checkAuthentification() {
        if(session.get("user") == null) login();
    }
 
    @Catch(IllegalStateException.class)
    public static void logIllegalState(Throwable throwable) {
        Logger.error("Illegal state %s…", throwable);
    }

    @Finally
    static void log() {
        Logger.info("Response contains : " + response.out);
    }
 
    public static void index() {
        List<User> users = User.findAll();
        render(users);
    }

    @Finally
    static void log(Throwable e) {
        if( e == null ){
            Logger.info("action call was successful");
        } else{
            Logger.info("action call failed", e);
        }
    }

    public static void index() {
        List<User> users = User.findAll();
        render(users);
    }
}

public class Secure extends Controller {
    
    @Before
    static void checkAuthenticated() {
        if(!session.containsKey("user")) {
            unAuthorized();
        }
    }
}   
@With(Secure.class)
public class Admin extends Application {
    
    …
}

Session and Flash:它们的数据都不保存在服务器端,保存在加密的客户端Cookie中。它们的值是通过Cookie机制加到http请求中的。所以只能最大保存4KB的数据,并且只能保存字符串值
  • Session:用户会话进程中起作用
  • Flash:下个请求中起作用

action链:链式调用playframework 1.2.3 Controllers Action chaining
playframework中没有和Servlet api中forward相应的功能。一个Http请求只能调用一个action方法。如果你要调用另外的action,那么只能使用浏览器redirect 那个action对应的url来完成。这种情况下,浏览器url和执行的action方法一一对应。浏览器的后退、前进、刷新就很容易管理。

你可以简单地通过java的方式调用另外一个action方法,playframework会自动地发送重定向请求到另外的action方法。
public class Clients extends Controller {
 
    public static void show(Long id) {
        Client client = Client.findById(id);
        render(client);
    }
 
    public static void create(String name) {
        Client client = new Client(name);
        client.save();
        show(client.id);
    }
}

// 相应路由信息:
GET    /clients/{id}            Clients.show
POST   /clients                 Clients.create 

playframework相应的处理机制:
  • 浏览器发送一个POST请求到/clients url
  • playframework Router模块调用Clients控制器的create方法
  • create方法执行时调用控制器的show 方法
  • Router模块解析这个包含id参数的Clients.show方法的调用请求,并且生成相应的url
  • Http Response返回状态码302,并伴随重定向的url信息 Location:/clients/3132.
  • 浏览器然后发送http重定向请求到/clients/3132
  • 因此最终返回的Http状态码是302, 而不调用其他控制器方法的action正常执行完成后,一般返回的Http状态码是200
  • 避免Http状态码的改变同时要迁移到非默认的模板页面,可以通过调用renderTemplate("Application/show.html",something);方法来实现

参考:playframework 1.2.3 Controllers
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics