springmvc


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值