You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从数据库获取的HTML无法在Hunchentoot路由中通过cl-who渲染

问题根源与解决方案

核心原因

cl-who 默认会自动转义所有传入的字符串内容(目的是防止XSS攻击),所以你从数据库取出的HTML字符串会被转义成诸如 <div> 的HTML实体,浏览器只会把它当成纯文本显示,而非渲染成实际的HTML元素。而你手动硬编码的是 cl-who 的语法表达式(比如 (:div "内容")),会被 cl-who 直接解析为HTML标签,因此能正常渲染。

解决方案

你需要明确告诉 cl-who 不要转义数据库返回的HTML字符串,有两种常用方式:

方式一:使用 cl-who 的 raw-string 函数

修改你的 Hunchentoot 路由,将数据库返回的HTML用 cl-who:raw-string 包裹:

(define-easy-handler (test-page :uri "/wow") ()
  (let ((content (bt:make-thread (lambda () (second (get-content))))))
    (defpage (:title "Main One") 
      (cl-who:raw-string (bt:join-thread content)))))

方式二:直接写入标准输出

在 cl-who 的输出上下文里,直接通过 write-string 将HTML字符串写入标准输出,绕开cl-who的转义逻辑:

(define-easy-handler (test-page :uri "/wow") ()
  (let ((content (bt:make-thread (lambda () (second (get-content))))))
    (defpage (:title "Main One") 
      (write-string (bt:join-thread content) *standard-output*))))

额外优化建议

Hunchentoot 本身是多线程处理请求的,每个请求都会在独立线程中执行,你额外创建线程获取数据库内容的操作其实没必要,可以简化路由代码:

(define-easy-handler (test-page :uri "/wow") ()
  (defpage (:title "Main One") 
    (cl-who:raw-string (second (get-content)))))

验证要点

可以先确认数据库返回的内容类型是否正确,在REPL中执行 (type-of (second (get-content))),确保返回的是字符串类型(比如 STRING),如果是其他类型(比如字节数组),需要先转换成字符串再处理。

内容的提问来源于stack exchange,提问作者Vinn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 16:25:37