package com.supben.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.supben.bean.User;
@Controller
public class MyController {
/**
* 普通跳转,并且把变量传到页面上 http://localhost:8080/mvc/index
*/
@RequestMapping("index")
public String index(Model model) {
User user = new User();
user.setId(1);
user.setName("yuhan");
model.addAttribute("user", user);
return "/index.jsp";
}
/**
* 获取rest风格 url 中参数的例子 http://localhost:8080/mvc/param/1
*/
@RequestMapping("param/{id}")
public String param(@PathVariable Long id) {
System.out.println(id);
return "/param.jsp";
}
/**
* 获取request中参数,并做redirect跳转 http://localhost:8080/mvc/redirect?name=good
*/
@RequestMapping("redirect")
public String redirect(String name) {
System.out.println(name);
return "redirect:/param/1";
}
/**
* 直接输出文本到页面例子 http://localhost:8080/mvc/text
*/
@RequestMapping("text")
public @ResponseBody
String text() {
return "it's a text";
}
/**
* 以变量形式获取post请求参数的例子 http://localhost:8080/mvc/post
*/
@RequestMapping("post")
public @ResponseBody
String post(String id) {
return "has get request paramters,id=" + id;
}
/**
* 以对象形式获取post请求参数的例子 http://localhost:8080/mvc/dopost
*/
@RequestMapping("dopost")
public @ResponseBody
String dopost(User user) {
return "has get request paramters,user=" + user;
}
/**
* 以对象形式获取get请求参数的例子 http://localhost:8080/mvc/doget
*/
@RequestMapping("doget")
public @ResponseBody
String doget(User user) {
return "has get request paramters,user=" + user;
}
}
springmvc
最新推荐文章于 2023-04-25 19:37:05 发布