Spring Boot学习总结(2)——Spring Boot整合Jsp
2024-08-31 15:04:38
怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功
整合jsp,于是决定将整合过程记载下来。
无论使用的是那种ide,基本在maven的使用上都是相同的,本文使用的是myeclipse,创建maven web工程,pom中依赖如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>cn.com</groupId>
- <artifactId>springboot-Web</artifactId>
- <packaging>jar</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>springboot-Web Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.2.5.RELEASE</version>
- <relativePath/>
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <java.version>1.7</java.version>
- <version.spring>3.2.9.RELEASE</version.spring>
- <version.jackson>2.4.4</version.jackson>
- </properties>
- <dependencies>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--WebJars-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <!--<version>${spring.boot.version}</version>-->
- <configuration>
- <mainClass>cn.com.SpringBootWebApplication</mainClass>
- <layout>ZIP</layout>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>
- repackage
- </goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- </pluginRepository>
- <pluginRepository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- </pluginRepository>
- </pluginRepositories>
- </project>
其中最需注意的如下这个依赖,少了这一个不能使用jsp。
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
同时在resources中的application.properties中配置如下:
- spring.view.prefix=/WEB-INF/jsp/
- spring.view.suffix=.jsp
- application.message: Hello Phil
- server.port=8282
最下两个配置可有可无,上面的两个配置中,是配置jsp所在目录,在这里和官网略有不同,官网配的都是spring.mvc.view的配置,但这样
似乎不起作用,有了解的可以与本人探讨一下。
下面就是controller编写,具体不再讲解,仅贴出代码:
- package cn.com.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import java.util.Date;
- import java.util.Map;
- /**
- * Created by Administrator on 2016/5/6.
- */
- @Controller
- public class IndexController {
- @Value("${application.message:Hello World}")
- private String message = "Hello World";
- @RequestMapping("/")
- public String welcome(Map<String, Object> model) {
- model.put("time", new Date());
- model.put("message", this.message);
- return "welcome";
- }
- }
同时在启动类的编写如下:
- package cn.com;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.context.web.SpringBootServletInitializer;
- /**
- * Created by Administrator on 2016/5/6.
- */
- @SpringBootApplication
- public class SpringBootWebApplication extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(SpringBootWebApplication.class);
- }
- public static void main(String[] args) throws Exception {
- SpringApplication.run(SpringBootWebApplication.class, args);
- }
- }
在这里,使用jsp必须继承SpringBootServletInitializer类,在这使用其他文章的原文:
对于需要部署到传统servlet容器之中的应用,Boot提供了一种方式以编码的方式初始化Web配置。为了使用这一点,Boot提供了可选的WebApplicationInitializer,
它会使用servlet容器来注册应用,这会通过Servlet 3.0 API以编码的方式注册servlet并且会用到ServletContext。通过提供SpringBootServletInitializer的子类,
Boot应用能够使用嵌入的Spring上下文来注册配置,这个Spring上下文是在容器初始化的时候创建的。
如此之后,就可以启动springboot应用使用jsp了。
最新文章
- ZOJ Problem Set - 1402 Magnificent Meatballs
- 基于Centos7+Nginx+Tomcat8的负载均衡服务器的搭建
- 推荐一个Android开发懒人库 -- ButterKnife
- lambda表达式对比
- preventDefault stopPropagation??
- js判断选择时间不能小于当前时间的代码
- maven构建带版本号和日期的war包名
- poj 3687 Labeling Balls【反向拓扑】
- oracle防火墙端口问题
- 【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(3)
- 函数返回char* 的解决方案
- Linux之环境搭建(二)
- win10 iis注册.net framework 此操作系统版本不支持此选项。
- 第二章代替netcat连接无响应
- (笔记)Linux内核学习(一)之内核介绍
- Servlet----------通过 GenericServlet 开发Servlet
- 简单的一个MySQL类的实现:
- 如何设置httpd-mpm-conf的参数
- $.ajax dataType设置为json 回调函数不执行
- 实现ListView的加载更多的效果,如何将按钮布局到始终在ListView的最后一行