使用 clojure.string/split 时出现 ClassCastException

问题描述 投票:0回答:1

我正在编写一个网站,使用 clojure hiccup 作为 html,使用 clojure Garden 作为 css。现在我在使用 clojure.string/split 分割文件名的路径时遇到问题。所以对于

some/path/file.md
我想要
file

对于上下文,这里是代码片段,问题出在哪里

(defn blog-card [post]
  [:a.blog-post-card-link {:href (->> (:location post)
                                      (string/split #"/")
                                      last
                                      (string/replace ".md" ""))}
    [:div.blog-post-card
      -- rest of content -- ]])

现在这个函数在这里被调用

(defn blog-content []
  [:div.blog-content
    [:div.blog-post-cards-list
      (let [json-str (slurp "resources/public/markdown/posts/all-posts.json")
            posts (json/parse-string json-str true)]
        (map blog-card (reverse posts)))]])

其中

json
[cheshire.core :as json]
string
[clojure.string :as string]
all-posts.json
看起来像这样

[
  {
    "id": "0",
    "title": "Test article #1",
    "description": "This article is just a place-filler for development.",
    "image": "images/mountain.jpg",
    "date": "10 Oct 2023",
    "author": "Elías Hauksson",
    "location": "markdown/posts/test-post01.md"
  },
  -- more test entries --
]

所以在这个例子中,我想要实现的是,当按下卡片时,一个人会被重定向到

/test-post01

现在,当我尝试运行此代码时,我收到以下错误消息

HTTP ERROR 500 java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.regex.Pattern (java.lang.String and java.util.regex.Pattern are in module java.base of loader 'bootstrap')
URI:    /blog
STATUS: 500
MESSAGE:    java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.regex.Pattern (java.lang.String and java.util.regex.Pattern are in module java.base of loader 'bootstrap')
SERVLET:    -
CAUSED BY:  java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.regex.Pattern (java.lang.String and java.util.regex.Pattern are in module java.base of loader 'bootstrap')
Caused by:

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.regex.Pattern (java.lang.String and java.util.regex.Pattern are in module java.base of loader 'bootstrap')
    at clojure.string$split.invokeStatic(string.clj:219)
    at clojure.string$split.invoke(string.clj:219)
    at eliashaukssoncom.views.blog$blog_card.invokeStatic(blog.clj:10)
    at eliashaukssoncom.views.blog$blog_card.invoke(blog.clj:8)
    at clojure.core$map$fn__5935.invoke(core.clj:2772)
    at clojure.lang.LazySeq.sval(LazySeq.java:42)
    at clojure.lang.LazySeq.seq(LazySeq.java:51)
    at clojure.lang.RT.seq(RT.java:535)
    at clojure.core$seq__5467.invokeStatic(core.clj:139)
    at clojure.core$map$fn__5935.invoke(core.clj:2763)
    at clojure.lang.LazySeq.sval(LazySeq.java:42)
    at clojure.lang.LazySeq.seq(LazySeq.java:51)
    at clojure.lang.RT.seq(RT.java:535)
    at clojure.core$seq__5467.invokeStatic(core.clj:139)
    at clojure.core$apply.invokeStatic(core.clj:662)
    at clojure.core$apply.invoke(core.clj:662)
    at hiccup.compiler$fn__3080.invokeStatic(compiler.clj:139)
    at hiccup.compiler$fn__3080.invoke(compiler.clj:133)
    at hiccup.compiler$fn__3063$G__3058__3068.invoke(compiler.clj:119)
    at clojure.core$map$fn__5935.invoke(core.clj:2770)
    at clojure.lang.LazySeq.sval(LazySeq.java:42)
    at clojure.lang.LazySeq.seq(LazySeq.java:51)
    at clojure.lang.RT.seq(RT.java:535)
    at clojure.core$seq__5467.invokeStatic(core.clj:139)
    at clojure.core$apply.invokeStatic(core.clj:662)
    at clojure.core$apply.invoke(core.clj:662)
    at hiccup.compiler$fn__3080.invokeStatic(compiler.clj:139)
    at hiccup.compiler$fn__3080.invoke(compiler.clj:133)
    at hiccup.compiler$fn__3063$G__3058__3068.invoke(compiler.clj:119)
    at hiccup.compiler$render_element.invokeStatic(compiler.clj:129)
    at hiccup.compiler$render_element.invoke(compiler.clj:123)
    at hiccup.compiler$fn__3078.invokeStatic(compiler.clj:136)
    at hiccup.compiler$fn__3078.invoke(compiler.clj:133)
    at hiccup.compiler$fn__3063$G__3058__3068.invoke(compiler.clj:119)
    at clojure.core$map$fn__5935.invoke(core.clj:2770)
    at clojure.lang.LazySeq.sval(LazySeq.java:42)
    at clojure.lang.LazySeq.seq(LazySeq.java:51)
    at clojure.lang.RT.seq(RT.java:535)
    at clojure.core$seq__5467.invokeStatic(core.clj:139)
    at clojure.core$apply.invokeStatic(core.clj:662)
    at clojure.core$apply.invoke(core.clj:662)
    at hiccup.compiler$fn__3080.invokeStatic(compiler.clj:139)
    at hiccup.compiler$fn__3080.invoke(compiler.clj:133)
    at hiccup.compiler$fn__3063$G__3058__3068.invoke(compiler.clj:119)
    at hiccup.compiler$render_element.invokeStatic(compiler.clj:129)
    at hiccup.compiler$render_element.invoke(compiler.clj:123)
    at hiccup.compiler$fn__3078.invokeStatic(compiler.clj:136)
    at hiccup.compiler$fn__3078.invoke(compiler.clj:133)
    at hiccup.compiler$fn__3063$G__3058__3068.invoke(compiler.clj:119)
    at eliashaukssoncom.views.base$base$fn__1001.invoke(base.clj:26)
    at eliashaukssoncom.views.base$base.invokeStatic(base.clj:26)
    at eliashaukssoncom.views.base$base.invoke(base.clj:25)
    at eliashaukssoncom.views.blog$blog_page.invokeStatic(blog.clj:35)
    at eliashaukssoncom.views.blog$blog_page.invoke(blog.clj:34)
    at eliashaukssoncom.handler$fn__1026.invokeStatic(handler.clj:13)
    at eliashaukssoncom.handler$fn__1026.invoke(handler.clj:13)
    at compojure.core$wrap_response$fn__2214.invoke(core.clj:158)
    at compojure.core$wrap_route_middleware$fn__2198.invoke(core.clj:128)
    at compojure.core$wrap_route_info$fn__2203.invoke(core.clj:137)
    at compojure.core$wrap_route_matches$fn__2207.invoke(core.clj:146)
    at compojure.core$routing$fn__2222.invoke(core.clj:185)
    at clojure.core$some.invokeStatic(core.clj:2718)
    at clojure.core$some.invoke(core.clj:2709)
    at compojure.core$routing.invokeStatic(core.clj:185)
    at compojure.core$routing.doInvoke(core.clj:182)
    at clojure.lang.RestFn.applyTo(RestFn.java:139)
    at clojure.core$apply.invokeStatic(core.clj:669)
    at clojure.core$apply.invoke(core.clj:662)
    at compojure.core$routes$fn__2226.invoke(core.clj:192)
    at ring.middleware.resource$wrap_resource_prefer_resources$fn__596.invoke(resource.clj:25)
    at clojure.lang.Var.invoke(Var.java:384)
    at ring.adapter.jetty$proxy_handler$fn__420.invoke(jetty.clj:27)
    at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown Source)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)
    at org.eclipse.jetty.server.Server.handle(Server.java:516)
    at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487)
    at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
    at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034)
    at java.base/java.lang.Thread.run(Thread.java:829)

我尝试过的事情

  1. (:location post)
    替换为
    "some/test/path/file.md"
  2. 编写不带
    ->>
    运算符的函数。像这样
{:href (string/replace (last (string/split (:location post) #"/")) #".md" "")}

提前感谢您的建议。

clojure classcastexception hiccup
1个回答
0
投票

clojure.string/split
接受字符串作为第一个参数,正则表达式作为第二个参数。 “我尝试过的内容”部分中的代码应该可以工作。主要部分中的代码不起作用,因为
(->> (:location post) (string/split #"/"))
扩展为
(string/split #"/" (:location post))

© www.soinside.com 2019 - 2024. All rights reserved.