SpringBoot实践之(三十一)---Web应用之JSP+打war包在tomcat中运行

首先引入依赖:

在build.gradle中

    //支持thymeleaf模板
    //compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    //支持jsp
    compile('javax.servlet:jstl') //<!--jsp页面使用jstl标签-->
    compile('javax.servlet:javax.servlet-api') 
    compile('org.springframework.boot:spring-boot-starter-tomcat')
    compile('org.apache.tomcat.embed:tomcat-embed-jasper')  //<!--用于编译jsp-->

依赖包范围说明:
compile:编译依赖范围。如果没有指定,就会默认使用该依赖范围。使用此依赖范围的Maven依赖,对于编译、测试、运行三种classpath都有效。典型的例子是spring-code,在编译、测试和运行的时候都需要使用该依赖。
test: 测试依赖范围。使用次依赖范围的Maven依赖,只对于测试classpath有效,在编译主代码或者运行项目的使用时将无法使用此依赖。典型的例子是Jnuit,它只有在编译测试代码及运行测试的时候才需要。
provided:已提供依赖范围。使用此依赖范围的Maven依赖,对于编译和测试classpath有效,但在运行时候无效。典型的例子是servlet-api,编译和测试项目的时候需要该依赖,但在运行项目的时候,由于容器以及提供,就不需要Maven重复地引入一遍。

特别说明:经验证,暂时发现不能同时使用thymeleaf模板和JSP,如果开启了了支持thymeleaf,则controller跳转仍然按照 thymeleaf的格式跳转到templates 下面的同名html模板文件,同时servlet-api包用的compile范围,但是在idea和外部的tomcat中都可以正常启动。

application.properties配置

要支持jsp,需要在application.properties中配置返回文件的路径以及类型,这里指定了返回文件类型为jsp,路径是在/WEB-INF/jsp/下面。

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

控制类

上面步骤有了,这里就开始写控制类,直接上简单的代码,跟正常的springMVC没啥区别:

@Controller
@RequestMapping("/learnJsp")
public class LearnResourceController {
    @RequestMapping("")
    public ModelAndView index(){
        List<LearnResouce> learnList =new ArrayList<LearnResouce>();
        LearnResouce bean =new LearnResouce("官方参考文档","Spring Boot Reference Guide","http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application");
        learnList.add(bean);
        bean =new LearnResouce("官方SpriongBoot例子","官方SpriongBoot例子","https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples");
        learnList.add(bean);
        bean =new LearnResouce("龙国学院","Spring Boot 教程系列学习","http://www.roncoo.com/article/detail/125488");
        learnList.add(bean);
        bean =new LearnResouce("嘟嘟MD独立博客","Spring Boot干货系列 ","http://tengj.top/");
        learnList.add(bean);
        bean =new LearnResouce("后端编程嘟","Spring Boot教程和视频 ","http://www.toutiao.com/m1559096720023553/");
        learnList.add(bean);
        bean =new LearnResouce("程序猿DD","Spring Boot系列","http://www.roncoo.com/article/detail/125488");
        learnList.add(bean);
        bean =new LearnResouce("纯洁的微笑","Sping Boot系列文章","http://www.ityouknow.com/spring-boot");
        learnList.add(bean);
        bean =new LearnResouce("CSDN——小当博客专栏","Sping Boot学习","http://blog.csdn.net/column/details/spring-boot.html");
        learnList.add(bean);
        bean =new LearnResouce("梁桂钊的博客","Spring Boot 揭秘与实战","http://blog.csdn.net/column/details/spring-boot.html");
        learnList.add(bean);
        bean =new LearnResouce("林祥纤博客系列","从零开始学Spring Boot ","http://412887952-qq-com.iteye.com/category/356333");
        learnList.add(bean);
        ModelAndView modelAndView = new ModelAndView("/index");
        modelAndView.addObject("learnList", learnList);
        return modelAndView;
    }
}

用的对象类LearnResouce

public class LearnResouce {

    private String author;
    private String title;
    private String url;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public LearnResouce(String author, String title, String url) {
        this.author = author;
        this.title = title;
        this.url = url;
    }
}

jsp页面编写

需在webapp/WEB_INF/jsp目录下新建index.jsp

<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!--[if lt IE 7 ]><html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]><html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]><html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]><html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js" > <!--<![endif]-->
<head>
    <meta charset="utf-8"/>
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title></title>

    <!-- main JS libs -->
    <script src="${pageContext.request.contextPath}/vanilla-cream-css/js/libs/modernizr.min.js"></script>
    <script src="${pageContext.request.contextPath}/vanilla-cream-css/js/libs/jquery-1.10.0.js"></script>
    <script src="${pageContext.request.contextPath}/vanilla-cream-css/js/libs/jquery-ui.min.js"></script>
    <script src="${pageContext.request.contextPath}/vanilla-cream-css/js/libs/bootstrap.min.js"></script>

    <!-- Style CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/vanilla-cream-css/css/bootstrap.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/vanilla-cream-css/style.css" />
    <!-- scripts -->
    <script src="${pageContext.request.contextPath}/vanilla-cream-css/js/general.js"></script>

    <!-- Include all needed stylesheets and scripts here -->

    <!--[if lt IE 9]><script src="${pageContext.request.contextPath}/vanilla-cream-css/js/respond.min.js"></script><![endif]-->
    <!--[if gte IE 9]>
    <style type="text/css">
        .gradient {filter: none !important;}
    </style>
    <![endif]-->
</head><body style="background-image: none;">
<div class="body_wrap">
    <div class="container">
        <div class="alert alert-success text-center" role="alert">Sptring Boot学习资源大奉送,爱我就关注嘟嘟公众号:嘟爷java超神学堂</div>
        <table class="table table-striped table-bordered">
            <tr>
                <td>作者</td>
                <td>教程名称</td>
                <td>地址</td>
            </tr>
            <c:forEach var="learn" items="${learnList}">
                <tr class="text-info">
                    <td>${learn.author}</td>
                    <td>${learn.title}</td>
                    <td><a href="${learn.url}" class="btn btn-search btn-green" target="_blank"><span>点我</span></a>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </div>
</div>
</body>


</html>

页面访问验证:

最后启动tomcat输入http://localhost:9090/learnJsp 查看效果:

这里写图片描述

外部的Tomcat服务器部署war包

打war包:

在build.gradle中增加

//编译为war包
apply plugin: 'war'

然后运行
这里写图片描述
或者命令行执行 gradle build

会生成war包到配置文件中的生成路径

然后把war放到tomcat的webapps下,最后启动tomcat,能正常访问如下图:
这里写图片描述

注意,springboot打war包需要做到如下修改才可以,否则找不对对应的jsp路径:

主类继承SpringBootServletInitializer,同时重写configure方法

外部容器部署的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,此时我们需要在启动类中继承SpringBootServletInitializer并实现configure方法:

public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
这个类的作用与在web.xml中配置负责初始化Spring应用上下文的监听器作用类似,只不过在这里不需要编写额外的XML文件了。

参考文档:MAVEN工程方式的:
Spring Boot干货系列:(五)开发Web应用之JSP篇
详解SpringBoot集成jsp(附源码)+遇到的坑

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot,通常我们将应用程序打成可执行的JAR文件,而不是WAR文件。但是,如果你确实需要将Spring Boot应用程序打WAR文件,可以按照以下步骤进行操作: 1. 在你的Spring Boot项目的pom.xml文件,将打方式设置为war。在`<packaging>`元素将值设置为war,如下所示: ```xml <packaging>war</packaging> ``` 2. 在你的主应用程序类上添加`extends SpringBootServletInitializer`,并重写`configure()`方法。这样可以将Spring Boot应用程序注册为一个Servlet。 ```java import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class YourApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(YourApplication.class); } // 程序的入口 public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 在pom.xml文件添加servlet-api和jsp依赖。这些依赖项是为了支持WAR文件的部署和JSP页面的渲染。 ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>javax.servlet.jsp.jstl-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 其他依赖 --> </dependencies> ``` 4. 执行打命令。在命令行导航到你的项目根目录,并执行以下命令: ``` mvn clean package ``` 5. 在项目的`target`目录下将会生成一个WAR文件,你可以将该WAR文件部署到支持Java应用程序部署的Web服务器上。 希望以上步骤能帮助到你成功打Spring Boot应用程序为WAR文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值