Spring:url not resolving links correctly

后端 未结 4 2092
执笔经年
执笔经年 2021-02-06 18:22

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:8

相关标签:
4条回答
  • 2021-02-06 18:54

    This should do the trick:

    <spring:url value="/some_link" context="myApp" var="apps_url" />
    <a href="${apps_url}">link</a>
    

    apps_url should be equal to: http://localhost:8080/myApp/some_link

    0 讨论(0)
  • 2021-02-06 19:01

    It is good practice to put all your links as follows

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

    ${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

    0 讨论(0)
  • 2021-02-06 19:04

    Tomcat can have many web apps executing at the same time. Consider the scenario:

    • tomcat is running on 8080
    • tomcat web apps folder has three apps running in it
      • appA.war
      • appB.war
      • ROOT.war

    The URL http://localhost:8080/ will be get the request to tomcat and tomcat must decide which of three apps should service the request since the URL does not identify the app the request will be routed by tomcat to the ROOT.war which is the root web app in tomcat.

    When you have a JSP in appA.war with <a href="/otherSite">link</a> in it then the web browser that parses the output will figure that the request should be submitted to root of the server since the href started with / therefore the browser puts togethec the url http://localhost:8080/otherSite when the request get to tomcat tomcat assumes that /otherSite is refering to a web app called otherSite.war but of course that is not there.

    if you use <a href="otherSite">link</a> the browser will assume this is a relative URL and so it will send the request to http://localhost:8080/appA/otherSite because the page that generate the url was served out of the context http://localhost:8080/appA

    So in general you need to make sure that the URLs from a JSP page are relative or that you add the context name to the generated url, this is what <spring:url /> and <c:url /> tags do.

    is there any connection between the url on local tomcat server and the url the webapplication will have when its published?

    the URL for the context is the same as the name of the war file in the tomcat webapps folder unless you change that in the server.xml or in the context.xml file.

    0 讨论(0)
  • 2021-02-06 19:07

    When facing same problem I use c liblary:

     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
     <a href="<c:url value="/otherSite"/>"> Other site </a> 
    
    0 讨论(0)
提交回复
热议问题