天天看点

spring MVC model

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

spring MVC model

@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 } 来获取数据.

具体是什么依据呢?

以下是官方资料:

the <code>modelmap</code> class is essentially a glorified <code>map</code> that can make adding objects that are to be displayed in (or on) a <code>view</code> adhere to a common naming convention. consider the following <code>controller</code> implementation; notice that objects are added to the <code>modelandview</code> without any associated name specified.

the <code>modelandview</code> class uses a <code>modelmap</code> class that is a custom <code>map</code> 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 <code>user</code>, 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 <code>modelmap</code> instance.

an <code>x.y.user</code> instance added will have the name <code>user</code> generated.

an <code>x.y.registration</code> instance added will have the name <code>registration</code> generated.

an <code>x.y.foo</code> instance added will have the name <code>foo</code> generated.

adding <code>null</code> will result in an <code>illegalargumentexception</code> being thrown. if the object (or objects) that you are adding could be <code>null</code>, then you will also want to be explicit about the name.

what, no automatic pluralization?

spring web mvc's convention-over-configuration support does not support automatic pluralization. that is, you cannot add a <code>list</code> of <code>person</code> objects to a <code>modelandview</code> and have the generated name be <code>people</code>.

this decision was made after some debate, with the “principle of least surprise” winning out in the end.

the strategy for generating a name after adding a <code>set</code> or a <code>list</code> is to peek into the collection, take the short class name of the first object in the collection, and use that with <code>list</code> 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 <code>x.y.user[]</code> array with zero or more <code>x.y.user</code> elements added will have the name <code>userlist</code>generated.

an <code>x.y.foo[]</code> array with zero or more <code>x.y.user</code> elements added will have the name <code>foolist</code>generated.

a <code>java.util.arraylist</code> with one or more <code>x.y.user</code> elements added will have the name <code>userlist</code>generated.

a <code>java.util.hashset</code> with one or more <code>x.y.foo</code> elements added will have the name <code>foolist</code> generated.

an empty <code>java.util.arraylist</code> will not be added at all (in effect, the <code>addobject(..)</code> call will essentially be a no-op).