怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功

整合jsp,于是决定将整合过程记载下来。

无论使用的是那种ide,基本在maven的使用上都是相同的,本文使用的是myeclipse,创建maven web工程,pom中依赖如下:

[html] view
plain
 copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>cn.com</groupId>
  5. <artifactId>springboot-Web</artifactId>
  6. <packaging>jar</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>springboot-Web Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>1.2.5.RELEASE</version>
  14. <relativePath/>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.7</java.version>
  19. <version.spring>3.2.9.RELEASE</version.spring>
  20. <version.jackson>2.4.4</version.jackson>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>javax.servlet</groupId>
  25. <artifactId>jstl</artifactId>
  26. <version>1.2</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <!--WebJars-->
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.tomcat.embed</groupId>
  40. <artifactId>tomcat-embed-jasper</artifactId>
  41. <scope>provided</scope>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. <!--<version>${spring.boot.version}</version>-->
  50. <configuration>
  51. <mainClass>cn.com.SpringBootWebApplication</mainClass>
  52. <layout>ZIP</layout>
  53. </configuration>
  54. <executions>
  55. <execution>
  56. <goals>
  57. <goal>
  58. repackage
  59. </goal>
  60. </goals>
  61. </execution>
  62. </executions>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. <repositories>
  67. <repository>
  68. <id>spring-snapshots</id>
  69. <url>http://repo.spring.io/snapshot</url>
  70. <snapshots><enabled>true</enabled></snapshots>
  71. </repository>
  72. <repository>
  73. <id>spring-milestones</id>
  74. <url>http://repo.spring.io/milestone</url>
  75. <snapshots><enabled>true</enabled></snapshots>
  76. </repository>
  77. </repositories>
  78. <pluginRepositories>
  79. <pluginRepository>
  80. <id>spring-snapshots</id>
  81. <url>http://repo.spring.io/snapshot</url>
  82. </pluginRepository>
  83. <pluginRepository>
  84. <id>spring-milestones</id>
  85. <url>http://repo.spring.io/milestone</url>
  86. </pluginRepository>
  87. </pluginRepositories>
  88. </project>

其中最需注意的如下这个依赖,少了这一个不能使用jsp。

[html] view
plain
 copy

  1. <dependency>
  2. <groupId>org.apache.tomcat.embed</groupId>
  3. <artifactId>tomcat-embed-jasper</artifactId>
  4. <scope>provided</scope>
  5. </dependency>

同时在resources中的application.properties中配置如下:

[plain] view
plain
 copy

  1. spring.view.prefix=/WEB-INF/jsp/
  2. spring.view.suffix=.jsp
  3. application.message: Hello Phil
  4. server.port=8282

最下两个配置可有可无,上面的两个配置中,是配置jsp所在目录,在这里和官网略有不同,官网配的都是spring.mvc.view的配置,但这样

似乎不起作用,有了解的可以与本人探讨一下。

下面就是controller编写,具体不再讲解,仅贴出代码:

[java] view
plain
 copy

  1. package cn.com.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import java.util.Date;
  6. import java.util.Map;
  7. /**
  8. * Created by Administrator on 2016/5/6.
  9. */
  10. @Controller
  11. public class IndexController {
  12. @Value("${application.message:Hello World}")
  13. private String message = "Hello World";
  14. @RequestMapping("/")
  15. public String welcome(Map<String, Object> model) {
  16. model.put("time", new Date());
  17. model.put("message", this.message);
  18. return "welcome";
  19. }
  20. }

同时在启动类的编写如下:

[java] view
plain
 copy

  1. package cn.com;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.context.web.SpringBootServletInitializer;
  6. /**
  7. * Created by Administrator on 2016/5/6.
  8. */
  9. @SpringBootApplication
  10. public class SpringBootWebApplication extends SpringBootServletInitializer {
  11. @Override
  12. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  13. return application.sources(SpringBootWebApplication.class);
  14. }
  15. public static void main(String[] args) throws Exception {
  16. SpringApplication.run(SpringBootWebApplication.class, args);
  17. }
  18. }

在这里,使用jsp必须继承SpringBootServletInitializer类,在这使用其他文章的原文:

对于需要部署到传统servlet容器之中的应用,Boot提供了一种方式以编码的方式初始化Web配置。为了使用这一点,Boot提供了可选的WebApplicationInitializer,

它会使用servlet容器来注册应用,这会通过Servlet 3.0 API以编码的方式注册servlet并且会用到ServletContext。通过提供SpringBootServletInitializer的子类,

Boot应用能够使用嵌入的Spring上下文来注册配置,这个Spring上下文是在容器初始化的时候创建的。

如此之后,就可以启动springboot应用使用jsp了。

最新文章

  1. ZOJ Problem Set - 1402 Magnificent Meatballs
  2. 基于Centos7+Nginx+Tomcat8的负载均衡服务器的搭建
  3. 推荐一个Android开发懒人库 -- ButterKnife
  4. lambda表达式对比
  5. preventDefault stopPropagation??
  6. js判断选择时间不能小于当前时间的代码
  7. maven构建带版本号和日期的war包名
  8. poj 3687 Labeling Balls【反向拓扑】
  9. oracle防火墙端口问题
  10. 【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(3)
  11. 函数返回char* 的解决方案
  12. Linux之环境搭建(二)
  13. win10 iis注册.net framework 此操作系统版本不支持此选项。
  14. 第二章代替netcat连接无响应
  15. (笔记)Linux内核学习(一)之内核介绍
  16. Servlet----------通过 GenericServlet 开发Servlet
  17. 简单的一个MySQL类的实现:
  18. 如何设置httpd-mpm-conf的参数
  19. $.ajax dataType设置为json 回调函数不执行
  20. 实现ListView的加载更多的效果,如何将按钮布局到始终在ListView的最后一行

热门文章

  1. BootStrap学习(三)——重写首页之导航栏和轮播图
  2. exsi中的虚拟机添加磁盘后虚拟机中磁盘不出现
  3. Servicification
  4. split方法切割数组
  5. 【BZOJ4016】【FJOI2014】最短路径树问题
  6. NOIp模拟赛三十
  7. HDU-4221 Greedy? 贪心 从元素的相对位置开始考虑
  8. javascript取前n天的日期两种方法
  9. 洛谷 P1332 血色先锋队
  10. iOS:UISplitViewController的创建