我正在尝试制作一个帮助教师在线计算成绩的网络应用程序。我使用了lein new heroku命令来制作它,并且无法对我的路由器进行排序和首页进行渲染。任何帮助,将不胜感激!这是我的defroutes的样子。它位于web.clj文件中,与我想要呈现的页面位于同一文件夹中:
(ns clojuregrade.web
(:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]]
[compojure.handler :refer [site]]
[compojure.route :as route]
[clojure.java.io :as io]
[ring.middleware.stacktrace :as trace]
[ring.middleware.session :as session]
[ring.middleware.session.cookie :as cookie]
[ring.adapter.jetty :as jetty]
[ring.middleware.basic-authentication :as basic]
[cemerick.drawbridge :as drawbridge]
[environ.core :refer [env]]))
(defroutes app
(ANY "/repl" {:as req}
(drawbridge req))
(GET "/" []
{:status 200 ;; <- this is where it says the error is showing up
:headers {"Content-Type" "text/plain"}
:body (pr-str (slurp (io "landing.clj"))) ;; <- I am guessing here
(ANY "*" []
(route/not-found (slurp (io/resource "404.html"))))
这是我想要呈现的页面的代码:
(ns clojuregrade.landing
(:require [compojure.core :refer [defroutes GET PUT POST DELETE ANY]]
[compojure.handler :refer [site]]
[compojure.route :as route]
[clojure.java.io :as io]
[ring.middleware.stacktrace :as trace]
[ring.middleware.session :as session]
[ring.middleware.session.cookie :as cookie]
[ring.adapter.jetty :as jetty]
[hiccup.core :refer :all]
))
...
(defn home [& [weights grades error]]
(layout/common
[:h1 "Welcome to Clojure-grade"]
[:hr]
(form-to [:post "/"]
[:p "Enter the weights for your various grades below. For example if you have quizzes, homework and tests
and they are worth 40% 10% and 50% respectively, you can enter: [40 10 50].
Of course, all of the numbers should add up to 100%. Be sure to include the brackets"
(text-field {:placeholder "[40 10 50]"} "weights" weights)]
[:p "Enter ALL of the grades for EACH STUDENT in your class.
Make sure that each of the grades is ordered such that the grade corresponds
to its matching weight above. Use brackets to separate students from each other.
Each set of grades should have the same number of grades as the number of possible weights (3 in this example case).
The following example shows grades for 4 students. Format your grades according to the number of students in your class:"
(text-area {:rows 40 :cols 40 :placeholder
"[89 78 63]
[78 91 60]
[54 85 91]
[100 89 77]
..." } "grades" grades)]
(submit-button "process"))))
(defn process-grades [weights grades]
(->> (float grades)
(map (partial percentify-vector (float weights)))
(mapv #(apply + %))))
(defroutes app
(GET "/" []
{:status 200
:headers {"Content-Type" "text/html"}
:body home})
(POST "/" [weights grades] ((process-grades (read-string weights) (read-string grades)) ))
(ANY "*" []
(route/not-found (slurp (io/resource "404.html")))))
(defn wrap-error-page [handler]
(fn [req]
(try (handler req)
(catch Exception e
{:status 500
:headers {"Content-Type" "text/html"}
:body (slurp (io/resource "500.html"))}))))
答案 0 :(得分:2)
您的代码存在多个问题。首先,你有(io "landing.clj")
,据我所知,io
指的是命名空间,而不是有效的var。但是即使你有(io/resource "landing.clj")
(并且landing.clj在资源目录中),这只会将landing.clj的内容(即源代码)转储到浏览器......可能不是你想要的
您需要致电clojuregrade.landing/home
。您可以直接从clojuregrade.web
中的“/”路由执行此操作。您也可以(我认为您打算这样做),将clojuregrade.web
中的“/”路由发送到clojuregrade.landing
中的路由处理程序(即调用clojuregrade.landing/app
)。您可能还会发现https://github.com/weavejester/compojure/wiki/Nesting-routes有用。
顺便说一句,clojuregrade.landing
中的“/”路线看起来几乎正确。但是,您可能希望实际调用home
,而不是返回对函数的引用:
(GET "/" []
{:status 200
:headers {"Content-Type" "text/html"}
:body (home)}) ;; <-- Note parentheses -- (home) instead of home
为了获得有效的应用程序,您可能还需要做更多的工作。如果你还没有,请通过至少一个Ring / Compojure教程来至少获得一个有效的应用程序,然后应用你学到的东西。