shinewang

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  53 随笔 :: 0 文章 :: 200 评论 :: 0 Trackbacks
本文通过5个例子展示了Play!框架背后的哲学。

 

1. 绑定HTTP参数到Java方法

通过在Java方法中定义和HTTP参数同名的参数就可以方便的取得HTTP中的参数值。

例如:

/articles/archive?date=08/01/08&page=2

你可以通过定义方法参数来获取datepage的值:

public static void archive(Date date, Integer page) {
    List
<Article> articles = Articles.fromArchive(date, page);
    render(articles);
}

也可以直接绑定到类:

public class Person {
  String name;
  Integer age;
}

public static void add(Person p) {
  p.save();
}

HTML中如下定义:

<form action="/Directory/add" >
 Name: 
<input type="text" name="p.name" />
 Age: 
<input type="text" name="p.age" />
</form>

2. 通过调用Java方法重定向到相应的action

只需要调用相应的Java方法就能方便地重定向到另一个actionPlay!会生成正确的redirect response,而不是forward

public static void show(Long id) {
    Article article 
= Article.findById(id);
    render(article);
}

public static void edit(Long id, String title) {
    Article article 
= Article.findById(id);
    article.title 
= title;
    article.save();
    show(id);
}

注意:在edit action的最后重定向到了show action

 

在模板中你可以使用类似的符号来生成链接:

<a href="@{Article.show(article.id)}">${article.title}</a>

最终生成的HTML

<a href="/articles/15">My new article</a>

 

3. 直接把Java对象传递到页面模板

在大多数Java框架中,你需要写类似下面代码来把Java对象传递给页面模板:

Article article = Article.findById(id);
User user 
= User.getConnected();
Map
<String, Object> model = new HashMap<String,Object>();
model.put(
"article", article);
model.put(
"user", user);
render(model);

Play!中你只需要这么做:

Article article = Article.findById(id);
User user 
= User.getConnected();
render(article, user); 

4. 方便的文件上传

Play!框架中实现文件上传很方便:

HTML form

<form action="@{Article.uploadPhoto}" method="POST" enctype="multipart/form-data">
    
<input type="text" name="title" />
    
<input type="file" id="photo" name="photo" />
    
<input type="submit" value="Send it " />
</form>

Java代码:

public static void uploadPhoto(String title, File photo) {
   
}

5. 不用任何配置就可以把应用分布到多个JVM

由于应用程序被设计成无状态的,所以你可以简单地在多台服务器上启动同一个应用,然后通过HTTP层面的负载均衡来实现分布式应用。不需要设置一个负债的服务器集群。


----------

Play With Play!系列目录

http://www.blogjava.net/shinewang/archive/2008/12/25/248237.html


posted on 2008-12-25 13:09 shinewang 阅读(2412) 评论(7)  编辑  收藏 所属分类: JavaPlay!

评论

# re: Play with Play! - Play!最酷的5个功能 2008-12-25 13:17 五月天
学习中  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-25 14:23 HiMagic!
semantic url  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-25 15:00 @beyongwcm
不错,有创意  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-26 14:23 rmn190
这段时间正在学OFBiz,初看它源码时发出里面有太多的static方法, 今天看到楼主的static方法,似乎明白了些.

  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-26 14:26 jeasonzhao
I think there is a lot of efforts needed to integrate Play! into real world.
So, I am watching.  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-26 15:08 shinewang
@jeasonzhao
确实是这样,play!很多地方是另起炉灶的,和现有ssh的经典架构、tomcat等服务器的集成可能不是很方便。目前使用play!最好还是直接使用它那套东西。当然play!也在努力,例如对spring的集成、对其他服务器的支持就在开发中。  回复  更多评论
  

# re: Play with Play! - Play!最酷的5个功能 2008-12-26 15:15 shinewang
@rmn190
play!有很多有争议的特性,例如大量static方法,但仔细想像是符合使用情形的,还有public的成员变量来模拟实现属性,这个是为了敏捷的变通,而我们一边收的教育是使用private,但好用就行,把对程序的控制权交回给开发者。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: