zoukankan      html  css  js  c++  java
  • Spring Boot 初体验(11)添加JSP支持

    第一步:配置 application.properties

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

    第二步:添加相关依赖

    <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>SpringBoot_001</groupId>
        <artifactId>SpringBoot_001</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>SpringBoot_001 Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <!-- 继承父包 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.7</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId><!--解决只引用spring-boot-starter-web时启动缺少JPA 
                    validator的Bug -->
                <artifactId>hibernate-validator</artifactId>
                <version>5.3.0.Final</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
            <dependency><!-- fastjson依赖 -->
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
            <dependency><!-- devtools依赖 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
                <scope>true</scope>
            </dependency>
    
    
            <dependency><!-- oracle依赖 -->
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc7</artifactId>
                <version>12.1.0.2</version>
            </dependency>
            <dependency><!-- data jpa配置信息 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <!-- thymeleaf的依赖: -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <!-- freemarker依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
            <!-- Jsp支持 -->
            <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>
        <build>
            <finalName>SpringBoot_001</finalName>
    
            <plugins>
                <plugin><!-- devtools构建依赖 -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <!-- <configuration> 如果devtools不起作用,加上此项配置 <fork>true</fork> </configuration> -->
                </plugin>
            </plugins>
    
        </build>
    </project>
    View Code

    第三步:写代码测试

    controller:

    package com.mt.controller;
    
    import java.util.Date;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloJspController {
        @RequestMapping("/helloJsp")
        public String hello(Map<String, Object> map) {
            map.put("hello", new Date());
            return "hello";
        }
    }
    View Code

     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
        <hr>
        ${hello}
        
    </body>
    </html>
    View Code

    工程目录截图

    一个走投无路的庄稼汉
  • 相关阅读:
    BTree B+Tree
    SpringMvc框架 解决在RESTFUL接口后加任意 “.xxx” 绕过权限的问题
    多线程基础知识---sleep和wait区别
    多线程基础知识---join方法
    Maven跳过单元测试的两种方式
    maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令
    Maven项目版本继承 – 我必须指定父版本?
    SpringMVC 零配置 无web.xml
    利用ApplicationContextAware装配Bean
    Spring Boot 读取 resource 下文件
  • 原文地址:https://www.cnblogs.com/kuangyefeige/p/10105525.html
Copyright © 2011-2022 走看看