同时使用 Thymeleaf 和 JSP
Using both Thymeleaf and JSP
我正在使用 JSP + JSTL,但我对 c:if、c:choose、...
感到厌烦
所以,我希望我的 JSP 页面同时使用 JSP 和 Thymeleaf 呈现(我计划尽快删除所有 JSTL)。我正在使用 Spring MVC 框架:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="2" />
</bean>
在我的控制器中,我只是 return jsp 没有扩展名。
return "folder/page";
我的 JSP 页面可以先用 JSP 解析器呈现,然后再用 Thymeleaf 解析器呈现吗?如果是,如何?
好像把JSP和Thymeleaf串起来很复杂。因此,我想对 JSP 文件使用内部解析器,对 HTML 文件使用 Thymeleaf 模板解析器。我该怎么做?
根据 Thymeleaf 论坛上的 this post,您有两种解决方案。
第一个解 :
删除 bean 声明中的后缀 属性(<property name="suffix" value=".html" />
和 <property name="suffix" value=".jsp" />
)并传递后缀 在您的控制器的 return 值中,例如:
@RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
@RequestMapping("/view2")
public String jspView(){
return "myjspview.html";
}
第二种解法:
将 viewNames
属性 添加到解析器。该值是包含视图的文件夹的名称,具体取决于它们的扩展名。因此,您将有一个文件夹用于 JSP 文件,另一个文件夹用于 HTML (thymeleaf) 文件,例如:
配置
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="viewNames" value="thymeleaf/*" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="viewNames" value="jsp/*" />
<property name="suffix" value=".jsp" />
</bean>
控制器
@RequestMapping("/view1")
public String thymeleafView() {
return "thymeleaf/mythymeleafview";
}
@RequestMapping("/view2")
public String jspView() {
return "jsp/myjspview";
}
项目文件夹
WEB-INF/views/jsp/myjspview.jsp
WEB-INF/views/thymeleaf/mythymeleafview.jsp
这两种解决方案都有效,但有一些限制。您必须指定一种或另一种方式,无论您是想用 JSP 还是 Thymeleaf 解决。
链接 JSP 和 Thymeleaf 的 "perfect" 解决方案 — 这将包括在无法使用 Thymeleaf 或 解析视图时尝试使用 JSP 解析视图反之亦然 — 这是不可能的,Daniel Fernández(Thymeleaf 团队)在 this same post 中解释了原因:
Thymeleaf allows you to create whichever ITemplateResolver implementation you wish, including some that might not allow to determine whether the template exists or not before actually reading it. [...] So, there is no way for Thymeleaf to be sure whether a template will be resolvable or not before trying to process the template. And that's why the ThymeleafViewResolver has to resort to the "viewNames" property.
这里是根据@Igd 的回复
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="viewNames" value="*.jsp" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="templateMode" value="HTML5" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewNames" value="redirect*" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*.html" />
</bean>
我将其用于映射:
@RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
@RequestMapping("/view2")
public String jspView(){
return "myjspview.jsp";
}
或者,两个 servlet 工作正常。关键是保持 servlet 配置最少,并为数据库和其他服务包含一个 appConfig.xml(这避免了大量重复配置)
Web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>AssessmentAdmin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdmin</servlet-name>
<url-pattern>/xz/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AssessmentAdminTL</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdminTL</servlet-name>
<url-pattern>/xztl/*</url-pattern>
</servlet-mapping>
........
jsp 的 servlet:
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- <property name="viewNames" value="jsp/*" />-->
<property name="suffix" value=".jsp" />
</bean>
..........
<import resource="applicationContext.xml" />
</beans>
百里香的 servlet
<mvc:annotation-driven />
<!-- Thymeleaf -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<import resource="applicationContext.xml" />
试过了,效果很好
根据@Athanor的回答,我们可能还有其他选择。
我们使用 属性 "viewNames" 来控制模板的解析器 select
<!-- jsp -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
<property name="viewNames" value="*admin/*,*packer/*,*courier/*,/" />
</bean>
<!-- thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*thymeleaf/*" />
<property name="order" value="2" />
</bean>
和控制器
@RequestMapping(value="/test")
public ModelAndView dboxPrint(Model model){
ModelAndView modelAndView = new ModelAndView("thymeleaf/dbox_print");
return modelAndView;
}
我正在使用 JSP + JSTL,但我对 c:if、c:choose、...
感到厌烦所以,我希望我的 JSP 页面同时使用 JSP 和 Thymeleaf 呈现(我计划尽快删除所有 JSTL)。我正在使用 Spring MVC 框架:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="2" />
</bean>
在我的控制器中,我只是 return jsp 没有扩展名。
return "folder/page";
我的 JSP 页面可以先用 JSP 解析器呈现,然后再用 Thymeleaf 解析器呈现吗?如果是,如何?
好像把JSP和Thymeleaf串起来很复杂。因此,我想对 JSP 文件使用内部解析器,对 HTML 文件使用 Thymeleaf 模板解析器。我该怎么做?
根据 Thymeleaf 论坛上的 this post,您有两种解决方案。
第一个解 :
删除 bean 声明中的后缀 属性(<property name="suffix" value=".html" />
和 <property name="suffix" value=".jsp" />
)并传递后缀 在您的控制器的 return 值中,例如:
@RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
@RequestMapping("/view2")
public String jspView(){
return "myjspview.html";
}
第二种解法:
将 viewNames
属性 添加到解析器。该值是包含视图的文件夹的名称,具体取决于它们的扩展名。因此,您将有一个文件夹用于 JSP 文件,另一个文件夹用于 HTML (thymeleaf) 文件,例如:
配置
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="viewNames" value="thymeleaf/*" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="viewNames" value="jsp/*" />
<property name="suffix" value=".jsp" />
</bean>
控制器
@RequestMapping("/view1")
public String thymeleafView() {
return "thymeleaf/mythymeleafview";
}
@RequestMapping("/view2")
public String jspView() {
return "jsp/myjspview";
}
项目文件夹
WEB-INF/views/jsp/myjspview.jsp
WEB-INF/views/thymeleaf/mythymeleafview.jsp
这两种解决方案都有效,但有一些限制。您必须指定一种或另一种方式,无论您是想用 JSP 还是 Thymeleaf 解决。
链接 JSP 和 Thymeleaf 的 "perfect" 解决方案 — 这将包括在无法使用 Thymeleaf 或 解析视图时尝试使用 JSP 解析视图反之亦然 — 这是不可能的,Daniel Fernández(Thymeleaf 团队)在 this same post 中解释了原因:
Thymeleaf allows you to create whichever ITemplateResolver implementation you wish, including some that might not allow to determine whether the template exists or not before actually reading it. [...] So, there is no way for Thymeleaf to be sure whether a template will be resolvable or not before trying to process the template. And that's why the ThymeleafViewResolver has to resort to the "viewNames" property.
这里是根据@Igd 的回复
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="viewNames" value="*.jsp" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="templateMode" value="HTML5" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewNames" value="redirect*" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*.html" />
</bean>
我将其用于映射:
@RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
@RequestMapping("/view2")
public String jspView(){
return "myjspview.jsp";
}
或者,两个 servlet 工作正常。关键是保持 servlet 配置最少,并为数据库和其他服务包含一个 appConfig.xml(这避免了大量重复配置)
Web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>AssessmentAdmin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdmin</servlet-name>
<url-pattern>/xz/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AssessmentAdminTL</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdminTL</servlet-name>
<url-pattern>/xztl/*</url-pattern>
</servlet-mapping>
........
jsp 的 servlet:
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- <property name="viewNames" value="jsp/*" />-->
<property name="suffix" value=".jsp" />
</bean>
..........
<import resource="applicationContext.xml" />
</beans>
百里香的 servlet
<mvc:annotation-driven />
<!-- Thymeleaf -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<import resource="applicationContext.xml" />
试过了,效果很好
根据@Athanor的回答,我们可能还有其他选择。
我们使用 属性 "viewNames" 来控制模板的解析器 select
<!-- jsp -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
<property name="viewNames" value="*admin/*,*packer/*,*courier/*,/" />
</bean>
<!-- thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*thymeleaf/*" />
<property name="order" value="2" />
</bean>
和控制器
@RequestMapping(value="/test")
public ModelAndView dboxPrint(Model model){
ModelAndView modelAndView = new ModelAndView("thymeleaf/dbox_print");
return modelAndView;
}