springboot-jsp supports jsp pages (Part 8)

Create maven project

Use idea to create a project named springboot-jsp with the following structure:



Introduce dependencies in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>         
    

    <groupId>com.example.springboot</groupId>
    <artifactId>springboot-jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-jsp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>    

    <properties>
        <!-- 指定项目编码方式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- 指定jdk版本,默认为jdk1.6 -->
<java.version>1.7</java.version></properties>        
        
                
    

    <dependencies>
        <!-- 引入spring-boot-web mvc,aop依赖包 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>        
            
            
        
        
            
            
            test</scope>
        </dependency>

        <!-- 添加tomcat插件包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- 添加servlet依赖包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Add jsp configuration in application.properties file

#Add jsp configuration
 # The default prefix directory of the page
 spring.mvc.view.prefix = /WEB-INF/jsp/
 # The default suffix of the response page
 spring.mvc.view.suffix = .jsp


在src/main下创建一个webapp目录,如下



创建一个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=UTF-8">
    <title>Insert title here</title>
</head>
<body>
内容: ${message}
</body>
</html>

编写controller类

package com.example.springboot.jsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @desc
 * @Author wangsh
 * @date 2018/5/5 18:18
 * @return
 */
@Controller
@RequestMapping("/jsp")
public class JspConroller {


   @RequestMapping("/index")
   public String index(Map<String, Object> model) {
      model.put("message", "spring boot对jsp的支持方式");
      return "index";
   }
}


编写服务启动类

package com.example.springboot.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringbootJspApplication extends SpringBootServletInitializer{

   public static void main(String[] args) {
      SpringApplication.run(SpringbootJspApplication.class, args);
   }
}

启动服务后浏览器测试

浏览器访问:http://localhost:8080/jsp/index


提示页面找不到错误,该错误是由于在生成的target目录中,没有将webapp目录编译到其中导致。eclipse中部署tomcat不错在该问题,这个大概是idea中的一个bug.

解决办法:


选择启动类中的edit configurations ,打开如下界面:


选择启动类,在右侧work directory 这一行,点击“文件”小图标,选择启动类工作目录,我们这里的项目创建的是module,所以就选择MODULE_DIR即可。


重启服务,再次浏览器测试,如下访问正常,index.jsp中已经取到值。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580228&siteId=291194637
jsp