虽然我对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>
@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