1. 程式人生 > >springMVC(10) springMVC引數傳遞(封裝物件接收引數)

springMVC(10) springMVC引數傳遞(封裝物件接收引數)

上面的例子中,是用相同的引數來接收頁面傳遞的引數,若頁面引數過多,方法傳的引數也會相應增多,這樣也不符合面向物件的思想。本示例就演示,把引數封裝成物件來接收。

先封裝物件User

package com.xdy.entity;

public class User {
	private String userName;
	private Integer age;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

修改DataController

package com.xdy.controller.annotation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xdy.entity.User;

@Controller
@RequestMapping("/data")
public class DataController{
	
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request,User user){
		request.setAttribute("userName",user.getUserName());
		request.setAttribute("age",user.getAge());
		return "/userManager";
	}
	
	@RequestMapping("/show")
	public String show(){
		return "/addUser";
	}
}