怎么使用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>
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- spring.view.prefix=/WEB-INF/jsp/
- spring.view.suffix=.jsp
- application.message: Hello Phil
- server.port=8282
似乎不起作用,有了解的可以与本人探讨一下。
下面就是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);
- }
- }
对于需要部署到传统servlet容器之中的应用,Boot提供了一种方式以编码的方式初始化Web配置。为了使用这一点,Boot提供了可选的WebApplicationInitializer,
它会使用servlet容器来注册应用,这会通过Servlet 3.0 API以编码的方式注册servlet并且会用到ServletContext。通过提供SpringBootServletInitializer的子类,
Boot应用能够使用嵌入的Spring上下文来注册配置,这个Spring上下文是在容器初始化的时候创建的。
如此之后,就可以启动springboot应用使用jsp了。