1、修改pom.xml
在dependencies节点下增加spring mvc资源:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
在properties下增加spring版本定义:
<spring.version>4.3.8.RELEASE</spring.version>
完成后的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.whowii</groupId>
<artifactId>website_java4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入spring-boot-start-parent 以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<!-- 引入spring-boot-starter-web 以包含spring webmvc和tomcat等web开发的特性 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 日志工具 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.23</version>
</dependency>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>website_java4</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 增加main方式启动的配置,若使用maven的spring-boot:run方式启动则不需要此配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 设置主类入口 -->
<mainClass>com.whowii.website4.AppBoot</mainClass>
<!-- 设置classpath -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</configuration>
</plugin>
</plugins>
</build>
<!-- 设置编码 -->
<properties>
<spring.version>4.3.8.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
保存后,记得执行下 Maven install。
2、增加 spring boot 配置文件
在src/main/resources目录下创建spring文件“application.properties”,内容如下:
# spring mvc 配置(视图解析器)
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
3、规范项目结构
在继续下文之前,先规范下项目结构:
1)、创建包“com.whowii.website4”,并将AppBoot.java移动到此包中;
2)、修改pom.xml中对AppBoot的引入为“com.whowii.website4.AppBoot”
3)、在website4包中依次创建如下包:admin(contrller、mapper、model、service、utils)、core、web,这些也就是后期规划,如下图:
4、创建控制器
在包com.whowii.website4.admin.controller下创建类“DemoController”:
package com.whowii.website4.admin.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/index")
public String index() {
return "demo-index";
}
}
5、创建视图页
在src/main下创建各级文件夹:webapp/WEB-INF/view,并创建视图页面“demo-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>首页</title>
</head>
<body>
demo-index
</body>
</html>
6、测试结果
在AppBoot.java上点击右键选择“Run as”->"Java Applicatipn",打开浏览器访问“http://127.0.0.1:8080/demo/index”: