Search code examples
javaspringmodelattribute

Multiple @ModelAttribute


I have Spring MCV form in JSP file:

<form:form action="confirm" modelAttribute="newStudent">
  <b>Personal info:</b><hr>
  <table>
    <tr>
        <td>First name:</td>
        <td>
            <form:input path="firstName" placeholder="Your first name?" />
            <form:errors path="firstName" cssClass="error"/>
        </td>
    </tr>

    <tr>
        <td>Last name:</td>
        <td>
            <form:input path="lastName" placeholder="Your second name?" />  
            <form:errors path="lastName" cssClass="error"/> 
       </td>
   </tr>
   <tr>
       <td>Country:</td>
       <td><form:select path="country">
           <form:options items="${newStudent.countryList}"/>
           </form:select></td>
   </tr>

  </table>
  <input type="submit" value="Submit"/>
</form:form>

And two classes:

public class NewStudent {


  //Countries List
  Map<String, String> countryList = new LinkedHashMap<>();

  public NewStudent() {
    countryList.put("US", "United States");
    countryList.put("UK", "United Kingdom");
    countryList.put("DE", "Germany");
  }

  public Map<String,String> getCountryList() {

    return countryList;
  }

/////

  @NotNull(message="Required!")
  @Size(min=3, message="At least 3 characters")
  @Pattern(regexp="[A-Za-z]+", message="Invalid name")
  private String firstName;

  @NotNull(message="Required!")
  @Size(min=3, message="At least 3 characters")
  @Pattern(regexp="[A-Za-z]+", message="Invalid last name")
  private String lastName;

  private String country;

  //setters, getters etc.
}

And:

@Entity
@Table(name="new_students")
public class NewStudentEntity {
  @Id
  private int id;

  @Column(name="first_name")
  private String firstName;

  @Column(name="last_name")
  private String lastName;

  @Column(name="country")
  private String country;

  ...
}

What I want to do is to use data from form to send it to NewStudent class via @ModelAttribute to validate the given data and also to get list of countries, but I also want to save data to database so I want to send data from form to Entity class. Is this possible to do that? Is there any way to use multiple @ModelAttribute annotation in Form and in Controller class or any different way to fix it?


Solution

  • Maybe there's different option to solve that but I fixed it in this way:

    I moved validation annotations to entity class, like this:

    @Entity
    @Table(name="new_students")
    public class NewStudent {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")  
    private int id;
    
    @Column(name="first_name")
    @NotNull(message="Required!")
    @Size(min=3, message="At least 3 characters")
    @Pattern(regexp="[A-Za-z]+", message="Invalid name")
    private String firstName;
    
    @Column(name="last_name")
    @NotNull(message="Required!")
    @Size(min=3, message="At least 3 characters")
    @Pattern(regexp="[A-Za-z]+", message="Invalid last name")
    private String lastName;
    
    @NotNull(message="Select the country!")
    @Column(name="country")
    private String country;
    

    Then moved countryList from NewStudent to Controller class and used addAttribute to send the list of countries from Controller to View.