1. 程式人生 > >springmvc請求接收引數的幾種方法

springmvc請求接收引數的幾種方法

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