作为完整的clojure noob,我正在尝试启动一个小教程应用程序,以便熟悉compojure。这是一个小应用程序,允许用户添加两个数字,点击按钮后显示其他页面上的总和。我遵循马克McGranaghan blog的指示。一切似乎都没问题,直到我尝试得到我输入的两个数字的总和,而不是得到结果,我被重定向到同一页面(所以基本上我被困在本教程的第一步)。检查代码后,似乎在输入解析(由于某种原因)时触发了NumberFormatException。在我的所有测试中,我都尝试输入各种数字格式,但没有成功。这是最简单的代码版本,作者说应该可以使用(我已经尝试了github site的最新版本 - 同样的场景:NFE):
(ns adder.core
(:use compojure.core)
(:use hiccup.core)
(:use hiccup.page-helpers))
(defn view-layout [& content]
(html
(doctype :xhtml-strict)
(xhtml-tag "en"
[:head
[:meta {:http-equiv "Content-type"
:content "text/html; charset=utf-8"}]
[:title "adder"]]
[:body content])))
(defn view-input []
(view-layout
[:h2 "add two numbers"]
[:form {:method "post" :action "/"}
[:input.math {:type "text" :name "a"}] [:span.math " + "]
[:input.math {:type "text" :name "b"}] [:br]
[:input.action {:type "submit" :value "add"}]]))
(defn view-output [a b sum]
(view-layout
[:h2 "two numbers added"]
[:p.math a " + " b " = " sum]
[:a.action {:href "/"} "add more numbers"]))
(defn parse-input [a b] ;; this is the place where problem occures
[(Integer/parseInt a) (Integer/parseInt b)])
(defroutes app
(GET "/" []
(view-input))
(POST "/" [a b]
(let [[a b] (parse-input a b)
sum (+ a b)]
(view-output a b sum)))
有人能告诉我更好的方法来解析输入值,以避免这种异常吗?我尝试了几种技术,但没有任何方法可以帮助我。我在win 7机器上使用Leningen v1.7.1和clojure 1.3。
以下是我的project.clj文件的内容:
(defproject adder "0.0.1"
:description "Add two numbers."
:dependencies
[[org.clojure/clojure "1.3.0"]
[org.clojure/clojure-contrib "1.1.0"]
[ring/ring-core "1.0.2"]
[ring/ring-devel "1.0.2"]
[ring/ring-jetty-adapter "1.0.2"]
[compojure "1.0.1"]
[hiccup "0.3.8"]]
:dev-dependencies
[[lein-run "1.0.0"]])
和run.clj脚本:
(use 'ring.adapter.jetty)
(require 'adder.core)
(let [port (Integer/parseInt (get (System/getenv) "PORT" "8080"))]
(run-jetty #'adder.core/app {:port port}))
感谢。
答案 0 :(得分:1)
您正在使用compojure 1.0.1,您所关注的博客中的示例正在使用compojure 0.4.0。
从版本0.6.0开始,Compojure不再向路由添加默认中间件。这意味着您必须明确地将wrap-params和wrap-cookies中间件添加到您的路由中。
来源:https://github.com/weavejester/compojure
因此您需要显式添加wrap-params中间件。因此需要进行以下更改......
(ns adder.core
(:use ; change to idiomatic usage of :use
[compojure.core]
[hiccup.core]
[hiccup.page-helpers]
[ring.middleware.params :only [wrap-params]])) ; add middleware for params
(defn view-layout [& content]
(html
(doctype :xhtml-strict)
(xhtml-tag "en"
[:head
[:meta {:http-equiv "Content-type"
:content "text/html; charset=utf-8"}]
[:title "adder"]]
[:body content])))
(defn view-input []
(view-layout
[:h2 "add two numbers"]
[:form {:method "post" :action "/"}
[:input.math {:type "text" :name "a" :id "a"}] [:span.math " + "]
[:input.math {:type "text" :name "b" :id "a"}] [:br]
[:input.action {:type "submit" :value "add"}]]))
(defn view-output [a b sum]
(view-layout
[:h2 "two numbers added"]
[:p.math a " + " b " = " sum]
[:a.action {:href "/"} "add more numbers"]))
(defn parse-input [a b]
[(Integer/parseInt a) (Integer/parseInt b)])
(defroutes main-routes ; needs to be renamed
(GET "/" []
(view-input))
(POST "/" [a b]
(let [[a b] (parse-input a b)
sum (+ a b)]
(view-output a b sum))))
(def app (wrap-params main-routes)) ; wrap the params to allow destructuring to work