forms - Is there a reasonable way to leverage Spring binding when my model can be represented by a simple type, rather than a custom bean? -
spring binding works nicely when model complex enough warrant custom domain object, application needs receive simple piece of data user doesn't warrant domain object. however, there doesn't seem way handle case.
for example, have form user prompted activation code, , nothing more. can represented string
. on page, we're asking user accept agreement, , indicate acceptance checking checkbox, submitting form. represented boolean
.
unfortunately, haven't found way bind these simple types , still retain value-add spring brings table. in case, want use validation, , expose validation errors user via <form:error>
tag. end writing mostly-pointless class following:
public class verificationbean { private string pin; public string getpin() { return pin; } public void setpin(string pin) { this.pin = pin; } }
i toyed idea of using regular <input>
tags , pulling user's input handler method @requestparam
annotation, works enough, handling validation becomes hairy because you're not provided bindingresult
or model
work with, , idea of associating errors via <form:error>
goes out window. so, seems less of compromise create simple container class , use well-understood spring mechanisms custom, can't feel can't uncommon scenario there ought better way handle littering class hierarchies beans exist data binding purposes. it's not awful transgression, know purists can be.
has else encountered similar scenario , felt similarly? if so, did end doing? we're using spring 3.0.x here. thanks.
i've used spring mvc ~5 years , have run number of times. have not found solution happy (so guess i'm in same boat you).
my "solution" in end move scala. scala makes less of pain create these lame little form classes
class lameforminput(@beanproperty var pin: string = null)
and can put many of these class definitions want in single file. i've found surprisingly big reason love scala.
Comments
Post a Comment