gpt4 book ai didi

java - Spring:url 没有正确解析链接

转载 作者:搜寻专家 更新时间:2023-11-01 04:00:49 25 4
gpt4 key购买 nike

虽然我对 Java 很熟悉,但我对 Spring 框架和 Web 应用程序还很陌生。当我在本地 tomcat 服务器上运行我的网站时,URL 是:http://localhost:8080/myApp/

现在请求映射委托(delegate)我访问我的主页:

@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} 始终提供您的应用程序根目录,当您开发时使用 http://localhost:8080/myApp,然后是您的应用程序根目录是 /myapp,但是当您希望将应用程序投入生产时,通常您的应用程序根将是 /,使用 ${pageContext.servletContext.contextPath} strong> 在链接之前确保它在两种情况下都有效

关于java - Spring:url 没有正确解析链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9324338/

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