1

我正在使用带有注释的 Spring MVC。这是我的问题的简要概述。

我的域名:

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”时,当我尝试填写餐厅时,名称来自街道但没有。

4

2 回答 2

0

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

于 2012-04-30T20:36:00.997 回答
0

尝试使用 spring 表单标签http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html进行绑定。它很容易。

于 2012-04-30T22:46:23.650 回答