项目代码放在spring-boot-mvc
这篇文章使用第一种方式
标签(空格分隔): 未分类项目打包成war包,同时完全基于java配置配置来实现spring mvc
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── springbootmvc
│ └── demo
│ ├── DemoApplication.java
│ └── controller
│ └── HelloController.java
├── resources
│ └── application.yaml
└── webapp
├── hello.jsp
├── index.jsp
└── static
├── css
│ └── style.css
└── js
└── app.js
打成war包的话, 必须要在webapp目录放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-java-config-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvc-java-config-war</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>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>SpringSecurityHelloWorldAnnotationExample</warName>
<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;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
super.addResourceHandlers(registry);
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
return resolver;
}
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源码,这里不贴代码了。
项目代码放在spring-boot-mvc
这篇文章使用第一种方式
标签(空格分隔): 未分类项目打包成war包,同时完全基于java配置配置来实现spring mvc
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── springbootmvc
│ └── demo
│ ├── DemoApplication.java
│ └── controller
│ └── HelloController.java
├── resources
│ └── application.yaml
└── webapp
├── hello.jsp
├── index.jsp
└── static
├── css
│ └── style.css
└── js
└── app.js
打成war包的话, 必须要在webapp目录放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-java-config-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvc-java-config-war</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>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>SpringSecurityHelloWorldAnnotationExample</warName>
<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;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@SpringBootApplication
public class DemoApplication extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
super.addResourceHandlers(registry);
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
return resolver;
}
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源码,这里不贴代码了。