首页 > 代码库 > Spring Boot入门——使用jsp

Spring Boot入门——使用jsp

使用步骤:

1、创建Maven web project项目

     技术分享

2、在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>com.wyl</groupId>
  <artifactId>SpringbootJSP</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringbootJSP Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
      <java.version>1.7</java.version>
  </properties>
  
  <parent>
      <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
  </parent>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- 引入tomcat相关依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- tomcat-jasper -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.0.M21</version>
    </dependency>
    
    <!-- 引入servlet依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0-b07</version>
        <scope>provided</scope>
    </dependency>
    
    <!-- 引入jstl标签库 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
    <!-- devtools插件,热部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
        <scope>true</scope>
    </dependency>
    
  </dependencies>
  <!-- 打包插件 -->
  <build>
      <finalName>SpringbootJSP</finalName>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <configuration>
                  <fork>true</fork><!-- 热部署生效必须加 -->
              </configuration>
          </plugin>
      </plugins>
  </build>
</project>

3、配置application.properties支持jsp

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

4、编写测试controller

 

@Controller
public class JSPController {

    @RequestMapping("indexJsp")
    public String index(Map<String, Object> map){
        map.put("hello", "there is jsp");
        return "index";  //映射的是/WEB-INF/jsp/index.jsp页面
    }
}

 

5、编写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=utf-8">
<title>Insert title here</title>
</head>
<body>
    helloJsp
    ${hello}
</body>
</html>

 

Spring Boot入门——使用jsp