spring MVC model

本文解析了Spring MVC中Model自动命名的原理。当向Model添加对象时,如果没有指定名称,Spring会根据对象类型自动生成名称,对于单一对象采用类名小写形式,对于集合则在类名基础上追加List。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用spring MVC的人都知道,控制器是通过Model把数据传到view层的.那么它们具体是通过什么来定位传递的数据呢?

比如控制器中的一个方法:

@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String addInputNews(String practiceWay, Model model) {
		commonAction(model);
		List<RoleLevel> roles=this.roleLevelDao.getAll();
		model.addAttribute("roles",roles);//选择上级
		model.addAttribute(new RoleLevel());
		return jspFolder+"/add";
	}

 在view层可以通过${roles}来获取数据,即view层通过model中的key进行获取.

那么问题来了,如果是model.addAttribute(roles); 呢?没有key?!

经过查spring MVC官方资料,view层可以通过${roleLevelList } 来获取数据.

具体是什么依据呢?

以下是官方资料:

17.12.2 The Model ModelMap (ModelAndView)

The ModelMap class is essentially a glorified Map that can make adding objects that are to be displayed in (or on) a View adhere to a common naming convention. Consider the following Controller implementation; notice that objects are added to the ModelAndView without any associated name specified.

public class DisplayShoppingCartController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

        List cartItems = // get a List of CartItem objects
        User user = // get the User doing the shopping

        ModelAndView mav = new ModelAndView("displayShoppingCart"); <-- the logical view name

        mav.addObject(cartItems); <-- look ma, no name, just the object
        mav.addObject(user); <-- and again ma!

        return mav;
    }
}

The ModelAndView class uses a ModelMap class that is a custom Map implementation that automatically generates a key for an object when an object is added to it. The strategy for determining the name for an added object is, in the case of a scalar object such as User, to use the short class name of the object's class. The following examples are names that are generated for scalar objects put into a ModelMap instance.

  • An x.y.User instance added will have the name user generated.

  • An x.y.Registration instance added will have the name registration generated.

  • An x.y.Foo instance added will have the name foo generated.

  • java.util.HashMap instance added will have the name hashMap generated. You probably want to be explicit about the name in this case because hashMap is less than intuitive.

  • Adding null will result in an IllegalArgumentException being thrown. If the object (or objects) that you are adding could be null, then you will also want to be explicit about the name.

The strategy for generating a name after adding a Set or a List is to peek into the collection, take the short class name of the first object in the collection, and use that with List appended to the name. The same applies to arrays although with arrays it is not necessary to peek into the array contents. A few examples will make the semantics of name generation for collections clearer:

  • An x.y.User[] array with zero or more x.y.User elements added will have the name userListgenerated.

  • An x.y.Foo[] array with zero or more x.y.User elements added will have the name fooListgenerated.

  • java.util.ArrayList with one or more x.y.User elements added will have the name userListgenerated.

  • java.util.HashSet with one or more x.y.Foo elements added will have the name fooList generated.

  • An empty java.util.ArrayList will not be added at all (in effect, the addObject(..) call will essentially be a no-op).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值