"偽の" MSWordファイルのリクエストをマップしようとしています。このファイルは実際にはHTMLページで、拡張子は.docで、ユーザーはダウンロードしてMS Wordで開くことができます。SpringMVC(jspページから).docファイルを返す方法は?
マッピングされたURLが.doc
拡張子が含まれていませんが、私は拡張子を追加する場合は、JSPファイル内のすべての<jsp:include />
は動作を停止し、同時にファイル場合は、ソースファイル/app/report.jsp
は、人口やHTMLページとして示されていますそれでもHTML文書としてレンダリングされます。
ユーザーにファイルをWord文書としてダウンロードさせるにはどうすればよいですか?ここで
は私のコントローラから、URLをマッピングする方法であって、
<?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をです:ここ
@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"?>
<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>
EDIT
(* .jspf部分は含まない)ページの不完全な解析の問題は、問題を解決しようとしているときに私がした変更に関連しています。 web.xml
から次のコードを削除しました。復元されたとき、すべてが期待通りに働いていた:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspf</url-pattern>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
ビュー名を返す場合は、対応するJSPの内容が応答全体であることが予想されます。 –
リクエストが.doc URLにマップされたコントローラから転送されただけなので、JSPのインクルードが失敗する可能性があります。 JSPのコードは何ですか? JSPには、<%@ page contentType = "application/msword"%> ' –
@SotiriosDelimanolisが必要です。実際、対応するJSPがすべての応答になると思います。残念ながら、「includes」は含まれませんが、RequestMappingアノテーション値に「.doc」という拡張子がある場合にのみ – furins