gpt4 book ai didi

java - Spring MVC : URL Pattern

转载 作者:行者123 更新时间:2023-12-02 13:28:11 25 4
gpt4 key购买 nike

这是我的 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>

如果我更改此代码

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

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

我可以点击我的 Controller 代码,如下所示

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

如果我错过了任何依赖项,请告诉我

最佳答案

使用此 url 模式 <url-pattern>/learn/*</url-pattern>配置,要命中您的方法,url 路径将为:/learn/learn

web.xml 上的 url-pattern 属性就像 spring servlet 的基本路径。因此,一个不错的选择是将 url-pattern 更改为/* ,如下所示:

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

或者,如果您想要一些基本路径,请将 url 模式更改为如下所示:/基本路径/*

要使用您的方法,您需要使用路径 url:/basepath/learn

关于java - Spring MVC : URL Pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43311108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com