springmvc请求接收参数的几种方法

2015/04/29 00:38
阅读数 91

参考文章:http://blog.csdn.net/yaerfeng/article/details/23658391


package com.sondon.mayi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sondon.mayi.entity.Pojo;
import com.sondon.mayi.entity.User;
import com.sondon.mayi.manager.UserManager;

@Controller
public class UserController {
	@Resource(name = "userManager")
	private UserManager userManager;

	@RequestMapping("/all_user")
	public @ResponseBody List<User> handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		System.out.println("this  is UserController。。。。。。。");
		List<User> list = userManager.getAllUser();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getUserName());
		}
		return list;
	}

//	@RequestMapping(value="/add_user",method = RequestMethod.POST)
	@RequestMapping(value="/add_user",method = RequestMethod.GET)
	@ResponseBody
	public User AddUser(@RequestParam(value="name", required=false) String name) {
		System.out.println("接收到的str  :"+name);
		User user = new User();
		user.setAge("25");
		user.setId("1");
		user.setUserName("mayi");
		userManager.addUser(user);
		return user;
	}
	
    @RequestMapping(value= "/{id}/{str} " )
    @ResponseBody
    public String helloWorld(@PathVariable String id, @PathVariable String str) {  
    System.out.println(id);
    System.out.println(str);
    return "mayi";
   }  
    
    
    @RequestMapping(value="/test_modelattribute",method= RequestMethod.POST)
    @ResponseBody
    public String processSubmit(@ModelAttribute( "pojo" ) Pojo pojo) {
    System.out.println("pojo.a:"+pojo.getA()+"  pojo.b :"+pojo.getB());
    return " index" ;
    }
    
    @RequestMapping(value="/request",method= RequestMethod.GET)  
    public String get(HttpServletRequest request, HttpServletResponse response) {  
    System.out.println(request.getParameter("a"));  
    return "index" ;  
   }  
    
}


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部