我是backbone.js和前端开发的新手。 如果我的问题是愚蠢的,请注意。 我搜索了谷歌和stackoverflow并没有得到答案。 这是我的问题。 因为我使用前端的骨干和后端的java,我正在尝试使用backbone的collections.fetch()和save()等与Java Servlet进行通信。 模型就像这样简单:
var Todo = Backbone.Model.extend({ defaults: { title: '', completed: false } }); var TodosCollection = Backbone.Collection.extend({ model: Todo, url: '/todo' }); var todos = new TodosCollection(); todos.fetch();我在web.xml中
<servlet> <servlet-name>todo</servlet-name> <servlet-class>test.web.TodoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>todo</servlet-name> <url-pattern>/todo</url-pattern> </servlet-mapping>对于TodoServlet.java,我只需要以下代码来检查它是否获得GET和POST:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doGet"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doPost"); }当我在Tomcat上运行它并打开网页时,服务器似乎没有得到GET。 这是一个正确的方法吗? 提前致谢!
I am new to backbone.js and front end development. Appologies if my question is stupid. I searched over google and stackoverflow and didn't get an answer. Here is my question. As I am using backbone for the front end and java for my back end, I am trying to backbone's collections.fetch(), and save(), etc to talk to the Java Servlet. The model is as simple as this:
var Todo = Backbone.Model.extend({ defaults: { title: '', completed: false } }); var TodosCollection = Backbone.Collection.extend({ model: Todo, url: '/todo' }); var todos = new TodosCollection(); todos.fetch();I have in web.xml
<servlet> <servlet-name>todo</servlet-name> <servlet-class>test.web.TodoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>todo</servlet-name> <url-pattern>/todo</url-pattern> </servlet-mapping>And for TodoServlet.java, I simply have the following code just to check if it gets GET and POST:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doGet"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("In /todo: doPost"); }When I run this on Tomcat and open the webpage, it seems the server is not getting GET. Is this a right way to do it? Thanks in advance!
最满意答案
调用的URL应包括上下文路径,即部署应用程序的路径。 这通常是包含应用程序的文件夹的名称或WAR文件的名称,不带.war扩展名。 因此,如果您的应用程序位于文件夹xxx.war ,请尝试url: "/xxx/todo" 。
还要检查Firebug以查看被调用的确切URL以及服务器的响应。
THe URL called should include the context path, i.e. the path under which the application is deployed. This is usually the name of the folder that contains the application or the name of the WAR file, without the .war extension. So if your application is located in the folder xxx.war, try url: "/xxx/todo".
Also check with Firebug to see the exact URL that gets called and the response from the server.
更多推荐
发布评论