Java 如何在Spring MVC中集成多视图技术
在SpringMVC中是否可以使用双视图技术 假设我想要一个JSP和Velocity 在dispatcher-servlet.xml上Java 如何在Spring MVC中集成多视图技术,java,spring,jsp,spring-mvc,velocity,Java,Spring,Jsp,Spring Mvc,Velocity,在SpringMVC中是否可以使用双视图技术 假设我想要一个JSP和Velocity 在dispatcher-servlet.xml上 <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="spring-views"/> </bean> 我找了一整天,找不到任何答案。感谢您的帮助。谢
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views"/>
</bean>
我找了一整天,找不到任何答案。感谢您的帮助。谢谢 将Velocity配置添加到dispatcher servlet:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
</bean>
您可以在应用程序中使用任意数量的视图技术,前提是您已为每种技术配置了视图解析程序,并且它们之间没有冲突(在这方面,应选择控制器返回的视图)。是一个具有完整XML配置和实际视图的极好示例。这里是一个同时使用velocity视图解析器和内部视图解析器的示例。此外,我还派生了spring VelocityLayoutView,以便能够在2.0版中使用velocity工具。velocity resolver的顺序是10,internal的顺序是20,这样velocity resolver就有机会在所有解析的内部解析程序之前解析其视图。速度视图的输出强制为UTF-8
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="viewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="20">
</bean>
<bean class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"
id="vmViewResolver" p:order="10" p:suffix=".vm" p:prefix=""
p:cache="true" p:contentType="text/html;charset=UTF-8"
p:exposeRequestAttributes="false" p:exposeSessionAttributes="false"
p:exposePathVariables="true" p:exposeSpringMacroHelpers="true"
p:dateToolAttribute="date" p:toolboxConfigLocation="/WEB-INF/toolbox.xml"
p:viewClass="org.sba.views.Velocity2LayoutView">
<property name="attributesMap">
<map>
<entry key="messageSource" value-ref="messageSource"/>
</map>
</property>
</bean>
这是修改后的VelocityView:
public class Velocity2LayoutView extends VelocityLayoutView {
ViewToolManager toolManager;
@Override
protected Context createVelocityContext(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
ViewToolContext context = toolManager.createContext(request, response);
context.putAll(model);
return context;
}
@Override
protected void initTool(Object tool, Context velocityContext) throws Exception {
}
@Override
public void afterPropertiesSet() throws Exception {
toolManager = new ViewToolManager(getServletContext(), false, false);
if (getToolboxConfigLocation() != null) {
XmlFactoryConfiguration config = new XmlFactoryConfiguration();
config.read(getServletContext()
.getResourceAsStream(getToolboxConfigLocation()));
toolManager.configure(config);
}
toolManager.setVelocityEngine(getVelocityEngine());
}
}
公共类Velocity2LayoutView扩展了VelocityLayoutView{
视图工具管理器工具管理器;
@凌驾
受保护的上下文createVelocityContext(映射模型,
HttpServletRequest请求、HttpServletResponse响应)引发异常{
ViewToolContext上下文=toolManager.createContext(请求、响应);
context.putAll(模型);
返回上下文;
}
@凌驾
受保护的void initTool(对象工具,上下文velocityContext)引发异常{
}
@凌驾
public void afterPropertieSet()引发异常{
toolManager=新的ViewToolManager(getServletContext(),false,false);
if(getToolboxConfigLocation()!=null){
XmlFactoryConfiguration config=新的XmlFactoryConfiguration();
config.read(getServletContext()
.getResourceAsStream(getToolboxConfigLocation());
toolManager.configure(配置);
}
setVelocityEngine(getVelocityEngine());
}
}
Yes的可能重复项只要路径是分开的。@Stefan我一直有这样的错误:org.springframework.beans.factory.BeanCreationException:创建名为“image list”的bean时出错:初始化bean失败;嵌套异常为org.springframework.context.ApplicationContextException:必须在此web应用程序上下文中定义单个VelocityConfig bean(可能会被继承):VelocityConfigure是常用的实现。这个豆子可以叫任何名字。;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[org.springframework.web.servlet.view.velocity.VelocityConfig]类型的符合条件的bean它工作了现在我将.vm文件移动到我的类路径。谢谢,但现在它说请求处理失败;嵌套异常为org.apache.velocity.exception.ResourceNotFoundException:找不到资源“/WEB-INF/velocity/image list.vm”该文件存在吗?(/WEB-INF/velocity/image list.vm)是的,它存在,控制器似乎没有读取它。这就是它的实际外观:List images=userService.getImagesByUserId;ModelAndView ModelAndView=新的ModelAndView(“图像列表”);添加对象(“图像”,图像);addObject(“imagePath”,imagePath);addObject(“userId”,userId);返回模型和视图;无法读取文件,因为类路径中没有。例如,您可以将它们放在src/velocity/image-list.vm下面,或者配置从不同的目录加载它们。
public class Velocity2LayoutView extends VelocityLayoutView {
ViewToolManager toolManager;
@Override
protected Context createVelocityContext(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
ViewToolContext context = toolManager.createContext(request, response);
context.putAll(model);
return context;
}
@Override
protected void initTool(Object tool, Context velocityContext) throws Exception {
}
@Override
public void afterPropertiesSet() throws Exception {
toolManager = new ViewToolManager(getServletContext(), false, false);
if (getToolboxConfigLocation() != null) {
XmlFactoryConfiguration config = new XmlFactoryConfiguration();
config.read(getServletContext()
.getResourceAsStream(getToolboxConfigLocation()));
toolManager.configure(config);
}
toolManager.setVelocityEngine(getVelocityEngine());
}
}