SpringSource IDE does not use project name as root URL for Spring MVC application

后端 未结 3 843
旧时难觅i
旧时难觅i 2021-01-02 19:34

When I create a Spring MVC Template Project with the SpringSource IDE, I run the application and the root URL is set as the last word of the default package name:

Fo

相关标签:
3条回答
  • 2021-01-02 19:51

    It depends on how you run the application. I assume you chose "Run on Server" from within the SpringSource IDE (STS)? If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.

    Hope that helps.

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

    Not sure about STS, but the war file name is set in pom file :

    </dependencies>    
    <build>
    <finalName>yourProject</finalName>
    

    (just realized you didn't specify maven, oh well)

    0 讨论(0)
  • 2021-01-02 20:10

    The first part in the URL after host and port is called the context-path. In your case myapp. In tomcat the context-path is equal to the name of the war-file. If you name the war-file ROOT.war then no context-path is used.

    As bimsapi mentioned you can change the context-path in STS, too.

    But: You can and should make your app independent of the context-path. If you are using JSP then build links with the <c:url /> tag:

    <c:url value="/resources/css/mycss.css" />
    

    This outputs the correct url including the actual context-path. Or your can store it in a variable and use it later:

    <c:url value="/items/index" var="cancel-url" />
    <a href="${cancel-url}">Cancel</a>
    

    In Java code you can get the context-path with:

    request.getContextPath()
    

    where request is the current HttpServletRequest object. Using this makes your app completely independent of the actual context-path which simplifies deployment a lot.

    0 讨论(0)
提交回复
热议问题