1

My problem starts when I want to access to an url with this pattern ignoring any extension

localhost:8084/project/foo/1

I got this web.xml configuration:

<?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>

Actually, to access to that url, I need to use /foo/1.html and I want to use /foo/1, because I will use that number as id to find an object and I don't know if I'm missing something, but this config doesnt work.

Controller code

@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 Answer 1

1

Have you tried

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

Check this out

Try with this.

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

As /foo redirect to the servlet make /foo/1 will try to find /foo/foo/1

Sign up to request clarification or add additional context in comments.

9 Comments

Yes, I've already tried. If I use that url-pattern it gives me a 404, even for /foo/1 or /foo/1.html
Let me know the code for your controller or servlet, did you define @Controller(/{id}). It seems controller is not taking the request.
It worked!! Thanks ^^ Now, if I want to ignore .html, should I use a securityFilter? Its because it access with both urls /foo/1 and /foo/1.html
Remove tag for <url-pattern>*.html</url-pattern>, as both url patterns are mapped to the same servlet
I will use a security filter then :) Thank you so much for all the info.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.