提供された URL を使用して、他の Web サイトから取得した複数の値を表示できる、小さな compojure Web アプリケーションを作成しました。現時点では、この URL は関数の 1 つにハード コードされています。テキスト フィールドとチェックボックスの値に基づいて動的 URL を作成する機能を追加したいと考えています。
これは私のページがどのように見えるかです:
(defn view-layout [& content]
(html [:body content]))
(defn view-input []
(view-layout
[:h2 "Find"]
[:form {:method "post" :action "/"}
( for [category ["Cat1" "Cat2" "Cat3"]]
[:input {:type "checkbox" :id category } category ] )
[:br]
[:input {:type "text" :id "a" :value "insert manga name"}] [:br]
[:input.action {:type "submit" :value "Find"}]
[:a {:href "/downloads"} "Downloads"]]))
(defn view-output []
(view-layout
[:h2 "default images"]
[:form {:method "post" :action "/"}
(for [name (get-content-from-url (create-url))]
[:label name [:br]]
)]))
(defn create-manga-url
[]
"http://www.mysite.net/search/?tfield=&check=000")
ルートは次のとおりです。
(defroutes main-routes
(GET "/" []
(view-input))
(GET "/downloads" []
(view-downloads))
(POST "/" []
(view-output) ))
現時点では、(create-url)
関数 (文字列を返す) のヘルプが必要です。検索に必須のすべてのフィールド (1 つのテキスト フィールドと 3 つのチェックボックス) をフェッチし、それらから値を解析して (連結された) URL - チェックボックスの場合、チェックされている場合、チェック セクションの値は 0 ではなく 1 になり、チェックされていない場合は 0 のままになります (check=100、または 2 つのチェック ボックスが選択されている場合は 010, 011)。テキスト フィールドの場合、tfield=userinputtext です。
EDIT私は.NetおよびJava開発者として多くの時間を費やしました.compojureのこの部分は私にとって完全な謎です. これは私が関数で達成したいこと(create-url)
です(OOスタイルで書かれた疑似コード):
(defn create-url [*text_field cbox1 cbox2 cbox3*]
(def url "http://www.mysite.net/search/?")
(def tfield "tfield=")
(def cbox "&check=")
(if (checked? cbox1)
(str cbox "1")
(str cbox "0"))
(if (checked? cbox2)
(str cbox "1")
(str cbox "0"))
(if (checked? cbox3)
(str cbox "1")
(str cbox "0"))
(str tfield (:value text_field))
(str url tbox cbox))
この擬似コードがどのように見えるかについてお詫び申し上げますが、これは私が学びたい部分です: フォームからデータをスクープして解析するにはどうすればよいですか (この場合、フォームフィールドから文字列に値を添付したいと思います)
誰でもこれで私を助けることができますか?