springboot中使用jsp作为展示层的示例,其实这个东西 只要你自己愿意看文档 很好学的 ,大家都是边学边研究,
不会了看下官方的文档
官方文档地址:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-running-your-application
官方还提供了直接生成项目的地址:https://start.spring.io/
本次使用的是自己通过myeclipse创建的maven项目,通过官方的文档将项目跑起来
具体如下maven的配置文件pom.xml
<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>SpringBootWebToJSP</groupId>
<artifactId>SpringBootWebToJSP</artifactId>
<!-- 以war包形式发布 -->
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootWebToJSP Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 引入的版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 定义项目的名字 也可以不适用此配置 ,自动生成的项目名字 后边会带一串数字 不方便 -->
<finalName>SpringBootWebToJSP</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
上述配置 如果不想使用 war的形式运行 可以改为jar的形式
启动类SampleWebJspApplication.java
SampleWebJspApplication.java
package com.julongtech.action;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* 主程序启动类 SpringBootServletInitializer web程序需要继承
* @author julong
* @date 2017-8-22 上午11:51:18
*/
@SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleWebJspApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWebJspApplication.class, args);
}
}
package com.julongtech.action;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 页面跳转类
* @author julong
* @date 2017-8-22 上午11:52:23
*/
@Controller
public class WelcomeController {
@Value("${application.message:Hello World}")
private String message = "Hello World";
@GetMapping("/")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
@RequestMapping("/foo")
public String foo(Map<String, Object> model) {
throw new RuntimeException("Foo");
}
@RequestMapping("/foot")
public String foot(Map<String, Object> model) {
System.out.println("123");
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
你还需要在 src/main/resource/application.properties创建 application.properties文件 如果你使用的是 spring官方提供的工具生成的项目可以忽略此,只需要在文件中添加配置即可 :有人说 使用“:” 有人说使用“=” 其实两个都可以 我试过了 ,官方的示例是 : 文档中是 =
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello julong
jsp页面 welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<body>
<c:url value="/resources/text.txt" var="url"/>
<spring:url value="/resources/text.txt" htmlEscape="true" var="springUrl" />
Spring URL: ${springUrl} at ${time}
<br>
JSTL URL: ${url}
<br>
Message: ${message}
</body>
</html>
书写完毕后 点击项目 运行 ,本文使用的jdk1.7
启动后如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.7.RELEASE)
2017-08-22 14:40:24.690 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : Starting SampleWebJspApplication on julong-841deb9b with PID 1488 (started by Administrator in D:\Workspaces\OWER\SpringBootWebToJSP Maven Webapp)
2017-08-22 14:40:24.690 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : No active profile set, falling back to default profiles: default
2017-08-22 14:40:24.752 INFO 1488 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@186616b: startup date [Tue Aug 22 14:40:24 CST 2017]; root of context hierarchy
2017-08-22 14:40:26.018 INFO 1488 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-22 14:40:26.034 INFO 1488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-08-22 14:40:26.034 INFO 1488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1531 ms
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-22 14:40:26.706 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@186616b: startup date [Tue Aug 22 14:40:24 CST 2017]; root of context hierarchy
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/foo]}" onto public java.lang.String com.julongtech.action.WelcomeController.foo(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/foot]}" onto public java.lang.String com.julongtech.action.WelcomeController.foot(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.julongtech.action.WelcomeController.welcome(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.784 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-22 14:40:26.784 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-22 14:40:26.799 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.799 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.846 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.971 INFO 1488 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-22 14:40:27.018 INFO 1488 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-22 14:40:27.034 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : Started SampleWebJspApplication in 2.891 seconds (JVM running for 3.141)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.7.RELEASE)
2017-08-22 14:40:24.690 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : Starting SampleWebJspApplication on julong-841deb9b with PID 1488 (started by Administrator in D:\Workspaces\OWER\SpringBootWebToJSP Maven Webapp)
2017-08-22 14:40:24.690 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : No active profile set, falling back to default profiles: default
2017-08-22 14:40:24.752 INFO 1488 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@186616b: startup date [Tue Aug 22 14:40:24 CST 2017]; root of context hierarchy
2017-08-22 14:40:26.018 INFO 1488 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-22 14:40:26.034 INFO 1488 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-08-22 14:40:26.034 INFO 1488 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-08-22 14:40:26.268 INFO 1488 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1531 ms
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-22 14:40:26.424 INFO 1488 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-22 14:40:26.706 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@186616b: startup date [Tue Aug 22 14:40:24 CST 2017]; root of context hierarchy
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/foo]}" onto public java.lang.String com.julongtech.action.WelcomeController.foo(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/foot]}" onto public java.lang.String com.julongtech.action.WelcomeController.foot(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.768 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.julongtech.action.WelcomeController.welcome(java.util.Map<java.lang.String, java.lang.Object>)
2017-08-22 14:40:26.784 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-22 14:40:26.784 INFO 1488 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-22 14:40:26.799 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.799 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.846 INFO 1488 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-22 14:40:26.971 INFO 1488 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-08-22 14:40:27.018 INFO 1488 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-22 14:40:27.034 INFO 1488 --- [ main] c.j.action.SampleWebJspApplication : Started SampleWebJspApplication in 2.891 seconds (JVM running for 3.141)
启动成功后 访问:http://localhost:8080/foot