1

I am creating simple spring-mvc application referring this url. I created maven web project by referring url.

After creating this project some default file named 'index.jpg' is generated in this project. And when I build and run this project it was shpoing content of 'index.jsp' file.

Now I edited content of pom.xml file and it looks like this:

 <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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.leader.unisys</groupId>
 <artifactId>sample-application</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>sample-application Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <properties>
 <java.version>1.6</java.version>
 <spring.version>4.0.3.RELEASE</spring.version>
 <cglib.version>2.2.2</cglib.version>
 </properties>

 <dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
  </dependency>
    <!-- Spring core & mvc -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
    <type>jar</type>
    <scope>compile</scope>
  </dependency>

<!-- CGLib for @Configuration -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib-nodep</artifactId>
    <version>${cglib.version}</version>
    <scope>runtime</scope>
  </dependency>


  <!-- Servlet Spec -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>sample-application</finalName>
  </build>
</project>

The project structure looks like as shown in the image:

enter image description here

Now, How can I make this project to work as per my mapping in the spring-servlet.xml file.

view resolver:

 public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

content from controller:

@RequestMapping(value="/")
public ModelAndView getHomePage(){
    return new ModelAndView("home");
}

web xml default content:

<web-app>
    <display-name>Archetype Created Web Application</display-name>
 </web-app>

I want to display home.jsp after hitting http://localhost:8080/sample-application. Can someone give me the instructions to do this. I am working with maven for first time.

Madhusudan
  • 4,637
  • 12
  • 55
  • 86

3 Answers3

0

what you are asking for has nothing to do with Spring or Maven but Java EE / Web container configuration - via web.xml

Here is a link that should help .. http://docs.oracle.com/cd/E14571_01/web.1111/e13712/configurejsp.htm#WBAPP183

Relevant parts copied over: Welcome files are defined at the Web application level. If your server is hosting multiple Web applications, you need to define welcome files separately for each Web application. You define Welcome files using the welcome-file-list element in web.xml. (The web.xml file is located in the WEB-INF directory of your Web application.) The following is an example Welcome file configuration:

Welcome File Example

<servlet>
  <servlet-name>WelcomeServlet</servlet-name>
  <servlet-class>foo.bar.WelcomeServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>WelcomeServlet</servlet-name>
  <url-pattern>*.foo</url-pattern>
</servlet-mapping>

<welcome-file-list>
  <welcome-file>/welcome.foo</welcome-file>
</welcome-file-list>

hope this helps.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
jayaram S
  • 580
  • 6
  • 13
0

Move your home.jsp under WEB-INF. And then add this line in your web.xml

<welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
</welcome-file-list>

This <welcome-file-list> in web.xml tells the application that it has to be loaded first.

programmer
  • 472
  • 4
  • 5
0

When we start the web application, by default web container looks for the welcome-file-list present in web.xml, if the welcome-file-list does not exist in web.xml then the container will look for index.html,index.htm, and index.jsp under the web-content folder in case of a normal dynamic web project, but incase of maven project container looks under the web-app folder. Container follows the order and if the first file exists it will execute that file and if it doesn't exist, the container follows the order until the third file. If any one of the files doesn't exist container will throw a 404 error.

For reference https://www.javatpoint.com/welcome-file-list

The above flow is the same for both dynamic and maven web projects, the only thing that differs from each other is that we have a web-content folder in case of dynamic WP and a web-app folder incase of maven WP

D S
  • 258
  • 9
  • 25