如何根据 Racket Web servlet 中的路径显示不同的内容? [英] How to show different content based on the path in Racket web servlets?

查看:58
本文介绍了如何根据 Racket Web servlet 中的路径显示不同的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照关于简单网络应用程序的 Racket 指南的教程进行操作,但无法获得一个基本的、基本的东西.

I'm trying to follow the tutorial on the Racket guide on simple web apps, but can't get one, basic, basic thing.

如何让 servlet 根据请求 URL 提供不同的内容?尽管我进行了搜索,即使是巨大的博客示例也是一个大文件,并且所有内容都在我背后使用巨大的 get 查询字符串处理.我怎样才能根据 URL 做任何事情?Clojure 的 Noir 框架将这个基本功能放在主页的前面(defpage),但如何使用 Racket 做到这一点?

How can you have a servlet serve different content based on the request URL? Despite my scouring, even the huge blog example was one big file and everything handled with huge get query strings behind my back. How can I do anything based on URLs? Clojure's Noir framework puts this basic feature big up front on the home page (defpage) but how to do this with Racket?

推荐答案

URL 是 request 结构表示servlet作为参数接收.您可以通过调用request-uri 来获取URL,然后您可以查看它为您做任何您想做的事情.请求还包括 HTTP 方法、标头等.

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.

但那是相当低级的.更好的解决方案是使用 dispatch-rules 定义从 URL 模式到处理程序函数的映射.这是文档中的一个示例:

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]))

制作您的主要 servlet 处理程序 blog-dispatch.URL http://yoursite.com/ 将通过调用 (list-posts req) 来处理,其中 req 是请求结构.URL http://yoursite.com/posts/a-funny-story 将通过调用 (review-post req "a-funny-story") 来处理.等等.

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.

这篇关于如何根据 Racket Web servlet 中的路径显示不同的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆