Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Populating Db Tables With Dropdown Menu Using Spring/hibernate

hibernate spring database webapplication spring-mvc

  • Please log in to reply
15 replies to this topic

#13 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 17 April 2012 - 05:56 AM

I would have a

DeliveryService.createDelivery(String customerName, String customerCountry)

DeliveryService propably has a CustomerService Autowired in it which it can use to retrieve the ID (and customerService has CustomerDao autowired)
  • 0

#14 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 17 April 2012 - 06:12 AM

Thanks :) Do I understand it correctly that customerName and customerCountry correspond to the Form Data?

Do you know how I could have dropdown boxes filled with the existing customer names and countries that the user would select from instead of having a form? There would then be an option "new" where the user could create a new customer if it doesn't already exist.

I know I'm asking a million questions here but I want you to know I'm truly grateful for your fast and clear answers :)
  • 0

#15 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 17 April 2012 - 06:22 AM

hanks Do I understand it correctly that customerName and customerCountry correspond to the Form Data?

yes, you'll just have to call 2 getters to get both.

Do you know how I could have dropdown boxes filled with the existing customer names and countries that
the user would select from instead of having a form? There would then be an option "new" where the user could create a new customer if it doesn't already exist.


http://static.spring...glib-optionstag

It's spring 2.0 documentation but it should be pretty much the same.
So in the initForm controller method you'd fetch the countries and names from the DB and add both lists to the model.
The variable name under which you've added them will be available with JSTL like ${customerList}


The 'new' option could either be a link to a totally different page with a new form.
Or you'd have to expand the current form (and form object) with extra fields for a new customer.
So it becomes:
+---form-------------+
|   dropdown1		|
|   dropdown2		|
|   submitbutton1	|
|   newnameField	 |
|   newcountryField  |
|   submitbutton2	|
+--------------------+

I would use javascript to show / hide 1 of the 2 sections to make it look pretty :)

Edit: can actually be 2 separate forms then.
  • 1

#16 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 17 April 2012 - 06:34 AM

yes, you'll just have to call 2 getters to get both.


Could I do it like this?

Form Object:
public class DeliveryDto {

private String customerName;
private String customerCountry;
	   
public void setCustomerName(String customerName){
  this.customerName = customerName;
}
public String getCustomerName(){
  return customerName;
}
public void setCustomerCountry(String customerCountry){
  this.customerCountry = customerCountry;
}
public String getCustomerCountry(){
  return customerCountry;
}


Controller:
@Controller
public class DeliveryController {

@Resource(name="deliveryService")
private DeliveryService deliveryService;
@RequestMapping(value="/initForm", method=RequestMethod.GET)
public String getAdd(Model model){
  DeliveryDto form = new DeliveryDto();
  model.addAttribute("formData", form);
  return "----view----";
 
}

@RequestMapping(value="/postForm", method=RequestMethod.POST)
public String postAdd(@ModelAttribute("formData") DeliveryDto form, Model model) {
 
  deliveryService.createDelivery(form.getCustomerName(), form.getCustomerCountry());
 
  return "----view----";
}

}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download