简体   繁体   English

Spring URL映射问题

[英]Spring URL mapping question

I am using Java with Spring framework. 我在Spring框架中使用Java。 Given the following url: 给定以下URL:

www.mydomain.com/contentitem/234

I need to map all requests that come to /contentitem/{numeric value} mapped to a given controller with the "numeric value" passed as a parameter to the controller. 我需要使用作为参数传递给控制器​​的“数值”来映射所有映射到给定控制器的/contentitem/{numeric value}请求。

Right now in my servlet container xml I have simple mappings similar to the following: 现在,在我的servlet容器xml中,我具有类似于以下内容的简单映射:

...
<entry key="/index.html">
   <ref bean="homeController" />
</entry>
...

I am just wondering what I need to add to the mapping in order to achieve what I described? 我只是想知道我需要添加到映射中以实现我描述的内容吗?

Edit : I unaccepted the answer temporarily because I can't seem to figure out how to do the mapping in my web.xml (I am using annotations as described in axtavt's answer below). 编辑 :我暂时不接受答案,因为我似乎无法弄清楚如何在我的web.xml中进行映射(我正在使用以下axtavt的答案中所述的注释)。 How do I add a proper <url-pattern>..</url-pattern> in my <servlet-mapping> so that the request for "/contentitem/{numeric_value}" gets properly picked up? 如何在我的<servlet-mapping>添加适当的<url-pattern>..</url-pattern> ,以便正确接收对“ / contentitem / {numeric_value}”的请求? Thanks! 谢谢!

It can be done using annotation-based controller configuration (see 15.3 Implementing Controllers ): 可以使用基于注释的控制器配置来完成(请参见15.3实施控制器 ):

@Controller
public class ContentItemController {

    @RequestMapping("/contentitem/{id}")
    public ModelAndView contentItem(@PathVariable("id") int id) {
        ...
    }
}

try to use @RequestMapping and @PathVariable 尝试使用@RequestMapping和@PathVariable

@RequestMapping(value="/contentitem/{value}", method=RequestMethod.GET)
public String demo(@PathVariable(value="nvalue") String name, ModelMap map) {

int intValue = Integer.parseInt(nvalue);

// Do manipulation

return "something"; // Forward to something.jsp
}

Watch this Spring MVC Framework Tutorial 观看此Spring MVC Framework教程

您必须导入org.springframework.stereotype.Controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM