我正在尝试映射“假”MSWord文件的请求:该文件实际上是一个html页面,扩展名为.doc,强制用户下载并用MS Word打开它。
如果映射的网址不包含/app/report.jsp
扩展名,则填充源文件.doc
并显示为HTML网页,但如果我在jsp中添加了<jsp:include />
扩展名文件停止工作,同时文件仍然呈现为html文档。
如何强制用户将文件下载为Word文档?
以下是从我的控制器映射URL的方法:
@RequestMapping(value="/report.doc", produces = "application/msword")
public ModelAndView reportProducer(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("something", false);
mav.setViewName("/app/report");
response.setContentType("application/msword"); // probably unnecessary?
return mav;
}
这是 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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="3.1">
<display-name>MyApp</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/classes/jsp/app/errorPage.jsp</location>
</error-page>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我的 applicationContext.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<mvc:view-controller path="/" view-name="home"/>
<context:annotation-config/>
<context:component-scan base-package="myBasePackage1, myBasePackage2"/>
[... datasources, beans omitted ...]
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/classes/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<小时/> 的修改
页面解析不完整(不包括* .jspf部分)的问题与我在尝试解决问题时所做的更改有关。我从web.xml
中删除了以下代码。恢复后,一切都按预期工作:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspf</url-pattern>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
答案 0 :(得分:1)
jspf文件通常是JSP片段,应该静态包含,而不是动态包含。我不会将这个标准文件扩展名用于常规JSP。只需使用.jsp扩展名即可。这将使您的自定义jsp servlet映射变得不必要,并且不会让人感到困惑。
WEB-INF / classes用于类加载器加载的类和资源。 JSP不应该位于那里。我会把它们放在WEB-INF下的任何其他地方。
最后,关于渲染为网页,这是因为JSP设置的默认内容类型是text / html。你需要添加
<%@ page contentType="application/msword" %>
在JSP的顶部设置适当的内容类型。