SpringMVC-Maven

Posted zy-stu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC-Maven相关的知识,希望对你有一定的参考价值。

设置Maven

技术图片

设置Maven相关的参数

创建项目 

 创建maven-spring项目

技术图片

pom中添加Maven依赖

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

    <groupId>maven-spring-demo</groupId>
    <artifactId>maven-spring-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>hi-spring</name>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <!-- This plugin will set the properties values using dependency information -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>properties</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

下载Maven依赖

下载相关的依赖

创建相应的类文件

技术图片

 

在DemoRun中添加如下代码:

1 import org.springframework.boot.SpringApplication;
2 import org.springframework.boot.autoconfigure.SpringBootApplication;
3 
4 @SpringBootApplication
5 public class DemoRun 
6     public static void main(String[] args) 
7         SpringApplication.run(DemoRun.class, args);
8     
9 

在DemoController中添加如下代码:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class DemoController 
    @RequestMapping("/greeting")
    public String greeting(
            @RequestParam(value = "name", required = false, defaultValue = "name") String name,
            @RequestParam(value = "age", required = false, defaultValue = "age") String age,
            @RequestParam(value = "sex", required = false, defaultValue = "男") String sex,
            Model model) 
        model.addAttribute("name", name);
        model.addAttribute("age", age);
        model.addAttribute("sex", sex);
        return "greeting";
    

 

以上是关于SpringMVC-Maven的主要内容,如果未能解决你的问题,请参考以下文章