简体   繁体   中英

Spring URL mapping question

I am using Java with Spring framework. Given the following 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.

Right now in my servlet container xml I have simple mappings similar to the following:

...
<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). 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? Thanks!

It can be done using annotation-based controller configuration (see 15.3 Implementing Controllers ):

@Controller
public class ContentItemController {

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

try to use @RequestMapping and @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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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