2017-04-09 72 views
0

,這裏是我的web.xmlSpring MVC的:URL模式

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

如果我改變了代碼

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

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

我能打到這是給我的控制器代碼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"; 
    } 
} 

但我也想打第一種方法或我將增加更多的方法,我可以通過

/learn/abc/ 

在URL映射無法實現。

所以請幫助我走出這個

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

請讓我知道如果我錯過了任何依賴

+0

如果你不改變'所以會發生什麼',即把它作爲' /學習/ *'?對'/ learn /'和'/ learn/abc /'都有要求嗎? – Andreas

+0

它適用於沒有 – sparsh610

+0

,但我想要的網址,所以如果它包含/學習/在它所以它會擊中控制器 – sparsh610

回答

1

以這樣的URL模式<url-pattern>/learn/*</url-pattern>配置,打你的方法,url路徑將爲:/learn/learn

web-url上的url-pattern atribute ml就像Spring的servlet的基本路徑一樣工作。因此,一個很好的選擇是你的URL模式更改爲/ *,例如:

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

或者,如果你想要一些基本路徑,更改URL模式是這樣的: /基本路徑/ *

而打你的方法,你需要使用路徑網址:/基本路徑/學習