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();
}

}

[b]获取HTTP参数:[/b]
[list]
[*]简单类型参数
[*]高级Java类型参数
[*]Date参数
[*]Calendar参数
[*]File参数
[*]数组、集合参数
[*]POJO(简单Java对象)参数
[*]JPA对象参数
[*]自定义参数绑定@play.data.binding.As @play.data.binding.NoBinding play.data.binding.TypeBinder @play.data.binding.Global
[/list]
[b]Response返回类型:[/b]action方法必须生成HTTP Response。最简单的方法就是发出一个Result对象。当Result对象被发出后,正常的代码执行流就会被中止,方法被返回。[color=red]即发出Result对象后,后续的代码将不再被执行![/color]

public static void show(Long id) {
Client client = Client.findById(id);
render(client);
System.out.println("This message will never be displayed !");
}

[list]
[*]renderText
[*]renderBinary
[*]下载文件
[*]返回默认匹配的模板render 模板参数:renderArgs.put("client", client);
[*]指定返回特定的模板 renderTemplate("Clients/showClient.html", id, client);
[*]重定向redirect("http://www.zenexity.fr");
[*]action链:链式调用
[/list]
[b]自定义response编码[/b] response.encoding = "ISO-8859-1";
[b]自定义request编码[/b] accept-charset告诉浏览器使用什么编码发送数据 _charset_隐藏域告诉play框架发送的请求使用的是什么编码

[b]HTTP拦截器:[/b]
[list]
[*]@Before: 注解表明在控制器的每个action方法被调用之前,调用这个拦截器
[*]@After: 注解表明在控制器的每个action方法被正常调用之后,调用这个拦截器
[*]@Finally: 在每个action方法调用后(不管是正常调用还是异常结束),调用这个拦截器
[*]@Catch: 特定异常拦截器,异常拦截优先顺序 priority = 1
[*]@With: 由于Java不允许多重继承,因此使用继承层次的拦截器会有一定的限制,这时,你可以在不同的类中定义不同的拦截器方法,然后用@With注解来调用 @With(Secure.class)
[/list]
[color=red]继承的控制器会自动继承父类的拦截器[/color]
对于@Before,@After和@Finally拦截器,可以使用unless或者only参数来[color=red]指定、排除特定的action方法[/color] @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 {


}

[b]Session and Flash:[/b]它们的数据都不保存在服务器端,保存在[color=red]加密的客户端Cookie[/color]中。它们的值是通过Cookie机制加到http请求中的。所以[color=red]只能最大保存4KB的数据,并且只能保存字符串值[/color]。
[list]
[*]Session:用户会话进程中起作用
[*]Flash:下个请求中起作用
[/list]
[b]action链:链式调用[/b][url=http://www.playframework.org/documentation/1.2.3/controllers#chaining]playframework 1.2.3 Controllers Action chaining[/url]
playframework中没有和Servlet api中forward相应的功能。[color=red]一个Http请求只能调用一个action方法[/color]。如果你要调用另外的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

[b]playframework相应的处理机制:[/b]
[list]
[*]浏览器发送一个POST请求到/clients url
[*]playframework Router模块调用Clients控制器的create方法
[*]create方法执行时调用控制器的show 方法
[*]Router模块解析这个包含id参数的Clients.show方法的调用请求,并且生成相应的url
[*]Http Response返回状态码302,并伴随重定向的url信息 Location:/clients/3132.
[*]浏览器然后发送http重定向请求到/clients/3132
[*][color=red]因此最终返回的Http状态码是302, 而不调用其他控制器方法的action正常执行完成后,一般返回的Http状态码是200[/color]
[*]要[color=red]避免Http状态码的改变同时要迁移到非默认的模板页面[/color],可以通过调用[color=red]renderTemplate[/color]("Application/show.html",something);方法来实现
[/list]
参考:[url=http://www.playframework.org/documentation/1.2.3/controllers]playframework 1.2.3 Controllers[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值