当前位置: 首页 > 面试题库 >

Spring:URL无法正确解析链接

高经艺
2023-03-14
问题内容

我对Spring框架和Web应用程序还很陌生,尽管我对Java有经验。当我在本地tomcat服务器上运行网站时,URL为:http://localhost:8080/myApp/

现在,请求映射将我委托给我的主页,其中包括:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String someMethod(Model model) { ... 
   return "index"; }

现在在文件中,index.xhtml我链接到另一个页面,<a href="apps/">link</a> 但是当我想链接回索引页面时,必须使用<a href="../index/">link</a>。我搜索了一个解决方案,发现:

<spring:url value='/apps' var="apps_url" />
<a href="${apps_url}">link</a>

但是spring:url总是解析为http://localhost:8080/myApp/-我当前所在的页面。另外,当我只使用像这样的链接时<a href="/otherSite">link</a>,它总是解析为,http://localhost:8080/otherSite而不是http://localhost:8080/myApp/otherSite我期望的那样。如何获得我的工作链接?是否http://localhost:8080/myApp隐式定义为我的上下文,或者可以/应该将其更改为http://localhost:8080/

另外,本地tomcat服务器上的URL和Web应用程序发布时将具有的URL之间是否存在任何连接?

这是我的一些应用程序文件:

servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <context:component-scan base-package="myApp" />

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- Handles HTTP GET requests for /resources/** and /css/** by efficiently 
    serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <resources mapping="/css/**" location="/css/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".xhtml" />
    </beans:bean>

</beans:beans>

来自web.xml的摘录:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

问题答案:

优良作法是将您的所有链接如下

<a href="${pageContext.servletContext.contextPath}/othersite"> Other site </a>

$ {pageContext.servletContext.contextPath}
始终为您提供应用程序根目录,当您开发use时http://localhost:8080/myApp,您的应用程序根目录为 / myapp
,但是当您要将应用程序投入生产时,通常情况下,应用程序根目录将为 / ,使用 $ {pageContext
.servletContext.contextPath}
,以确保在两种情况下都可以使用



 类似资料:
  • GSON不是在一开始就解析我传递的整个JSON字符串吗?因此,最终,我希望新的数据源是对象。那可行吗?

  • 我希望有人能帮我解决这个问题。 我想用WebClient创建一个Rest客户端,从API中检索响应。所以我创建了我的Spring项目,添加了webflux、lombok和h2。我还创建了一个DTO类“CashAccount”和以下方法: 当我使用“.bodyToMono(String.class)”时,所有的功能都很好,我收到了结果: 相反,当我使用“.bodyToMono(cashcount.c

  • 我有一个简单的rest服务来存储时间范围,然而,Spring不能正确解析带有时区的datetime格式。 该实体是

  • 我不知道为什么我的json解析不起作用。这是我正在使用的Api。还有完整json输出的链接http://api.openweathermap.org/data/2.5/forecast/daily?zip=85008 我试图得到每天的最低和最高温度。这是我的代码。引发的异常应该是BEGIN_OBJECT,但在第1行第190列编号。任何帮助不胜感激,谢谢!

  • 这是我用来读取gradle项目中的application.properties文件的代码。 我的文件位于中 这就是我试图在IntelliJ中运行应用程序的方式

  • 我正在使用撒克逊语(9.1)和FOP(0.95)创建目录和书签树。一切正常,除了以下 FOP 输出: 在生成的 PDF 中,所有链接(来自目录和书签树)都指向第一章。为什么?感谢您的任何帮助。 XML输入文件: XSLT 2.0 样式表(仅省略定义块特性的属性集)

  • 问题内容: 我有一个非常棘手的问题,我希望有人能对此有所启发。我正在使用Jquery从另一个网站(我拥有)检索Http响应。收到DOM后,我将对其进行解析以获取某些信息。但是,当我尝试获取链接的href属性时,IE会将本地域添加到href的开头! 这是我的代码: 我的变量PageLink应该是“ /pages/getThisPage.aspx?id=8347”。但是,它将作为“ http://my

  • 通过后端生成的分享链接点击进入本地项目,请求app.js文件的地址并不是运行项目的8080端口,而是ip地址 + 8080 + 链接参数,导致资源无法获取,报错404。