虽然你已经解决了这个问题,但我想我可以添加这个作为替代方案。如果你不想制作一个完整的自定义接受器,你可以在HUNCHENTOOT:ACCEPTOR-DISPATCH-REQUEST
for上添加一个 around-method HUNCHENTOOT:EASY-HANDLER
。
让我们先做一个接受者和一个页面:
(defparameter *acceptor* (make-instance 'hunchentoot:easy-acceptor :port 4242))
(hunchentoot:define-easy-handler (foo :uri "/foo") ()
(format nil "<html><body><h1>Test</h1><p>foo</p></body></html>"))
(hunchentoot:start *acceptor*)
然后重定向/bar
到/quux
:/foo
;; A simple helper to create prefix dispatchers.
(defun make-redirect-list (redirects)
(mapcar (lambda (redirect)
(destructuring-bind (from . to) redirect
(hunchentoot:create-prefix-dispatcher from
(lambda ()
(hunchentoot:redirect to)))))
redirects))
(defparameter *redirects* (make-redirect-list
'(("/bar" . "/foo")
("/quux" . "/foo"))))
(defmethod hunchentoot:acceptor-dispatch-request :around
((acceptor hunchentoot:easy-acceptor) request)
(dolist (redirect *redirects*)
;; Match the request against the prefix dispatchers in *REDIRECTS*...
(let ((handler (funcall redirect request)))
(when handler
;; and call the corresponding handler if a match is found.
(return-from hunchentoot:acceptor-dispatch-request
(funcall handler)))))
;; Unless a handler was found, call next method to
;; handle the request normally.
(call-next-method))
编辑:使用 around 方法而不是 before。我最初认为让它正常调用 main 方法对于任何日志记录/等都是必要的。在那里发生,但经过进一步测试后似乎没有。