Announcement Announcement Module
Collapse
No announcement yet.
Reducing boiler plating through better technique or existing utilities. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reducing boiler plating through better technique or existing utilities.

    Hi All

    I'm relatively new to Spring Web MVC 3 but I'm pretty ok with using it so far.
    Apologies if this has been asked/answered before but google is not returning much for me.

    I'm using automatically generated hibernate entity beans (so I'd prefer no to edit them, in case of regenerating).

    The problem I'm having is best explained as so:

    I have a User hibernate entity and a Country hibernate entity. Upon registration, I provide a <select> list of countries for the registering person to select.
    To achieve this, I need a wrapper bean which has:

    Code:
    private List<TCountry> countryList;
    private String selectedCountryId;
    getters and setters
    The jsp code then is somethign to this effect:

    Code:
    <form:form commandName="register" method="POST" action="register.htm">
    etc.
    <form:select id="countryList" path="selectedCountryId" >
         <form:options items="${register.countryList}" itemValue="countryId" itemLabel="countryName" />
    </form:select>
    etc.
    </form:form>
    This is a simplistic example.
    My issue is that I hate having to create a bean for each form and populate it etc. It's huge boildeplating.
    I'd love to simply be able to supply the entity beans to the form and have e.g.
    selectedCountryId stored in another object and/or bound and available through some generic means thus eliminating the need for a different bean for each form.

    Does such a thing exist? I've seen BindingResult examples, while I don't understand BindingResult fully, it doesn't seem to solve the problem (I think)?

    Apologies if the question is a bit vague or wide open. Any and all comments welcome.

    Thanks very Much
    J

  • #2
    I have a User hibernate entity and a Country hibernate entity. Upon registration, I provide a <select> list of countries for the registering person to select.
    To achieve this, I need a wrapper bean which has:
    No you don't...

    Your country list is reference data and should simply be in the model so the view can render it, you don't need an object to store the list or selection.

    You can simply use the User as a form object, provide a list of country objects, use a Converter or Property Editor to convert the selected id to a country and it will be set on the user. No need to add extra objects etc.

    I strongly suggest you take a look at the sample application and a read of the reference guide.

    Comment


    • #3
      Thanks Marten

      I really appreciate your response. I'm delighted to hear that we don't need a dedicated bean for each form.
      I was able to use User as the form as so:
      The form:
      Code:
      <form:form commandName="userForm" method="POST" action="edituser.htm">
                      <form:select  path="country.id" >
                          <form:options items="${countryList}" itemValue="id" itemLabel="name" />
                      </form:select>
      That actually automatically causes a new instance of Country to be set in User having the correct id.

      User bean
      Code:
      public class User {    
          private long id;
          private String name;
          private Country country;
      getters and setters
      The controller
      Code:
      @RequestMapping( method= RequestMethod.GET )
          public String showForm(Map model) {
              if( !model.containsAttribute("userForm") )
                  model.put("userForm", new User() );
              model.put( "countryList", Util.getCountyList() );
              return "edituser";
          }
      
      public String submitForm(@ModelAttribute("userForm") User cmd, BindingResult result, Map model) {
              if cmd has validation issues {
                    return showForm(model);
              } else {
              return "redirect:/index.htm";
              }
          }
      I'm now looking into the @Valid feature and making (proper) use of BindingResult.

      For a newbe like myself, I'd love to hear of any spring 3 features that you/anybody would consider essential to know and use in the building of any web application. Any thoughts and comments welcome please.

      Thanks very much
      J

      Comment

      Working...
      X