Spring MVC point to host name

1.2k views Asked by At

I'm setting up a Spring MVC project for my own knowledge and I'm hitting a bit of a bump. I have a welcome file and a web service set up and they work pretty nicely. However, instead of localhost:8080/ZFGC/ hitting the welcome file, I'd like localhost:8080/ to hit it. From there, I want to be able to call my web services in a similar manner, i.e. without the ZFGC/. I haven't been able to find a tutorial that actually explains this properly. Could someone enlighten me?

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>zfgc</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>zfgc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>zfgc</servlet-name>
        <url-pattern>/welcome.jsp</url-pattern>
        <url-pattern>/welcome.html</url-pattern>
        <url-pattern>/index.htm</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ZFGC</groupId>
  <artifactId>ZFGC</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>ZFGC</name>
  <description>Zelda Fan Game Central</description>

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

zfgc-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.zfgc.forum.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

directory structure:

2

There are 2 answers

2
The Coder On BEST ANSWER

Since you're running the application inside eclipse (by tomcat), it's just a one step configuration.

Follow the Steps. (Be sure to stop tomcat before doing this)

1) You'll find a Folder as "Servers" in Project list displayed in eclipse.

2) Expand Servers -> Tomcat v8.0

3) You'll see list of configuration files. Open server.xml

4) Scoll to the bottom. You'll see a line which will be like this,

<Context docBase="ZFGC" path="/ZFGC" reloadable="true" source="org.eclipse.jst.jee.server:ZFGC"/>

5) Now simply change that line to this

<Context docBase="ZFGC" path="/" reloadable="true" source="org.eclipse.jst.jee.server:ZFGC"/>

and save the changes.

To mention accurately, path="/ZFGC" is changed to path="/" . That's it. Now start your tomcat and you can access your application in http://localhost:8080.

0
Amin Abu-Taleb On

To hit your app with http://localhost:8080 the only thing you need to do is deploy your web application as the ROOT application in your Application Server.

That basically consists on naming your WAR file ROOT.war

In Maven you can do it easily using:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>ROOT</warName>
                    <outputDirectory>war</outputDirectory>
                </configuration>
            </plugin>
</plugins>