ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

How to make my URL to only be "localhost:8080/*"?

I'm just starting out with giving Spring MVC a look so I truly have no idea what I'm doing. Therefore, I started following a series of tutorials on youtube. I've followed them step by step only differing in that I'm using STS instead of og Eclipse but I set the project up like the tut so I can't see why it would matter.

Towards the end we make an index.jsp page, restart the server, and navigate to localhost:8080/index.html and this is where my "problem" begins. I have to use localhost:8080/test/index.html in order to avoid a 404 error.

I think the answer is somewhat obvious but I'm not clear why mine's different than the tut's even though I followed it, names aside, exactly.

How to make my URL to only be localhost:8080/*? I don't want /test/ included.

Beginning of pom.xml:

  <modelVersion>4.0.0</modelVersion>
  <groupId>my.name.spring.test</groupId>
  <artifactId>blog-aggregator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>blog-aggregator</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.json</url-pattern>
        <url-pattern>*.xml</url-pattern>     
    </servlet-mapping>
</web-app>

IndexController.java:

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index() {
        return "/WEB-INF/jsp/index.jsp";
    }
}

dispatch-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="wilk.robert.spring.test.controller" />
</beans>

I'm guessing it has to do with my base package but that seems to limit me on what my package can be; that doesn't sound right (remember I'm ignorant!).

If there's any important code missing I will promptly add it.

Upvotes: 1

Views: 1318

Answers (2)

Rename your war to ROOT.war and then deploy it

If the webapp is called root.war or the directory is called root/ then Jetty deploys it at the / context.

jetty link

Add the next line to your pom.xml to rename your war file:

<build>
  <finalName>ROOT</finalName>
</build>

If you are using the jetty plugin modify your plugin configuration like the following code:

 <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.22</version>
                <configuration>
                    <contextPath>/</contextPath>                     
                </configuration>
                ...
            </plugin>

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

You need to make your webapp as the ROOT app. Easiest way of doing this either naming your war file as ROOT.war or taking your exploded webapp directory and rename is as ROOT

${jetty.home}/webapps/ROOT

Upvotes: 1

Related Questions