Spring mvc url参数

时间:2011-05-24 13:15:54

标签: spring spring-mvc

我无法展示可能的餐厅。 我得到了我的控制器课程:

@Controller
public class RestaurantController extends MultiActionController{

    private RestaurantDAO restaurantDAO;

    public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
        this.restaurantDAO = restaurantDAO;
    }


    @RequestMapping("/restaurant/{restaurantId}")
    public ModelAndView restaurantid(@PathVariable("contactId") int id, 
         HttpServletRequest request,
         HttpServletResponse response) throws Exception {

       Restaurant restaurant = restaurantDAO.findRestaurantById(id);
       ModelMap modelMap = new ModelMap();
       modelMap.addAttribute("restaurant", restaurant);
       return new ModelAndView("restaurant", modelMap);
    }
}

我的jsp只是:

<c:out value="${restaurant.name }"

在我的spring-servlet.xml中:

<bean name="/restaurant/**" class="web.RestaurantController" >
<property name="restaurantDAO" ref="myRestaurantDAO"/> 
</bean>

1 个答案:

答案 0 :(得分:5)

因为您混淆了restaurantIdcontactId

@RequestMapping("/restaurant/{restaurantId}")
public ModelAndView restaurantid(@PathVariable("contactId") ...

我想当您将@PathVariable("contactId")更改为@PathVariable("restaurantId")时,它会起作用。

@RequestMapping("/restaurant/**")添加到您的控制器:

 @RequestMapping("/restaurant/**")
 @Controller
 public class RestaurantController extends MultiActionController{

BTW:什么是MultiActionController?