Java - 多个url-pattern,没有扩展名

时间:2014-02-07 16:31:45

标签: java spring-mvc

当我想要使用此模式访问包含忽略任何扩展名的网址的

时,我的问题就开始了

localhost:8084/project/foo/1

我有这个web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/foo</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

实际上,要访问该网址,我需要使用/foo/1.html并且我想使用/foo/1,因为我会将该数字用作id来查找对象而我不要不知道我是否遗漏了什么,但这个配置不起作用。

控制器代码

@Controller
public class FooController {

    @RequestMapping(value = "/foo/{id}")
    public ModelAndView telefono(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @PathVariable String id) {
        Map<String, Object> model = new HashMap<String, Object>();
        // Code to search foo
        return new ModelAndView("foo", model);
    }
}

1 个答案:

答案 0 :(得分:1)

你试过吗

  <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/foo/*</url-pattern>
    </servlet-mapping>

检查this out

试试这个。

@RequestMapping(value = "/{id}")

当/ foo重定向到servlet make / foo / 1会尝试查找/ foo / foo / 1