[bigdata-075] maven+spring+mvc web开发 示例

1. 在eclipse新建一个maven项目


2. 在spring.io里找到spring mvc的示例代码
http://spring.io/guides/gs/serving-web-content/
Serving Web Content with Spring MVC


3. maven项目依次设置如下项


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>


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


    <properties>
        <java.version>1.8</java.version>
    </properties>


 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>




4. 创建一个GreetingController控制器,在任意的包下都可以,代码如下:
-------------------------
package com.ttss.demo.demo_spring_mvc;


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 GreetingController {

@RequestMapping("/greeting")
   public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
       model.addAttribute("name", name);
       return "greeting";
   }
}
-------------------------


5. 创建sroucefold,也就是src/main/resources
在这里创建html模板,也就是
src/main/resources/templates/greeting.html
内容如下
------------------------
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
------------------------


6. 修改main函数所在的主类,修改如下:
------------------------
package com.ttss.demo.demo_spring_mvc;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
    
}
------------------------




7. 在项目的根目录下执行
mvn clean package,就会在target目录下生成  demo-spring-mvc-0.0.1-SNAPSHOT.jar
以命令 java -jar demo-spring-mvc-0.0.1-SNAPSHOT.jar
然后在浏览器打开本地地址 http://localhost:8080/greeting,既可看到结果。

http://localhost:8080/greeting?name=User也可以看到结果







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值