首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过注释配置spring boot,以便在web.xml中拥有类似于?

如何通过注释配置spring boot,以便在web.xml中拥有类似于?
EN

Stack Overflow用户
提问于 2014-06-19 03:48:47
回答 3查看 1.5K关注 0票数 2

如何通过注释配置spring boot,以获得类似于web.xml中的内容?

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2020-02-21 00:42:16

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

代码语言:javascript
复制
@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);
    }

}

以下是我的项目结构

这是我的toDos.jsp

代码语言:javascript
复制
<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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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
EN

Stack Overflow用户

发布于 2014-06-22 22:49:06

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

干杯。

票数 1
EN

Stack Overflow用户

发布于 2014-06-19 18:24:16

我不认为有一个Java API可以解决这个问题。你仍然可以在Spring Boot中使用web.xml。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24293901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档