1

I've looked into many sources to find the exact answer that I need, but unfortunately, I couldn't understand or they never hit the exact spot that I need.

Basicly, I am developing a spring-mvc web application and I am going to allow user to add post to the website. While adding this post, he/she is going to identify some features of the post, and one of the feature is category. Everything is working fine but category. I've tried to implement this category field with a dropdown menu, but I get nothing populated in dropdown when I run the project.

Here is my jsp page:

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@include file="/WEB-INF/views/template/header.jsp" %>

<div id="page">
<div id="main">
    <div class="row">
        <div class="three columns"></div>
        <div class="six columns">
            <form:form action="${pageContext.request.contextPath}/addPost" method="post" commandName="post"
                       enctype="multipart/form-data">
                <div class="form-group">
                    <label>Kategori</label>
                    <form:select path="category" id="category">
                        <form:option value="NONE" label="--- Seçiniz ---"/>
                        <form:options items="${category}"></form:options>
                    </form:select>
                    <!--select name="category">
                        <option name="category_id" value="0">Seçiniz</option>
                    </select-->
                </div>
                <div class="form-group">
                    <label>İlan Başlığı</label> <form:errors path="postTitle" cssStyle="color: #ff0000"/>
                    <form:input path="postTitle" id="title" class="form-Control"/>
                </div>
                <div class="form-group">
                    <label>İlan Açıklaması</label>
                    <form:input path="description" id="description" class="form-Control"/>
                </div>
                <!--div class="form-group">
                <label>Etiketler</label>
                <input type="text" class="asdasd" name="title" placeholder="İlanınız ile ilgili etiketler" required>
                </div-->
                <div class="form-control">
                    <label>Fiyat</label> <form:errors path="price" cssStyle="color: #ff0000"/>
                    <form:input path="price" id="price" class="form-Control"/>
                </div>
                <div class="form-group">
                    <label>Lokasyon</label>
                    <form:input path="postAddress" id="address" class="form-Control"/>
                </div>
                <div class="form-group">
                    <label class="control-label" for="postImage">Fotoğraf Yükle</label>
                    <form:input name="file" path="postImage" id="postImage" type="file" class="form:input-large"/>
                </div>
                <div class="form-group">
                    <button type="submit" value="submit" class="btn btn-send-message pull-right">İLAN OLUŞTUR</button>
                </div>
            </form:form>
        </div>
        <div class="three columns"></div>
    </div>
</div>

And here is my Controller function:

@RequestMapping(value= "/addPost", method = RequestMethod.GET)
public String addPost(Model model) {
    Post post = new Post();
    post.setActive(true);

    List<Category> category = categoryService.getAllCategories();

    model.addAttribute("category", category);
    model.addAttribute("post", post);

    return "addPost";
}

What I want to do here is, I want to get the necessary data from the database table that I've created by the help of Hibernate, and send it to dropdown menu.

Many examples here in SO and some other blogs shows that feature like filling the dropdown menu from the controller by manually. This is the one that I don't want. So far, the closest answer that I can find is this. But since I am new in Spring, I couldn't get it.

If anyone can even point me in the right direction on how to set this up that would be a great help.

Thanks in advance.

1 Answer 1

1

From what I can see, it looks like you have two model attributes, the post and list of categories. The JSP has a form, this form is bound to the post bean, and you want to have the list of categories appear in a dropdown on the form.

As far as I know, and google this is not possible, as long as you have two separate model attributes. The problem is that <form:select path="category"..> is scoped by your commandName bean, so it is looking for category as a property on post, and not in the view model.

I think you need to make a new class FormBean which has two properties, category and post, and then bind that to the from using commandName="formBean", and then use path="post.postTitle" for a single property on the post object, and <form:select path="category"...> for the dropdown.

Hope this helps

2
  • Do you mean beans by two properties?
    – Prometheus
    Commented Jan 8, 2017 at 17:29
  • Your model has two attributes, "post" and "category". When you use Spring form binding, your model should only have 1 attribute (per form). This is because you can only address one bean (model attribute) with commandName, and every path attribute will always refer to the command bean. If this is not clear enough, I recommend that you read the documentation again and get the terminology right. Commented Jan 8, 2017 at 19:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.