Spring:url无法正确解析链接 [英] Spring:url not resolving links correctly
问题描述
我对Spring框架和Web应用程序很陌生,尽管我对Java很有经验。当我在本地tomcat服务器上运行我的站点时,URL为: http:// localhost:8080 / myApp /
I'm pretty new to the Spring framework and web applications, though I'm experienced with Java. When I run my site on a local tomcat server the URL is: http://localhost:8080/myApp/
现在请求映射将我委托给我的主页:
Now a request mapping delegates me to my home page with:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String someMethod(Model model) { ...
return "index"; }
现在在文件 index.xhtml
我使用< a href =apps />链接< / a>
链接到另一个页面,但是当我想链接回索引页面时我必须使用< a href =../ index /> link< / a>
。我搜索了一个解决方案,发现:
Now within a file index.xhtml
I link to another page with <a href="apps/">link</a>
but when I want to link back to the index page I have to use <a href="../index/">link</a>
. I searched for a solution and found:
<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 /
?
But spring:url always resolves to http://localhost:8080/myApp/
- the page that I'm currently on. In addition, when I just use a link like this: <a href="/otherSite">link</a>
, it always resolves to http://localhost:8080/otherSite
and not http://localhost:8080/myApp/otherSite
like I expected. How can I get my link to work? Is http://localhost:8080/myApp
implicitly defined as my context or can/should it be changed to http://localhost:8080/
?
此外,本地tomcat服务器上的URL与Web应用程序发布时的URL之间是否存在任何连接?
Also, is there any connection between the URL on local tomcat server and the URL the web application will have when it's published?
以下是我的一些应用程序文件:
Here are some of my application files:
servlet-context.xml:
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>
推荐答案
优良作法是将所有链接放置如下
It is good practice to put all your links as follows
<a href="${pageContext.servletContext.contextPath}/othersite"> Other site </a>
$ {pageContext.servletContext.contextPath} 始终为您的应用提供根,当您开发使用 http:// localhost:8080 / myApp
时,您的应用程序根目录是 / myapp ,但是当您想要放置生产中的应用程序通常你的应用程序根目录 / ,在链接之前使用 $ {pageContext.servletContext.contextPath} 确保它在两种情况下都能正常工作
${pageContext.servletContext.contextPath} always gives your application root, when you are developing use http://localhost:8080/myApp
, then your application root is /myapp, but when you want to place your application in production generally your application root will be /, using ${pageContext.servletContext.contextPath} before links you ensure it will work in both cases
这篇关于Spring:url无法正确解析链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!