在原始旧系统中,有一个JSP文件,它在WEB-INF / views中动态生成,并在spring-mvc.xml中具有以下配置。
<mvc:view-controller path="/downloadAppPage" view-name="downloadAppPage" />
<!-- mvc view -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:contentType="text/html; charset=utf-8" />
但是现在,我通过Spring Boot加载和嵌入式tomcat启动(jar -jar * .war)来更改原始系统。并且将不再访问downloadAppPage。
是否可以配置或编写Application以在Spring Boot上下文路径之外提供外部JSP文件?
谢谢, 詹姆斯。
答案 0 :(得分:0)
您可以将其他类型的视图解析器bean添加到配置中,从而添加您的外部文件夹。它们将按照您将它们放入配置中的顺序进行处理。您也可以编写自己的视图解析器。
public class MyViewResolver extends InternalResourceViewResolver {
@Override
protected AbstractUrlBasedView buildView(String viewName) throws Exception {
HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
String viewFile =request.getRealPath(this.getPrefix()+viewName+this.getSuffix());
File reqFile=new File(viewFile);
System.out.println(reqFile.getAbsolutePath()+":exists:"+reqFile.exists());
if(!reqFile.exists()){
viewName="default";
}
InternalResourceView view = (InternalResourceView) super.buildView(viewName);
view.setAlwaysInclude(false);
return view;
}
}
// example extracted from https://javabeat.net/extending-viewresolver-and-chaining-viewresolvers-in-spring-mvc/
然后,您的配置文件将如下所示:
<mvc:view-controller path="/downloadAppPage" view-name="downloadAppPage" />
<!-- mvc view -->
<bean class="mypack.MyViewResolver">
<property name="prefix" value="/your/new/path/" />
<property name="suffix" value=".jsp" />
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>