我正在尝试按照简单网络应用程序的球拍指南上的教程进行操作,但无法获得一个基本的基本内容。
如何让 servlet 根据请求 URL 提供不同的内容?尽管经过我的努力,即使是巨大的博客示例也是一个大文件,并且在我背后使用巨大的 get 查询字符串处理所有内容。如何根据 URL 做任何事情?defpage
Clojure 的 Noir 框架将这个基本功能放在了主页的前面(
The URL is part of the request
structure that the servlet receives as an argument. You can get the URL by calling request-uri
, then you can look at it to do whatever you want. The request also includes the HTTP method, headers, and so on.
But that's pretty low-level. A better solution is to use dispatch-rules
to define a mapping from URL patterns to handler functions. Here's an example from the docs:
(define-values (blog-dispatch blog-url)
(dispatch-rules
[("") list-posts]
[("posts" (string-arg)) review-post]
[("archive" (integer-arg) (integer-arg)) review-archive]
[else list-posts]))
Make your main servlet handler blog-dispatch
. The URL http://yoursite.com/
will be handled by calling (list-posts req)
, where req
is the request structure. The URL http://yoursite.com/posts/a-funny-story
will be handled by calling (review-post req "a-funny-story")
. And so on.