相当于web.xml< jsp-config>用于Spring Boot MVC? [英] Equivalent of web.xml <jsp-config> for Spring Boot MVC?

查看:285
本文介绍了相当于web.xml< jsp-config>用于Spring Boot MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSP规范允许我使用web.xml中的<jsp-config>部分将.html文件用作JSP(即,容器将它们作为JSP文件处理),例如:

The JSP specification allows me to serve .html files as JSP (that is, have the container process them as JSP files) using a <jsp-config> section in web.xml, e.g.:

<web-app …>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.html</url-pattern>
    </jsp-property-group>
  </jsp-config>
</web-app>

但是当我切换到运行带有嵌入式Tomcat的@SpringBootApplication时,它会完全绕过web.xml文件. Spring Boot MVC中是否有等效的设置来按照标准web.xml设置JSP属性组的JSP配置,该配置将配置现有嵌入式Tomcat JSP servlet?

But when I switch to running a @SpringBootApplication with embedded Tomcat, it completely bypasses the web.xml file. Is there an equivalent setting in Spring Boot MVC to set the JSP configuration of a JSP property group, as per standard web.xml, that will configure the existing embedded Tomcat JSP servlet?

(我可能要配置的JSP设置的另一个示例是<trim-directive-whitespaces>.)

(Another example of a JSP setting I might want to configure is <trim-directive-whitespaces>.)

在将其标记为重复项之前,请仔细阅读. 我知道Walkeros的广泛的答案.但是,该答案仅考虑添加 JSP Servlet.它没有解决向现有 JSP servlet添加新的JSP属性组的问题,并且实际上根本没有提到web.xml中的<jsp-config>部分.

Before you mark this as a duplicate, please read this closely. I am aware of the extensive answer by walkeros. But that answer only considers adding a new JSP servlet. It does not address adding a new JSP property group to the existing JSP servlet, and indeed doesn't mention the <jsp-config> section in web.xml at all.

推荐答案

您可能会找到一种使Spring Boot与XML配置一起工作的方法,但这不是应该的. 要将特殊的JSP配置应用于您的应用程序,您可以按照以下步骤进行操作:

You may find a way to make Spring Boot work with XML configurations, but that is not the way it is supposed to be. To apply special JSP configurations to your application, you could do that as follows:

  • 创建一个实现 JspConfigDescriptor 的JSP配置类,例如:
  • Create a JSP configuration class that implements JspConfigDescriptor , ex:
public class MyJspConfigDescriptor implements JspConfigDescriptor {

    private Collection<JspPropertyGroupDescriptor> jspPropertyGroups =
            new LinkedHashSet<JspPropertyGroupDescriptor>();

    private Collection<TaglibDescriptor> taglibs =
            new HashSet<TaglibDescriptor>();

    @Override
    public Collection<JspPropertyGroupDescriptor> getJspPropertyGroups() {
        JspPropertyGroup newPropertyGroup = new JspPropertyGroup();
        newPropertyGroup.addUrlPattern("*.html");
        // You can add more configurations as you wish!
        JspPropertyGroupDescriptorImpl jspDescriptor = new JspPropertyGroupDescriptorImpl(newPropertyGroup);
        jspPropertyGroups.add(jspDescriptor);
        return jspPropertyGroups;
    }

    @Override
    public Collection<TaglibDescriptor> getTaglibs() {
        return taglibs;
    }

}

  • 通知您的 SpringBootServletInitializer 有要添加的新配置,可以通过覆盖 onStartup 方法并将其添加到其中来实现:
    • Inform your SpringBootServletInitializer that there are new configurations that you would like to add, you can do so by overriding the onStartup method and add them there:
    • @Override
          public void onStartup(ServletContext servletContext) throws ServletException {
              super.onStartup(servletContext);
              servletContext.getJspConfigDescriptor().getJspPropertyGroups().addAll((new MyJspConfigDescriptor()).getJspPropertyGroups());
          }
      

      我相信这是可行的,但是我并没有真正对其进行测试!

      I believe that this would work, but I didn't really test it!

      这篇关于相当于web.xml&lt; jsp-config&gt;用于Spring Boot MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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