spring controller convention to use -
there 2 formats writing controller handlers in spring.
summarizing determines format used.
whats format of preference?
being new-bie,this being on teh right track.
option 1:
@requestmapping(value=".....", method=requestmethod.get) public string loadformpage(model m) { m.addattribute("subscriber", new subscriber()); return "formpage"; } @requestmapping(value="....", method=requestmethod.post) public string submitform(@modelattribute subscriber subscriber, model m) { m.addattribute("message", "successfully saved person: " + subscriber.tostring()); return "formpage"; }
option 2:
@requestmapping(value=".....") public modelandview personpage() { return new modelandview("person-page", "person-entity", new person()); } @requestmapping(value=".....") public modelandview processperson(@modelattribute person person) { modelandview modelandview = new modelandview(); modelandview.setviewname("person-result-page"); modelandview.addobject("pers", person); return modelandview; }
a controller method in spring can support zero-to-many input parameters, , principal mechanisms in spring specifying input parameters @requestparam
, @modelattribute
annotations.
@requestparam
annotation used bind individual request parameters, string , integers, method parameters in controller.
@modelattribute
annotation used bind complex objects, domain objects, data transfer objects and/or form backing objects, method parameters in controller.
the annotaton determined variable type.
primitive variables annotated @requestparam
complex variables annotated @modelattribute
the annotation name derived variable name
a controller method in spring can output model data, , principal mechanism in spring specifying output model data modelandview
object.
in event there isn't data returned method, controller method can return string
represents view should rendered.
if controller method return data, modalandview
object needs instantiated , each output variable added model attribute modelandview
object.
Comments
Post a Comment