0
@RequestMapping(value = "/account/{id}", method = RequestMethod.GET)
public ModelAndView getAccount(@PathVariable String id)
        throws ProfileNotFoundException {
   System.out.println(id);
   return null;
}

.../account/12345 结果为空

.../account/test?id=12345 '12345' 结果为 12345

不知道如何解决这个问题,但我希望第一个链接而不是第二个链接工作。这是我的 webmvc-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd                 
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.twheys.lexika.web.**"
        use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller"
            type="annotation" />
    </context:component-scan>

    <mvc:annotation-driven/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
4

2 回答 2

0

只需将其指定为@PathVariable("id")- 没有负责解析参数值的 HandlerMethodArgumentResolver 尝试通过参数名称找出参数值,但参数名称(id)在编译期间会丢失(除非调试符号在编译期间打开,这通常它不是)。

于 2012-07-10T16:05:02.277 回答
0
@RequestMapping(value = "/authors/{authorId}")
public ModelAndView getAuthorById(@PathVariable String authorId) {
    Author author = bookService.getAuthorById(authorId);
    ModelAndView mav = 
        new ModelAndView("bookXmlView", BindingResult.MODEL_KEY_PREFIX + "author", author);
    return mav;
}

上面的代码对我来说很好。请以类似方式使用。

于 2013-07-11T07:59:33.863 回答