项目代码放在spring-boot-mvc
这篇文章使用第一种方式
标签(空格分隔): 未分类项目打包成jar包,同时完全基于properties配置来实现spring mvc
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── springbootmvc
│ └── demo
│ ├── DemoApplication.java
│ └── controller
│ └── HelloController.java
└── resources
├── META-INF
│ └── resources
│ ├── hello.jsp
│ ├── index.jsp
│ └── static
│ ├── css
│ │ └── style.css
│ └── js
│ └── app.js
└── application.yaml
打成jar包的话, 必须要在resources目录中建立 META-INF/resources 然后在resources中放web资源。
<?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>
<parent>
<groupId>com.spring-boot-mvc</groupId>
<artifactId>spring-mvc-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<artifactId>mvc-all-properties-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvc-all-properties-jar</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile 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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
关键点
server:
port: 13106
contextPath: /security-demo
servlet-path: /spring
spring:
mvc:
view:
prefix: /
suffix: .jsp
static-path-pattern: /**
重点:
package com.springbootmvc.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);
}
}
重点:
package com.springbootmvc.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author wanli zhou
* @created 2017-10-31 3:11 PM.
*/
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@PostMapping("/hello")
public String sayHello(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
在webapp中放入jsp 和 css js文件即可。具体代码见 spring-boot-mvc源码,这里不贴代码了。