Spring:url无法正确解析链接
虽然我对Java有丰富的经验,但我对Spring框架和Web应用程序还是很陌生的。当我在本地的Tomcat服务器上运行我的网站的网址是:http://localhost:8080/myApp/
Spring:url无法正确解析链接
现在的请求映射委托我到我的主页有:
@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>
但春天:URL始终解析http://localhost:8080/myApp/
- 我目前的页面。此外,当我只是使用这样的链接:<a href="/otherSite">link</a>
,它总是解决http://localhost:8080/otherSite
而不是像我所料的http://localhost:8080/myApp/otherSite
。我怎样才能让我的链接工作?是http://localhost:8080/myApp
隐式定义为我的上下文,还是可以/应该将其更改为?
此外,本地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之前链接可以确保它会在两个工作CAS es
当面临同样的问题,我用c liblary :
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<a href="<c:url value="/otherSite"/>"> Other site </a>
感谢您的回答,但并未解决问题。 – jeyp 2012-02-17 07:31:12
这应该做的伎俩:
<spring:url value="/some_link" context="myApp" var="apps_url" />
<a href="${apps_url}">link</a>
apps_url应该等于:http://localhost:8080/myApp/some_link
这不适合我,在页面'http:// localhost:8080/myApp/apps'上,url已解析为'http:// localhost:8080/myApp/apps'。也许有一个错误,当你失败时,你当前所在的页面是默认的输出? – jeyp 2012-02-17 10:16:43
的Tomcat可以在同一时间执行许多Web应用程序。考虑场景:
- Tomcat是在8080
- 的Tomcat Web运行的应用程序文件夹中有运行
- appA.war
- appB.war
- ROOT.war
URL http://localhost:8080/将获得对tomcat和tomcat的请求必须决定三个应用程序中的哪一个应该为请求提供服务,因为该URL不能识别该应用程序,该请求将由tomcat路由到作为tomcat中的根web应用程序的ROOT.war。
当你在appA中有JSP时。与<a href="/otherSite">link</a>
战争中,然后解析输出的Web浏览器会认为该请求应该提交到服务器的根目录,因为href开始于/因此浏览器在请求到达tomcat的时候会将url设置为url http://localhost:8080/otherSite假设/ otherSite指的是一个名为otherSite.war的web应用程序,但当然不在那里。
如果使用<a href="otherSite">link</a>
浏览器会认为这是一个相对URL,并因此将请求发送到http://localhost:8080/appA/otherSite因为生成URL的网页是在一般担任了上下文的http://localhost:8080/appA
所以,你需要确保来自JSP页面的URL是相对的,或者将上下文名称添加到生成的url,这是<spring:url />
和<c:url />
标签所做的。
本地tomcat服务器上的url与web应用程序发布时的url之间是否有连接?
上下文的URL与tomcat webapps文件夹中war文件的名称相同,除非您在server.xml或context.xml文件中更改该文件。
@ams和Jhonathan,我认为这个问题是因为我使用eclipse运行Tomcat服务器。 $ {pageContext.servletContext.contextPath}为我解析为“”(空字符串)。我根本找不到任何战争档案。我可以使用maven构建我的spring应用程序来创建.war文件,但是在eclipse中使用tomcat时,有没有解决我的问题的方法?它似乎像Tomcat(隐式?)使用http:// localhost:8080/packageName作为根,但当你问上下文它不会返回“packageName” – jeyp 2012-02-18 09:19:04
我发现我的项目在“C:\ workspace \ .metadata \ .plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ packageName“ - 所以我认为按eclipse中的”保存“更改将复制到那里并由tomcat加载。但我怎样才能让我的项目检索正确的基础网址? – jeyp 2012-02-18 09:28:03
我完全不知道为什么它没有为我工作,但使用$ {facesContext.externalContext.requestContextPath}返回正确的路径。因此,像这篇文章一样接收上下文路径是某种正确的解决方案。感谢 – jeyp 2012-02-19 17:16:17