Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:56:17+00:00 2026-06-14T16:56:17+00:00

I am new to Spring MVC. I am writing an app that uses Spring,

  • 0

I am new to Spring MVC.
I am writing an app that uses Spring, Spring MVC and JPA/Hibernate
I don’t know how to make Spring MVC set a value coming from a dropdown into a model object. I can imagine this a very common scenario

Here is the code:

Invoice.java

@Entity
public class Invoice{    
    @Id
    @GeneratedValue
    private Integer id;

    private double amount;

    @ManyToOne(targetEntity=Customer.class, fetch=FetchType.EAGER)
    private Customer customer;

    //Getters and setters
}

Customer.java

@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Integer id;

    private String name;
    private String address;
    private String phoneNumber;

    //Getters and setters
}

invoice.jsp

<form:form method="post" action="add" commandName="invoice">
    <form:label path="amount">amount</form:label>
    <form:input path="amount" />
    <form:label path="customer">Customer</form:label>
    <form:select path="customer" items="${customers}" required="true" itemLabel="name" itemValue="id"/>                
    <input type="submit" value="Add Invoice"/>
</form:form>

InvoiceController.java

@Controller
public class InvoiceController {

    @Autowired
    private InvoiceService InvoiceService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addInvoice(@ModelAttribute("invoice") Invoice invoice, BindingResult result) {
        invoiceService.addInvoice(invoice);
        return "invoiceAdded";
    }
}

When InvoiceControler.addInvoice() is invoked, an Invoice instance received as a parameter. The invoice has an amount as expected, but the customer instance attribute is null. This is because the http post submits the customer id and the Invoice class expects a Customer object. I don’t know what is the standard way to convert that.

I have read about Controller.initBinder(), about Spring Type conversion (in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html) but I don’t know if that is the solution for this problem.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:56:19+00:00Added an answer on June 14, 2026 at 4:56 pm

    The trick as you have already noted is to register a custom converter which will convert the id from the drop down into a Custom instance.

    You can write a custom converter this way:

    public class IdToCustomerConverter implements Converter<String, Customer>{
        @Autowired CustomerRepository customerRepository;
        public Customer convert(String id) {
            return this.customerRepository.findOne(Long.valueOf(id));
        }
    }
    

    Now register this converter with Spring MVC:

    <mvc:annotation-driven conversion-service="conversionService"/>
    
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
           <list>
              <bean class="IdToCustomerConverter"/>
           </list>
        </property>
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application that uses GWT, some Spring MVC and Spring Security.
I am writing an asp.net MVC app that drives an IPhone application. I want
I am writing Web app in java using Spring Web MVC framework. Somehow validation
I'm writing a Spring 3 MVC based web app, using JSPs for my view
I'm writing my SPring MVC web application. I have set my session time out
I am writing simple CRUD(Create, Remove ,Update ,Delete) application using Spring MVC and hibernate.
New to Spring-MVC. I want to store two properties (uploadFolder=.., downloadFolder=..) in a .properties
I am new to Spring MVC . I have a web application. I have
I am new to spring MVC. i have below project structure. Below are classes:
I'm new to Spring MVC, so I'm confused. I've used MVC in Struts, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.