Announcement Announcement Module
Collapse
No announcement yet.
Spring Form whith complex object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Form whith complex object

    Hello,
    First, I point the fact that I'm french and maybe I'll make some mistake in my post.

    I'll expose my problem.
    I'm currently processing a forum with Spring MVC and Hibernate.

    I encounter problems to implement the form where I want to create new threads and the first new post.

    My hibernate model look's like this

    Code:
    @Entity
    @Table(name = "subject")
    public class Subject implements java.io.Serializable
    {
    
      private int id;
      private Forum forum;
      private Users users;
      private String shortdescription;
      private String description;
      private Date startdate;
      private Date updatedate;
      private Date enddate;
      private Set<Post> posts = new HashSet<Post>(0);
    
      ...
    
       @OneToMany(fetch = FetchType.LAZY, mappedBy = "subject")
      public Set<Post> getPosts()
      {
        return this.posts;
      }
    
      public void setPosts(Set<Post> posts)
      {
        this.posts = posts;
      }
    }
    Code:
    @Entity
    @Table(name = "post")
    public class Post implements java.io.Serializable
    {
    
      private int id;
      private Subject subject;
      private Users users;
      private String title;
      private String body;
      private Date startdate;
      private Date enddate;
      private Date updatedate;
    
      ...
    }
    I don't know how to design my form to create simultaneously the subject and the first post.

    I've start to write the form :

    HTML Code:
    <sf:form method="post" commandName="post.html" modelAttribute="subject">
    	<span class="spForm"><label>Titre</label><input type="text" name="shortdescription"/></span>
    	<span class="spForm"><textarea name="body"></textarea></span>
    	<span class="spForm"><button type="submit">Envoyer</button></span>
    </sf:form>
    My textatera, here, match to the post and the other fields are for the subject.

    Is there possible with spring, or must I reference the first post in my subject ?

    Thanks for your help

    Pierre

  • #2
    1. Use the form-tags from spring, not plain html-tags.
    2. There is no FIRST post, you are using a Set.
    3. You can access nested object by point-notation when using spring's form tags: <form:input path="subject.posts[0].title"... if posts would be a list.
    4. I do not know what tag lib sf stands for, but commandName="post.html" looks wrong.

    Comment


    • #3
      Thanks for your reply spgmx =)

      Originally posted by spgmx View Post
      1. Use the form-tags from spring, not plain html-tags.
      I agree that point, I've just seen in the reference documentation that I can use html 5 tags...
      Originally posted by spgmx View Post
      2. There is no FIRST post, you are using a Set.
      Sorry, I've made confusion for this point because I would like to expose my context.
      So I point the fact that my form is to create a subject and the first post in a same time.
      Originally posted by spgmx View Post
      3. You can access nested object by point-notation when using spring's form tags: <form:input path="subject.posts[0].title"... if posts would be a list.
      If this way is correct to complete a list in an object, this is what I want Thanks
      Originally posted by spgmx View Post
      4. I do not know what tag lib sf stands for, but commandName="post.html" looks wrong.
      The tag lib sf reference:

      HTML Code:
      <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
      So the commandLine isn't referencing the destination page to post the form?
      I've understand it, and it's work correctly for my others forms in other page...

      Pierre

      Comment


      • #4
        Originally posted by Pierre Raboteau View Post
        Thanks for your reply spgmx =)


        So the commandLine isn't referencing the destination page to post the form?
        I've understand it, and it's work correctly for my others forms in other page...

        Pierre

        Generally, "commandName" is the name of your model bean or form object which is put in data map (ModelAndView) for display.

        Comment

        Working...
        X