如何通过注释配置spring boot以便在web.xml中有类似<jsp-config>的东西?</jsp-config>

时间:2014-06-18 19:48:47

标签: spring-boot

如何通过注释配置spring boot以便在web.xml中有类似的内容?

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspf</url-pattern>
        <page-encoding>UTF-8</page-encoding>
        <scripting-invalid>false</scripting-invalid>
        <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
        <default-content-type>text/html</default-content-type>
    </jsp-property-group>
</jsp-config>

3 个答案:

答案 0 :(得分:1)

我认为Dave给出了一个非常直截了当的答案 - 也就是说 - 没有Servlet规范定义的Java API来配置JSP,如果你必须将它与Spring Boot一起使用,你只需要使用完全Spring Boot的web.xml支持。这听起来很清楚。

干杯。

答案 1 :(得分:1)

可能已经晚了,但希望它将对其他想要相同事物的人有所帮助。我试图做同样的事情。我开始学习spring boot 2,并尝试将我简单的spring mvc应用程序之一迁移到spring boot。在我的春季mvc中,我使用带模板的web.xml和jsp。尽管web.xml很小,但是它包含了<jsp-config>...</jsp-config>部分。这就是我在SpringBoot主文件中所做的,并且可以正常工作

@SpringBootApplication
public class SpringBootSimpleApp extends SpringBootServletInitializer  {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootSimpleApp.class);
    }

    /**
     *  <jsp-config>
            <jsp-property-group>
                <url-pattern>*.jsp</url-pattern>
                <url-pattern>*.jspf</url-pattern>
                <page-encoding>UTF-8</page-encoding>

                <!-- This change proves that you have replaced all Java code in your JSPs because any JSPs with Java code cannot compile 
                    with this setting enabled.
                -->
                <scripting-invalid>true</scripting-invalid>
                <include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
                <trim-directive-whitespaces>true</trim-directive-whitespaces>
                <default-content-type>text/html</default-content-type>
            </jsp-property-group>
        </jsp-config>
    */
    @Bean 
    public ConfigurableServletWebServerFactory configurableServletWebServerFactory ( ) { 
        return new TomcatServletWebServerFactory() { 
            @Override 
            protected void postProcessContext(Context context) { 
                super.postProcessContext(context); 
                JspPropertyGroup jspPropertyGroup = new JspPropertyGroup(); 
                jspPropertyGroup.addUrlPattern("*.jsp");
                jspPropertyGroup.addUrlPattern("*.jspf");
                jspPropertyGroup.setPageEncoding("UTF-8");
                jspPropertyGroup.setScriptingInvalid("true");
                jspPropertyGroup.addIncludePrelude("/WEB-INF/jsp/base.jspf");
                jspPropertyGroup.setTrimWhitespace("true");
                jspPropertyGroup.setDefaultContentType("text/html"); 
                JspPropertyGroupDescriptorImpl jspPropertyGroupDescriptor = new JspPropertyGroupDescriptorImpl(jspPropertyGroup); 
                context.setJspConfigDescriptor(new JspConfigDescriptorImpl(Collections.singletonList(jspPropertyGroupDescriptor), Collections.emptyList())); 
            } 
        }; 
    }

    public static void main( String[] args ) {
        SpringApplication.run(SpringBootSimpleApp.class, args);
    }

}

这是我的项目结构

Spring Boot project structure

这是我的toDos.jsp

<template:main htmlTitle="Landing Page" >
    <div class="container theme-showcase" role="main">
        <div class="jumbotron">
            <h1>ToDo Application</h1>
            <p>A simple Rest API Spring MVC application</p>
        </div>
        <div class="page-header">
            <h1>API</h1>
            <a href="<c:url value = "/services/rest/toDos/get"/>">Current ToDos</a>
        </div>
    </div>
</template:main>

main.tag

<%@ tag body-content="scriptless" trimDirectiveWhitespaces="true" %>
<%@ attribute name="htmlTitle" type="java.lang.String" rtexprvalue="true" required="true" %>
<%@ attribute name="headContent" fragment="true" required="false" %>
<%@ include file="/WEB-INF/jsp/base.jspf" %>
<!DOCTYPE html>
    <head>
        <title>Spring Security &mdash; SAML 2.0 Service Provider :: <c:out value="${fn:trim(htmlTitle)}" /></title>
        <link rel="stylesheet" href="<c:url value="/webjars/bootstrap/4.4.1/css/bootstrap.css" />" />
        <script type="text/javascript" src="<c:url value="/webjars/jquery/3.4.1/jquery.js" />" ></script>
        <script type="text/javascript" src="<c:url value="/webjars/bootstrap/4.4.1/js/bootstrap.js" />" ></script>
        <jsp:invoke fragment="headContent" />
    </head>
    <body>
        <jsp:doBody />
    </body>
</html>

base.jspf

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="template" tagdir="/WEB-INF/tags/template" %>

希望这个答案对其他人有帮助。我只是在学习spring boot2时才这样做。

谢谢

答案 2 :(得分:0)

我不认为有这种东西的Java API。你仍然可以在Spring Boot中使用web.xml。