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

3

7

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:

For example, when I create the project, I set the default package as com.sample.myapp. When I run the application, it opens at http://localhost:8080/myapp. Why does the root URL not use my project name, MyProject, instead?

This is a problem because I have to specify all of the URL's in my application to /myapp/resources/css/mycss.css but when I export a .war and deploy it to a Tomcat server, then Tomcat expects the project name instead (it wants me to use /MyProject/resources/css/mycss.css. As a result, all of my links are broken when I deploy (but not when I run on tomcat locally within the SpringSource IDE...)

Has anyone else run into this problem?


Example for comment discussion below:

<bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
    <property name="service"
        value="https://localhost:8443/MyProject/j_spring_cas_security_check" />
    <property name="sendRenew" value="false" />
</bean>
Cuthburt answered 4/12, 2012 at 14:55 Comment(0)
T
12

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.

Trimester answered 4/12, 2012 at 15:24 Comment(2)
Your assumption was correct, and that's exactly what I was looking for. Thank you very much!Cuthburt
Thanks a lot. After putting so much time, changing servlet-context, pom details and cleaning / building project I still was not able to find it. Even looked on SO and couldn't find anything i.e until I started asking a new question.Mary
W
3

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.

Winsome answered 5/12, 2012 at 6:18 Comment(5)
That was very informative, thank you. I was starting to look at a way that I wouldn't have to hard code the application name into my URL's, and that seems like it will do the trick! Thanks!Cuthburt
Florian, one more question. I also have some URL's in my applicationContext-security.xml in which I hardcode in the context path. Do you know the recommended way to make the URL's in the bean definitions relative?Cuthburt
What kind of URL is it? If it is for spring-security then just provide the relative URL without context-path, like this: <security:openid-login default-target-url="/items">Winsome
Florian, please see my revised post for an example of the type of URL.Cuthburt
Sorry, I don't know. But there is a related question: #4555706Winsome
A
0

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)

Affusion answered 4/12, 2012 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.