0

完全な clojure 初心者として、compojure に慣れるために、1 つの小さなチュートリアル アプリを開始しようとしています。これは、ユーザーが 2 つの数字を追加できる小さなアプリケーションであり、ボタンをクリックすると、その合計が別のページに表示されます。Mark McGranaghan blogの指示に従いました。結果を取得する代わりに、入力した 2 つの数値の合計を取得しようとするまで、すべて問題ないように見えますが、同じページにリダイレクトされます (基本的に、このチュートリアルの最初のステップで立ち往生しています)。コードを確認したところ、(何らかの理由で) 入力の解析が行われると NumberFormatException がトリガーされるようです。すべてのテストで、あらゆる種類の数値形式を入力しようとしましたが、成功しませんでした。これは、著者が動作するはずであると述べた最も単純なコードバージョンです(私はからの最新バージョンを試しましたgithub サイト- 同じシナリオ: 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マシンでclojure 1.3でLeningen v1.7.1を使用しています。

私の 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}))

ありがとう。

4

1 に答える 1

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
于 2012-04-02T08:33:17.420 に答える