gpt4 book ai didi

java - Spring MVC : Several ViewResolvers

转载 作者:行者123 更新时间:2023-11-28 23:39:51 25 4
gpt4 key购买 nike

一个按预期工作,但我想要两个 View 解析器,这可能吗?

<bean id="App_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1"/>
<property name="prefix" value="/WEB-INF/views/App/"/>
<property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"/>
<property name="prefix" value="/WEB-INF/views/OtherApp/"/>
<property name="suffix" value=".jsp"/>
</bean>

??

我想渲染一个 View ,通过调用

new ModelAndView("start/start"); 

如果它存在于第一个 View 解析器中,则渲染,否则尝试下一个。

InternalResourceViewResolver 显然不会像其他地方所说的那样返回 false,但是还有哪些 JSP View 解析器可以用于此目的?

扩展资源 View 解析器?

最佳答案

给你,这是答案:

import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

public class ViewResolver extends InternalResourceViewResolver {

protected Class<?> requiredViewClass() {
return View.class;
}

public static class View extends InternalResourceView {
public boolean checkResource(Locale locale) throws Exception {
if (getServletContext().getResource(getUrl()) != null ) {
return true;
}

return false;
}
}
}


<bean id="App_viewResolver" class="package.ViewResolver">
<property name="order" value="1"/>
<property name="prefix" value="/views/App"/>
<property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="package.ViewResolver">
<property name="order" value="2"/>
<property name="prefix" value="/WEB-INF/OtherApp"/>
<property name="suffix" value=".jsp"/>
</bean>

关于java - Spring MVC : Several ViewResolvers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com