spring-boot实现springmvc三种方式三

spring boot mvc 方便面系列是用最少的代码,使用springboot 实现spring mvc的文章。文章分文三篇:

  1. 项目打包成war包,同时完全基于properties配置来实现spring mvc
  2. 项目打成jar包,同时完全基于properties配置来实现spring mvc
  3. 项目打成war包,同时使用spring mvc 的配置类配置spring mvc

项目代码放在spring-boot-mvc

这篇文章使用第一种方式

标签(空格分隔): 未分类项目打包成war包,同时完全基于java配置配置来实现spring mvc

项目打包成jar包,同时完全基于properties配置来实现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资源。

pom


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

关键点

  • pom打成 war包
  • spring-boot-starter-web 的引入解决了spring mvc 的所有依赖,包含tomcat.embed库,所以不用在重复引入这个库
  • tomcat-embed-jasper 作用是变异jsp。如果加这个依赖,那么jsp会原样放回到浏览器段。
  • javax.servlet.jstl 如果想使用jstl标签在jsp中那么要引入这个

properties的 配置


    server:
        port: 13106
        contextPath: /security-demo
        servlet-path: /spring


    spring:
        mvc:
            view:
                prefix: /
                suffix: .jsp
        static-path-pattern: /**

重点:

  • spring.mvc.view.prefix 的相对目录是 webapp,这里配置成/ 意思是在webapp目录里面所有已 jsp结尾的jsp文件
  • static-path-pattern的作用是 解决静态文件的路径问题相当于代码的:ResourceHandlerRegistry

springboot main class


        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);
        }
    }

重点:

  • 不要在额外引入 enableMvc这个标签。

controller class

    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 目录的资源

在webapp中放入jsp 和 css js文件即可。具体代码见 spring-boot-mvc源码,这里不贴代码了。

  1. 项目打包成war包,同时完全基于properties配置来实现spring mvc
  2. 项目打成jar包,同时完全基于properties配置来实现spring mvc
  3. 项目打成war包,同时使用spring mvc 的配置类配置spring mvc

项目代码放在spring-boot-mvc

这篇文章使用第一种方式

标签(空格分隔): 未分类项目打包成war包,同时完全基于java配置配置来实现spring mvc

项目打包成jar包,同时完全基于properties配置来实现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资源。

pom


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

关键点

  • pom打成 war包
  • spring-boot-starter-web 的引入解决了spring mvc 的所有依赖,包含tomcat.embed库,所以不用在重复引入这个库
  • tomcat-embed-jasper 作用是变异jsp。如果加这个依赖,那么jsp会原样放回到浏览器段。
  • javax.servlet.jstl 如果想使用jstl标签在jsp中那么要引入这个

properties的 配置


    server:
        port: 13106
        contextPath: /security-demo
        servlet-path: /spring


    spring:
        mvc:
            view:
                prefix: /
                suffix: .jsp
        static-path-pattern: /**

重点:

  • spring.mvc.view.prefix 的相对目录是 webapp,这里配置成/ 意思是在webapp目录里面所有已 jsp结尾的jsp文件
  • static-path-pattern的作用是 解决静态文件的路径问题相当于代码的:ResourceHandlerRegistry

springboot main class


        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);
        }
    }

重点:

  • 不要在额外引入 enableMvc这个标签。

controller class

    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 目录的资源

在webapp中放入jsp 和 css js文件即可。具体代码见 spring-boot-mvc源码,这里不贴代码了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
慷慨打赏