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

时间:2016-09-10 03:02:17

标签: java spring-mvc servlets

在我的示例Java / Spring-MVC / Webapp(github project)中,当我尝试打开[http://localhost:8080/bookflix/querybook.html][2]时,我看到一个页面。

  1. 正常加载http://localhost:8080/bookflix/querybook.html
  2. 点击“搜索”按钮会驱动浏览器加载http://localhost:8080/bookflix/query并转到404.
  3. 我不明白为什么GET / bookflix / query会导致404.任何人都可以帮我解决这个问题。我的github项目在上面链接。代码片段如下。

    的web.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

    <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中

    <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

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

1 个答案:

答案 0 :(得分:0)

经过一些调试,我意识到了这个问题。
我在任何地方都使用了无关的<button onClick={function() {this.handleRedirection(user.userName)}.bind(this)}> ,这导致了不正确的URL命名空间。删除了,应用程序工作。
github commit解决了这个问题。

感谢您的帮助。