spring-bootDemo

这个原文地址讲的很好,我只是按照原文讲的直接惊醒拷贝。

原文链接:http://blog.csdn.net/xiaoyu411502/article/details/47864969

源码如下:

package com.tianmaying.spring_web_demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication

@RestController

public class Application {

@RequestMapping("/name")

public String greeting() {

return "Hello World!";

}

@RequestMapping("/")

public String index() {

return "Index Page";

}

@RequestMapping("/hello")

public String hello() {

return "Hello World!";

}

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

2.新建一个类

package com.tianmaying.spring_web_demo;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/classPath")

public class App {

@RequestMapping("/methodPath")

public String method() {

return "mapping url is /classPath/methodPath";

}

@RequestMapping("/users/{username}")

public String userProfile(@PathVariable("username") String username) {

return String.format("user %s", username);

}

@RequestMapping("/posts/{id}")

public String post(@PathVariable("id") int id) {

return String.format("post %d", id);

}

@RequestMapping(value = "/login", method = RequestMethod.GET)

public String loginGet() {

return "Login Page";

}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String loginPost() {

return "Login Post Request";

}

}

3.在html页面返回(此处请看原文链接,讲的比较细也比较清楚)

package com.tianmaying.spring_web_demo;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class HelloController {

@RequestMapping("/hello/{name}")

public String hello(@PathVariable("name") String name, Model model) {

model.addAttribute("name", name);

return "hello";

}

}

你可能感兴趣的:(spring-bootDemo)