Springboot添加Jsp的支持

(1) 创建Maven project;
在这里插入图片描述
(2) 在pom.xml文件添加依赖;

<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>
	<!-- 指定JDK的版本 -->
	<properties>
		<java.version>1.7</java.version>
	</properties>
	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 -->
		<!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

		<!-- tomcat 的支持. -->
		<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>

	</dependencies>
	<!-- 这是springboot devtools的plugin -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>

(3) 配置application.properties支持jsp

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application

(4) 编写测试Controller

package com.springboot.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Springboot添加JSP页面的使用方式
 * @ClassName HelloController
 * @Description 
 * @Author Huang
 * @Date 2018年11月13日 下午7:30:38
 */
@Controller
public class HelloController {

	@RequestMapping("/hello")
	public String hello(Map data){
		data.put("msg", "这是测试jsp页面的参数传递");
		return "index";
	}
}

(5) 编写JSP页面
在该目录下创建index.jsp文件
在这里插入图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	${msg}
</body>
</html>

(6) 编写启动类App.java

package com.springboot.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
 * 程序的启动类
 * @ClassName App
 * @Description 
 * @Author Huang
 * @Date 2018年11月13日 下午7:39:09
 */
@SpringBootApplication
@ComponentScan(basePackages = "com.springboot")
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

测试地址输入:http://localhost:8080/hello
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36964056/article/details/84034685