0

here is my web.xml

<servlet>
      <servlet-name>Learn</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>Learn</servlet-name>
      <url-pattern>/learn/*</url-pattern>
   </servlet-mapping>

if i change this code

<url-pattern>/learn/*</url-pattern>

to

<url-pattern>/learn/abc/</url-pattern>

i can hit my controller code which is given as

@Controller
@RequestMapping(value = "/learn")
public class ControllerClass
{
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String callRequest(ModelMap model)
    {
        return "index";
    }

    @RequestMapping(value = "/abc/", method = RequestMethod.GET)
    public String personController(ModelMap model)
    {
        return "welcome";
    }
}

but i also want to hit the first method or i will add more method, which i can not achieve by

/learn/abc/

in url mapping.

so please help me out with this

pom.xml

<dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- spring-context which provides core functionality -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
            programming implementation allowing you to define -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
            Spring’s model-view-controller (MVC) and REST Web Services implementation 
            for web applications -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- The spring-web module provides basic web-oriented integration features 
            such as multipart file upload functionality and the initialization of the 
            IoC container using Servlet listeners and a web-oriented application context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
    </dependencies>

please let me know if i missed any dependency

5
  • So what happens if you don't change the <url-pattern>, i.e. leave it as <url-pattern>/learn/*</url-pattern>? Do requests for both /learn/ and /learn/abc/ work then? Commented Apr 9, 2017 at 19:49
  • it works for none Commented Apr 9, 2017 at 19:52
  • but i want the url so if it contain /learn/ in it so it will hit the controller Commented Apr 9, 2017 at 19:52
  • So, the problem is it is not hitting the callRequest method when you change the url to /learn/* am I right? Commented Apr 9, 2017 at 20:18
  • in that case it is hitting none of the method, but logically it should .. right ? Commented Apr 9, 2017 at 20:24

1 Answer 1

1

With this url-pattern <url-pattern>/learn/*</url-pattern> configuration, to hit your method, the url path will be: /learn/learn

The url-pattern atribute on the web.xml works like a basepath for spring's servlet. So a good option is to change your url-pattern to /* , like this:

<url-pattern>/*</url-pattern>

Or if you want some basepath, change the url pattern to something like this: /basepath/*

And to hit your method, you need to use the path url: /basepath/learn

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

Comments

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.