带注释的Spring MVC @ModelAttribute自动化不使用关联对象

时间:2012-04-30 20:24:24

标签: spring jsf spring-mvc

我正在使用Spring MVC和Annotations。这是我的问题的快速概述。

我的网域:

public class Restaurant {    
    private String name;
    private Address address = new Address();
    //Get and set....
}

public class Address{
    private String street;
    //Get and set....
}

我的控制器:

//Configure and show restaurant form.
public ModelAndView showAction() {
   ModelAndView mav = new ModelAndView("/restaurant/showRestaurant");
   restaurant = new Restaurant();
   mav.addObject("restaurant", restaurant);
   return mav;
}

//Save restaurant
public ModelAndView saveAction(@ModelAttribute(value="restaurant") Restaurant restaurant,BindingResult result) {
    restaurant.getName();//<- Not is null
    restaurant.getAddress().getStreet(); //<- is null
} 

我的观点:

 <form>
    <span class="full addr1"> 
      <label for="Nome">Name<span class="req">*</span></label>
      <h:inputText class="field text large" value="#{restaurant.name}" 
         id="name" forceId="true" styleClass="field text addr"/>
    </span> 

    <span class="full addr1">
      <label for="Nome">Street <span class="req">*</span></label>
      <h:inputText class="field text large" value="#{restaurant.address.street}" 
         id="street" forceId="true" styleClass="field text addr"/>
    </span>  
 </form>

我的问题是,当我填写名称和街道以调用方法“saveAction”时,我试图让餐馆填满该名称来自街道,但没有。

2 个答案:

答案 0 :(得分:0)

我不熟悉jsf,但是对于春天的绑定,你通常需要完整的路径,即name="address.street",以便正确绑定街道名称

答案 1 :(得分:0)

使用弹簧形式标签http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html尝试绑定。它非常简单。