springboot之使用外置的servlet容器
嵌入式servlet容器:应用打成可执行性的jar 优点:简单\便捷 缺点:默认不支持JSP\优化定制比较复杂(使用定制器[ServerProperties\自定义WebServerFactoryCustomizer https://blog.****.net/level_Tiller/article/details/102173105],自己编写嵌入式servlet容器的创建工厂 [ServletWebServerFactory])
---------------------------------------------------------------------------------------------------------------------------------------
外置的servlet容器:外面安装tomcat--应用war包的方式打包
步骤:
(1)必须创建一个war项目(利用idea创建好目录结构)
(2)将嵌入式的tomcat指定为provided
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
(3)必须编写一个SpringBootServletInitializer的子类,并调用configure方法
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { //传入springboot应用的主程序 return application.sources(Springboot04WebJspApplication.class); } }
(4)启动服务器就可以使用
1\新建工程
打成war包
2\此时的目录结构如图所示
点击
选择modules,点击web
双击,
点击ok
yes
点击+号
将
更改为
点ok
此时目录结构为
3\把服务器整合到IDEA
选择
点击+号
32 more items,选择
写上名字,选择configure
deployment添加项目
选择
4\webapp下创建hello.jsp
写入
<body> <h1>Hello JSP</h1> </body>
访问
修改
为
此时访问路径可不带项目名
4\
WEB-INF下新建success.jsp
<body> <h1>success</h1> <h3>${msg}</h3> </body>
修改hello.jsp
<body> <h1>Hello JSP</h1> <a href="abc">abc</a> </body>
src包下新增controller,HelloController中写入
package com.example.springboot04webjsp.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("/abc") public String hello(Model model){ model.addAttribute("msg","你好"); return "success"; } }
resources下application.properties中写入前后缀
spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp
然后换了一个tomcat8版本