需要什么:
在地址http://localhost:8080/
的Tomcat上运行简单的Web应用程序。
应添加以下URL的处理程序:
GET http://localhost:8080/request/report/custom_report?from=2013-10-12&to=2014-10-12&download=true
只会写入HttpServletResponse
一些数据,即不涉及任何视图。
做了什么:
根据DispatcherServlet
,web.xml
的映射已添加到<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/request/*</url-pattern>
<!-- PLEASE NOTE that mapping to /* is not an option -->
</servlet-mapping>
spring-webmvc-4.0.5.RELEASE
现在,因为使用了最新的XML or Java
,我想添加上面提到的最小package org.yura.servlet.spring;
@Controller
public class SpringRequestController {
@RequestMapping(value = "/report/custom_report",
method = GET,
produces = "application/pdf")
public void getCustomReport(
@RequestParam("from") @DateTimeFormat(pattern = "yyyy-MM-dd") final Date from,
@RequestParam("to") @DateTimeFormat(pattern = "yyyy-MM-dd") final Date to,
@RequestParam("download") final boolean download,
final HttpServletResponse response) throws IOException {
takeParamsAndWriteReportAsPdfToServletResponse(from, to, download, response.getOutputStream());
}
配置的处理程序,所以我创建了控制器类:
Controller
然后,为了让这个springDispatcher-servlet.xml
被选中&#34;通过Spring,我将web.xml
放在WEB-INF
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="org.yura.servlet.spring" />
</beans>
文件夹旁边,并配置了以下配置(请告知是否可以进一步简化):
springDispatcher-servlet.xml
问题
使用此配置,在启动Tomcat并导航到上述URL后,我得到错误404 。
问题1 :请告知处理程序网址有什么问题 - 我应该指定它们是相对的还是什么? (因为根据日志,DispatcherServlet是正常创建的)
问题2 :是否可以将配置从Controller
移到我的{{1}}类,以免在多个文件中分散请求处理逻辑。
提前致谢...
答案 0 :(得分:1)
您尚未启用MVC堆栈。添加
<mvc:annotation-driven />
到您的springDispatcher-servlet.xml
(以及相应的命名空间)。
springDispatcher-servlet.xml
中的配置不仅仅是请求处理配置。它可以包含任何bean声明。如果有的话,您可以将其移动到Java配置,但它不应该是您的@Controller
源代码的一部分。