GVKun编程网logo

spring boot tomcat jsp

6

在本文中,我们将给您介绍关于springboottomcatjsp的详细内容,此外,我们还将为您提供关于#SpringBoot|怎样启动tomcat以及怎样配置tomcat。、nginxtomcats

在本文中,我们将给您介绍关于spring boot tomcat jsp的详细内容,此外,我们还将为您提供关于# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。、nginx tomcat spring-boot 对 json 等数据压缩、spring boot + jsp 发布到tomcat后el表达式无效、spring boot 2 内嵌Tomcat Stopping service [Tomcat]的知识。

本文目录一览:

spring boot tomcat jsp

spring boot tomcat jsp

spring boot 1.3.6.RELEASE

 

一、 spring boot tomcat jsp

1.   maven webapp为例

pom.xml所需配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

spring mvc 配置

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

启动类

@SpringBootApplication
public class App extends SpringBootServletInitializer {

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

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

controller

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping
    public String index(Model model) {
        model.addAttribute("s", "s");
        return "user";
    }
}

三种启动方式:

1)   找到target目录下打包好的war文件,使用下面命令运行

java –jar xx.war

2)   使用spring-boot-maven-plugin插件运行

a)   在pom.xml所在目录以命令方式运行:

mvn spring-boot:run

b)   使用idea时:

3)   将项目放到tomcat中运行

启动方法必需继承SpringBootServletInitializer

 

*如果el表达式没有生效

1.在jsp页面加上

     <%@ page isELIgnored="false"%>

2.查看项目是否包含web.xml

     如果是2.3,下图所示:

   

    默认是开启了忽略el表达式的,换为更高版本的即可

# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。

# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。

SpringBoot | 怎样启动tomcat以及默认的tomcat配置。

标签(空格分隔): springboot tomcat


因为springboot已经被大部分公司运用,所以基于springboot 来讲解tomcat。

  • springboot 怎样引入的tomcat
  • springboot 怎样创建一个tomcat实例
  • springboot 从哪里读取tomcat配置
  • springboot 中tomcat的配置详解

本文主要讲解这三个问题

springboot 怎样引入的tomcat

当我们在项目中加入

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

加入这个依赖后,maven会把tomcat的一些jar也加入进来。 这些jar包应该是一些开源工作者在维护着。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>8.5.31</version>
  </dependency>

上面这个依赖是springboot为我们准备好的,版本号为8.5.31,springboot的版本为:2.0.x。 这个依赖包含了tomcat的核心jar包。springboot 初始化tomcat,启动tomcat都需要这个jar。

当我们 run springboot的引导文件时,springboot 会创建WebServer, 我们跟着这个方法(createWebServer())走下去。

@Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

这里会去获取一个webServier,这个方法里面很多引用的类都存在于上面提到的jar包中。 new Tomcat() 创建一个tomcat 实例。到这里我们已经看到springboot已经创建了一个tomcat实例。

@Override
	public WebServer getWebServer(ServletContextInitializer... initializers) {
		Tomcat tomcat = new Tomcat();
		File baseDir = (this.baseDirectory != null ? this.baseDirectory
				: createTempDir("tomcat"));
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		Connector connector = new Connector(this.protocol);
		tomcat.getService().addConnector(connector);
		customizeConnector(connector);
		tomcat.setConnector(connector);
		tomcat.getHost().setAutoDeploy(false);
		configureEngine(tomcat.getEngine());
		for (Connector additionalConnector : this.additionalTomcatConnectors) {
			tomcat.getService().addConnector(additionalConnector);
		}
		prepareContext(tomcat.getHost(), initializers);
		return getTomcatWebServer(tomcat);
	}

当我们追踪到里面的类时,发现里面已经配置好了端口以及一些其他配置。 这样已经能创建一个默认的tomcat。 我们需要知道的是springboot是怎样吧appplication.property中的配置运用到tomcat实例中的。

探果网

nginx tomcat spring-boot 对 json 等数据压缩

nginx tomcat spring-boot 对 json 等数据压缩

nginx tomcat spring-boot 对 json 等数据压缩

nginx 配置文件修改
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types application/json text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
针对代理修改
proxy_buffering  off;
1
spring-boot 启用压缩 (版本需要大于 1.3)
server.compression.enabled=true
server.compression.min-response-size=10
server.compression.excluded-user-agents=gozilla,traviata
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
--------------------- 
tomcat 启用压缩
<Connector port="8888" protocol="HTTP/1.1" 
               connectionTimeout="21000" 
               redirectPort="28080" 
               URIEncoding="UTF-8"
               compression="on"  
               compressionMinSize="50"
               noCompressionUserAgents="gozilla, traviata"
            compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/csv,application/javascript,application/json,application/xml" />

以上如果服务器前面有 nginx 代理,可以使用 nginx 的提供的数据压缩方式

注意
noCompressionUserAgents 针对某些浏览器不启动数据压缩
1
参考:https://stackoverflow.com/questions/21410317/using-gzip-compression-with-spring-boot-mvc-javaconfig-with-restful
 

spring boot + jsp 发布到tomcat后el表达式无效

spring boot + jsp 发布到tomcat后el表达式无效

如题,本地跑是正常的,打war包发到tomcat后el表达式无效,直接在页面显示${username}。找过解决方案,在页面加isELIgnored="false"即可,在这里是想问还有没有更好的办法?

spring boot 2 内嵌Tomcat Stopping service [Tomcat]

spring boot 2 内嵌Tomcat Stopping service [Tomcat]

我在使用springboot时,当代码有问题时,发现控制台打印下面信息:

Connected to the target VM, address: ''127.0.0.1:42091'', transport: ''socket''
log4j:WARN No appenders could be found for logger (org.springframework.boot.devtools.settings.DevToolsSettings). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. . ____ _ __ _ _ /\\ / ___''_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ''_ | ''_| | ''_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) '' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.6.RELEASE) 2018-10-25 10:10:21.425 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-10-25 10:10:21.427 INFO 102158 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 2018-10-25 10:10:21.444 INFO 102158 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 2018-10-25 10:10:21.590 INFO 102158 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-10-25 10:10:24.522 INFO 102158 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] Disconnected from the target VM, address: ''127.0.0.1:42091'', transport: ''socket'' Process finished with exit code 0 

WTF?没有错误信息怎么解决问题? 各种搜索,总之就是代码有问题,自己检查把...

好吧,直接debug把

内嵌tomcat的入口类是org.apache.catalina.core.StandardService

//TODO 后面补上过程

最终找到org.springframework.context.support.AbstractApplicationContext 定位方法refresh()

if (logger.isWarnEnabled()) {
				logger.warn("Exception encountered during context initialization - " +
						"cancelling refresh attempt: " + ex);
			}

debug可以正常进入,然后就看到我们希望看到的 ex了

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricApiController'': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''fabricTemplate'': Injection of resource dependencies failed; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name ''fabricConfiguration'': Could not bind properties to ''FabricConfiguration'' : prefix=blockchain, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under ''blockchain.channel-peers'' to java.util.List<com.smy.bc.fabric.core.configuration.FabricConfiguration$EndPoint> 

问题发现了,解决自己代码问题,然后重新启动,正常! 万事大吉?错,这才开始

上面我们简单解决了问题,但是根源没有解决!
要说解决方案把当前流行的日志体系简单说一遍
下面整理的来源网络:

常见的日志框架,注意不是具体解决方案

1 Commons-logging: apache 最早提供的日志的门面接口。避免和具体的日志方案直接耦合。类似于JDBC的api接口,具体的的JDBC driver实现由各数据库提供商实现。通过统一接口解耦,不过其内部也实现了一些简单日志方案
2 Slf4j: 全称为Simple Logging Facade for JAVA:java简单日志门面。是对不同日志框架提供的一个门面封装。可以在部署的时候不修改任何配置即可接入一种日志实现方案。和commons-loging应该有一样的初衷。

常见的日志实现:
log4j
logback
jdk-logging

详细优缺点不是本文重点,请自行搜索。

接着分析上面的问题,Commons-logging 是tomcat默认的日志系统(apache自家东西得支持),具体的日志实现,根据系统已存在日志系统选择。 简单列举以下log的实现: org.apache.commons.logging.Log | org.apache.commons.logging.impl.SimpleLog org.apache.commons.logging.impl.NoOpLog org.apache.commons.logging.impl.Log4JLogger org.apache.commons.logging.impl.SLF4JLog org.apache.commons.logging.impl.Jdk14Logger

springboot 默认使用的是logback日志实现,问题就出现在这里了!!!common-logs并没有logback的实现!

根据maven依赖,我们看到log4j和logback的包都被引入了,然后tomcat之能选择的是log4j,springboot使用的是logback。 log4j和logback只见缺少一个桥梁,正是缺少的这个桥梁,导致springboot只能输出logback!!!

中间的桥梁就是下面这个依赖

    <dependency>
        <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency> 

这个依赖可以将log4j输出到slf4j,从而从sl4j输出。

总结: 总结一下,已经搞明白是slf4j/common-logs <> log4j和logback的恩怨情仇

第一种解决方式:根据日志定位问题,然后采用加法处理,增加jcl-over-slf4j,打通slf4j和common-logs通道

第二种解决方式:解决冲突,一山不容二虎,排除掉slf4j,common-logs任意一方,spring使用slf4j,那可以排除调common-logs

从项目优化的角度看,第二种更优,可以减少不必要的依赖。

如果日志出现问题,那就是日志体系发生冲突了,可以参考这个思路,处理项目中日志异常问题

今天的关于spring boot tomcat jsp的分享已经结束,谢谢您的关注,如果想了解更多关于# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。、nginx tomcat spring-boot 对 json 等数据压缩、spring boot + jsp 发布到tomcat后el表达式无效、spring boot 2 内嵌Tomcat Stopping service [Tomcat]的相关知识,请在本站进行查询。

本文标签: