在 Spring Boot 中混合 thymeleaf 和 jsp 文件 [英] Mixing thymeleaf and jsp files in Spring Boot

查看:39
本文介绍了在 Spring Boot 中混合 thymeleaf 和 jsp 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,在 Spring Boot/MVC 中使用控制器是否可以使用没有控制器与 thymeleaf 视图混合的普通 jsp 文件?

Is there anyway to use plain jsp files without a controller mixed with thymeleaf views using controllers in Spring Boot/MVC?

例如一个标准的控制器如下;

For example, a standard controller is as follows;

@RequestMapping("/")
public String get() {
    return "view";
}

这完全按预期工作,但是否可以将放在文件夹中的.jsp"文件映射到任何 URL,例如 test.jsp 将转到 www.website.com/test.jsp

Which works exactly as expected, but is it possible to map ".jsp" files put in a folder to any url for example test.jsp would go to www.website.com/test.jsp

问候.

推荐答案

我在servlet-context.xml(DispatcherServlet的contextConfigLocation)中使用了如下配置:

I use the following configuration in servlet-context.xml (the contextConfigLocation of the DispatcherServlet):

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
    <beans:property name="viewNames" value="jsp/*"></beans:property>
    <beans:property name="order" value="1"></beans:property>
</beans:bean>

<!-- Thymeleaf -->
<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <beans:property name="templateEngine" ref="templateEngine" />
    <!-- This narrows the scope of the resolver to the view names (returned 
        by the controllers' methods) to those matching the pattern -->
    <beans:property name="viewNames" value="templates/*"></beans:property>
    <!-- places this resolver before the default InternalResourceViewResolver -->
    <beans:property name="order" value="0"></beans:property>
</beans:bean>

<beans:bean id="templateResolver"
    class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="templateMode" value="HTML5" />
    <beans:property name="suffix" value=".html" />
</beans:bean>

<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>

<!-- Thymeleaf _ end -->

为了使用 JSP 呈现响应,我在控制器中返回如下字符串:

In order to render the response with a JSP, in the controller I return a string like the following:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {

    return "jsp/home";
}

虽然使用 thymeleaf 模板进行渲染,但我执行以下操作:

whereas to render with a thymeleaf template, I do the following:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getRanks(Model model, HttpServletRequest request)
{
    return "templates/user/ranks";
}

当然,.jsp.html 文件的物理位置必须与控制器返回的视图名称相匹配.因此,根据解析器的配置方式,您必须将 .jsp 文件放在 /views/jsp.html 文件中(对于thymeleaf) 内 /views/templates

Of course, the physical location of the .jsp and .html files must match the view name returned by the controllers. So, according to how the resolvers are configured, you will have to place .jsp files inside /views/jsp and .html files (for thymeleaf) inside /views/templates

如果你希望像 www.website.com/test.jsp 这样的 url 用 test.jsp 视图呈现,你可以简单地把 aformentiond jsp 文件在您的 web 应用程序的根目录中.如果没有奇怪的配置,该请求应该由 JSP servlet 解析.实际上,像 Tomcat 这样的 servlet 容器通常在其全局 web.xml 文件中都有这个规范:

If you want a url like www.website.com/test.jsp to be rendered with the test.jsp view, you can simply put the aformentiond jsp file inside the root directory of your webapp. The request should be resolved by the JSP servlet, if no strange configuration is in place. In fact, a servlet container like Tomcat normally have this specification in its global web.xml file:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<!-- The mappings for the JSP servlet -->
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

如果您想要更自定义和更具体的内容,则必须相应地修改您的配置.例如,从 servlet 容器的 web.xml 中删除 JSP servlet 并在适当的控制器中映射 *.jsp url

If you want something more customized and specific, you will have to modify your configuration accordingly. For example, remove the JSP servlet from you servlet container's web.xml and map *.jsp url in the appropriate controllers

这篇关于在 Spring Boot 中混合 thymeleaf 和 jsp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆