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";

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容