简体   繁体   English

Spring MVC + Java Servlets 中的 URL 映射错误

[英]Error in URL Mapping in Spring MVC + Java Servlets

In my sample Java/Spring-MVC/Webapp( github project ), When I try to open [ http://localhost:8080/bookflix/querybook.html][2] , I see a page.在我的示例 Java/Spring-MVC/Webapp( github 项目)中,当我尝试打开 [ http://localhost:8080/bookflix/querybook.html][2] 时,我看到了一个页面。

  1. Loading http://localhost:8080/bookflix/querybook.html happens smoothly加载http://localhost:8080/bookflix/querybook.html顺利
  2. Hitting 'Search' button drives the browser to load http://localhost:8080/bookflix/query and leads to a 404.点击“搜索”按钮驱动浏览器加载http://localhost:8080/bookflix/query并导致 404。

I do not understand why GET/bookflix/query leads to a 404. Can anyone help me in fixing this.我不明白为什么 GET/bookflix/query 会导致 404。谁能帮我解决这个问题。 My github project is linked above.我的github项目在上面链接。 Code snippets below.下面的代码片段。

web.xml网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>bookflix-java</display-name>
  <welcome-file-list>
    <welcome-file>querybook.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>bookflix</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>bookflix</servlet-name>
    <url-pattern>/bookflix/*</url-pattern>
  </servlet-mapping>
</web-app>

querybook.html查询书.html

<body>
<div id='container' class="container">
  <div class="row">
    <div class="col-lg-12">
      <h2>Yet Another e-Bookshop</h2>
      <form method="get" action="query">
        Choose an author:<br /><br />
        <input type="checkbox" name="author" value="Tan Ah Teck" />Ah Teck
        <input type="checkbox" name="author" value="Mohammad Ali" />Ali
        <input type="checkbox" name="author" value="Kumar" />Kumar
        <input type="submit" value="Search" />
      </form>
    </div>
  </div><!--/row-->
</div> <!-- /container -->
</body>

bookflix-servlet.xml bookflix-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.bookflix.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean> 
</beans>

QueryController.java查询控制器.java

... imports here ...
@Controller
public class QueryController {

    @RequestMapping(value="/bookflix/query", method=RequestMethod.GET)
    public void helloWorld(HttpServletRequest request, HttpServletResponse response) {
        ....
    }
}

After some debugging I realized the issue.经过一些调试,我意识到了这个问题。
I was using an extraneous /bookflix/ everywhere, which was leading into incorrect URL namespace.我在任何地方都使用了一个无关的/bookflix/ ,这导致了不正确的 URL 命名空间。 Removed that, and the application worked.删除它,应用程序就可以工作了。
This github commit fixes the problem.这个github 提交解决了这个问题。

Thanks for helping.谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM