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

ε祈祈猫儿з 提交于 2019-11-30 13:50:08

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.

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!