以附带的Hello world为例
view/index.html 内容如下
<html>
<head>
<title>Argo sample page</title>
</head>
<body>
<h2>Samples</h2>
<ul>
<li><a href="$__beat.servletContext.contextPath/hello/world">根据url获得参数的hello world</a></li>
<li><a href="$__beat.servletContext.contextPath/1.html">静态文件显示</a></li>
<li><a href="$__beat.servletContext.contextPath/form.html">区分queryString和form参数</a></li>
<li><a href="$__beat.servletContext.contextPath/upload-form.html">文件上传展示</a></li>
</ul>
</body>
</html>
当点击第一个链接的时候,跳转到hello/world路径,这是Argo做了什么处理?
找到一个HelloController.java类,内容如下
public class HelloController extends AbstractController {
@Path("hello/{name}")
public ActionResult hello(String name) {
return writer().write("hello %s", name);
}
}
@Path 注解将url的路径名与方法hello关联起来了
writer().write()则直接设定页面显示的内容。
如果要显示非静态的内容呢?
再看例子postForm.html
@Path("post.html")
@POST // 只处理post的请求
public ActionResult postForm() {
BeatContext beat = beat();
ClientContext client = beat.getClient();
Preconditions.checkState(Strings.isNullOrEmpty(client.form("company")));
Preconditions.checkState(Strings.isNullOrEmpty(client.form("address")));
client.queryString("name");
Preconditions.checkState(Strings.isNullOrEmpty(client.queryString("name")));
Preconditions.checkState(Strings.isNullOrEmpty(client.queryString("phone")));
Preconditions.checkState(Strings.isNullOrEmpty(client.queryString("submit")));
beat.getModel()
.add("company", client.queryString("company"))
.add("address", client.queryString("address"))
.add("name", client.form("name"))
.add("phone", client.form("phone"))
.add("submit", client.form("submit"));
return view("post"); // resources/views/post.html velocity模板
}
程序获得表单提交内容以及url参数,然后以<key,value>的形式添加到beat的model中,然后用对应的view/post的velocity模版显示页面。
<h3>queryString parameter</h3>
<ul>
<li>company: $company</li>
<li>address: $address</li>
</ul>
<h3>form parameter</h3>
<ul>
<li>name: $name</li>
<li>phone: $phone</li>
<li>submit: $submit</li>
</ul>
$company显示company变量的值。
这些view是用Velocity语言写的,语法可以参考http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
controller:
Argo启动时扫描Controller,所有Controller必须是Controller结尾