栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将Spring MVC控制器映射到带有和不带有斜杠的uri?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将Spring MVC控制器映射到带有和不带有斜杠的uri?

例如,如果您的Web应用程序位于Web服务器的webapps目录中,

webapps/myapp/
则可以使用
http://localhost:8080/myapp/
默认的Tomcat端口访问此应用程序上下文的根。
我认为 默认情况下,无论是否有斜杠,这都可以使用-肯定是Jetty v8.1.5中的情况

按下

/myapp
Spring
DispatcherServlet接管之后,
<servlet-name>
按照您的配置将请求路由到
web.xml
,在您的情况下为
/ui/*

然后,DispatcherServlet将所有请求从路由

http://localhost/myapp/ui/
@Controller

控制器 本身可以使用

@RequestMapping(value = "/*")
炫魅()
方法,这将导致两个
http://localhost/myapp/ui/
http://localhost/myapp/ui
被路由到 炫魅()

注意:由于SPR-7064,您还应该使用Spring> =
v3.0.3

为了完整起见,以下是我对此进行测试的文件:

src / main / java / controllers / UIRootController.java

package controllers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class UiRootController {  @RequestMapping(value = "/*")  public ModelAndView mainPage() {    return new ModelAndView("index");  }  @RequestMapping(value={"/other"})  public ModelAndView otherPage() {    return new ModelAndView("other");  }}

WEB-INF / web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0" metadata-complete="false">  <servlet>    <servlet-name>ui</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>    <!-- spring automatically discovers /WEB-INF/<servlet-name>-servlet.xml -->  </servlet>  <servlet-mapping>    <servlet-name>ui</servlet-name>    <url-pattern>/ui/*</url-pattern>  </servlet-mapping></web-app>

WEB-INF / ui-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:p="http://www.springframework.org/schema/p"  xmlns:context="http://www.springframework.org/schema/context"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="controllers" /><bean id="viewResolver"   p:order="2"  p:viewClass="org.springframework.web.servlet.view.JstlView"  p:prefix="/WEB-INF/views/"  p:suffix=".jsp"/></beans>

而且2 JSP文件的

WEB-INF/views/index.jsp
WEB-INF/views/other.jsp

结果:

  • http://localhost/myapp/
    ->目录列表
  • http://localhost/myapp/ui
    http://localhost/myapp/ui/
    -> index.jsp
  • http://localhost/myapp/ui/other
    http://localhost/myapp/ui/other/
    -> other.jsp

希望这可以帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417099.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号