Spring boot 之一 HelloWorld

谨以此专辑,纪念Spring Boot学习之旅。

创建第一个项目以及跳转到JSP页面

1、新建项目
Spring boot 之一 HelloWorld

Spring boot 之一 HelloWorld

Spring boot 之一 HelloWorld

Spring boot 之一 HelloWorld

Spring boot 之一 HelloWorld

2、项目创建完成后的目录结构
Spring boot 之一 HelloWorld

3、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</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        <java.version>10</java.version>
    </properties>

    <dependencies>
        <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>

        <!--web 支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--jsp页面使用jstl标签-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--用于编译jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

4、启动类DemoApplication.java 文件

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

5、HelloController控制器文件

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/view")
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(HttpServletRequest request){
        System.out.println("测试数据");
        request.setAttribute("demo","大漠孤烟直");
        request.setAttribute("demo2","长河落日圆");
        return "hello";
    }
}

6、application.properties文件

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

7、hello.jsp 文件

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<html>
<head>
    <base href="<%=basepath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Insert title here</title>
</head>
<body>
    这是测试页面
    <%
        String demo = (String)request.getAttribute("demo");

        String demo2 = (String)request.getAttribute("demo2");

        System.out.println("demo :"+demo);
        System.out.println("demo2:"+demo2);
    %>

</body>
</html>```  

8、启动成功
![image](https://yqfile.alicdn.com/878fb0ca4dd3e27223e243e7e1690c64d97a6cc3.png)

9、访问测试
![image](https://yqfile.alicdn.com/cf4d9448373204825fbe75f150930d76db88b47c.png)
上一篇:String StringBuffer StringBuilder之间的区别


下一篇:solr配置与solrj EmbeddedSolrServer使用篇