sparsh610
sparsh610

Reputation: 1601

Spring MVC : URL Pattern

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

Upvotes: 0

Views: 1143

Answers (1)

ronaldo.ronie
ronaldo.ronie

Reputation: 11

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

Upvotes: 1

Related Questions