简体   繁体   中英

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.

  1. Loading http://localhost:8080/bookflix/querybook.html happens smoothly
  2. Hitting 'Search' button drives the browser to load http://localhost:8080/bookflix/query and leads to a 404.

I do not understand why GET/bookflix/query leads to a 404. Can anyone help me in fixing this. My github project is linked above. Code snippets below.

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) {
        ....
    }
}

After some debugging I realized the issue.
I was using an extraneous /bookflix/ everywhere, which was leading into incorrect URL namespace. Removed that, and the application worked.
This github commit fixes the problem.

Thanks for helping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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