Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 946
littleK Avatar asked Dec 04 '12 14:12

littleK


2 Answers

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.

like image 61
bimsapi Avatar answered Sep 21 '22 23:09

bimsapi


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.

like image 45
Florian Fankhauser Avatar answered Sep 25 '22 23:09

Florian Fankhauser