【发布时间】:2017-01-18 05:24:47
【问题描述】:
在我的示例 Java/Spring-MVC/Webapp(github project) 中,当我尝试打开 [http://localhost:8080/bookflix/querybook.html][2],我看到一个页面。
- 加载
http://localhost:8080/bookflix/querybook.html顺利进行 - 点击“搜索”按钮会驱动浏览器加载
http://localhost:8080/bookflix/query并导致 404。
我不明白为什么 GET/bookflix/query 会导致 404。谁能帮我解决这个问题。我的 github 项目链接在上面。代码如下:sn-ps。
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) {
....
}
}
【问题讨论】:
-
为什么你认为你不应该得到 404?你的应用程序的哪一部分应该服务于请求,为什么?
-
@SotiriosDelimanolis 我的假设是,QueryController:helloWorld() 中的方法(带有 requestMapping: /bookflix/query)应该接收到 /bookflix/query 的 GET 请求。我还通过将 querybook.html 中的表单操作更改为 /bookflix/query 进行了双重检查,结果是相同的。如果我假设错误,请纠正。
-
我希望你更深入。为什么该方法应该接收请求?
-
根据 web.xml,任何像“bookflix/*”这样的 URL 模式都应该由 bookflix-servlet.xml 处理。 bookflix-servlet.xml 中的这一行(
) 将确保 QueryController.java(用@Controller 注释)可用于获取“/bookflix /查询,获取”。 -
组件扫描只发现和注册bean。它不做任何其他事情。它与 MVC 堆栈无关。
标签: java spring-mvc servlets