Spring MVC 控制器配置 - 混合注释和 XML @PathVariable
我正在使用 Spring MVC 框架 (v3) 开发一个 Web 应用程序。
目前我定义了几个控制器类 - 我通过扩展 MultiActionController 创建了它们,然后在我的 web mvc XML 配置中定义了 beans 和 URL 映射:
QuestionController.java:
public class QuestionController extends MultiActionController implements InitializingBean
{
webmvc-config.XML
<bean id="questionController" class="com.tmm.enterprise.microblog.controller.QuestionController"></bean>
...
<bean id="fullHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="mappings">
<props>
<prop key="/question/**">questionController</prop>
</props>
</property>
</bean>
这工作正常,我在我的 web mvc XML 配置中定义了方法控制器匹配被调用的 URL,并且一切正常(例如,我有一个 list(..)
方法,当我浏览到 /question/list
时,该方法会正确执行)。
但是,对于这个特定的控制器,我想利用 Spring 中的 @PathVariable 选项来允许变量 URL(例如,我想要一个 details(..)
方法,该方法当我调用 /question/detail/9999
时 - 其中 9999
是问题 ID,该方法将被执行)。我尝试按如下方式使用它:
QuestionController.java:
@RequestMapping("/detail/{questionId}")
public ModelAndView detail(@PathVariable("questionId") long questionId, HttpServletRequest request, HttpServletResponse response) throws Exception{
但是,当我运行上面的代码并得到:
在
@RequestMapping
中找不到@PathVariable
[questionId
]
有人以前遇到过这个吗?是否可以将 RequestMapping
注释与现有的 XML 配置的 URL 映射混合使用?
I am developing a web app using the Spring MVC framework (v3).
Currently I have several controller classes defined - I have created them by extending the MultiActionController and then in my web mvc XML config defined the beans and the URL mappings:
QuestionController.java:
public class QuestionController extends MultiActionController implements InitializingBean
{
webmvc-config.XML
<bean id="questionController" class="com.tmm.enterprise.microblog.controller.QuestionController"></bean>
...
<bean id="fullHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="mappings">
<props>
<prop key="/question/**">questionController</prop>
</props>
</property>
</bean>
This works fine, I define methods in my controller matching the URLs being called and everything works correctly (for example, I have a list(..)
method, that gets correctly executed when I browse to /question/list
).
However, for this particular controller I want to make use of the @PathVariable
option in Spring to allow for variable URLs (e.g. i want a details(..)
method, that when I call /question/detail/9999
- where 9999
is a question ID the method is executed). I have tried to use this as follows:
QuestionController.java:
@RequestMapping("/detail/{questionId}")
public ModelAndView detail(@PathVariable("questionId") long questionId, HttpServletRequest request, HttpServletResponse response) throws Exception{
However, I get an error when I run the above and get:
Could not find
@PathVariable
[questionId
] in@RequestMapping
Has anyone come across this before? Is it ok to mix RequestMapping
annotations with the existing XML configured URL mapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是来自 DefaultAnnotationHandlerMapping 类的注释,如果我正确地阅读了注释,如果您在 QuestionController.java 的顶部添加 @RequestMapping,它可能会解决您的问题
带注释的控制器通常标有 { @link Controller} 构造型
在类型级别。当 {@link RequestMapping} 为
应用在类型级别(因为这样的处理程序通常实现
{@link org.springframework.web.servlet.mvc.Controller}接口)。然而,
检测 {@link RequestMapping} 注释需要 {@link Controller}
如果类型级别不存在{@link RequestMapping},则在方法级别。
编辑
你可以做这样的事情,将uri的/question部分移动到顶部并保留
方法级别的细节部分
This is the comment from DefaultAnnotationHandlerMapping class, If i am reading the comment right, If you add @RequestMapping at the top of your QuestionController.java, It might resolve your problem
Annotated controllers are usually marked with the {@link Controller} stereotype
at the type level. This is not strictly necessary when {@link RequestMapping} is
applied at the type level (since such a handler usually implements the
{@link org.springframework.web.servlet.mvc.Controller} interface). However,
{@link Controller} is required for detecting {@link RequestMapping} annotations
at the method level if {@link RequestMapping} is not present at the type level.
EDIT
You can do some thing like this, move the /question part of the uri to the top and leave the
detail part at the method level