mete eroğlu
mete eroğlu

Reputation: 95

Intellij Idea Spring 404 – Not Found

When start project on Tomcat, 404 not found page is opening. Also link is http://localhost:8080. But if enter a http://localhost:8080/web/WEB-INF/views/main-menu.jsp, my index(main-menu) page is opening. I want to opening main page when project start.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     version="4.0">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>/</url-pattern>
</servlet-mapping>

applicationContext.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"
   xmlns:mvc="http://www.springframework.org/schema/cache"
   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.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>

</bean>

HomeController.java

package com.demo.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage(){
        return "main-menu";
    }
}

Also opening browser setting is http://localhost:8080/ in Run/Debug Configurations.Server.Open Browser. In addition Application context is "/" in Run/Debug Configurations.Deployment

Upvotes: 1

Views: 3218

Answers (3)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

Add a lines in your applicationContext.xml file:

<mvc:annotation-driven />
<context:component-scan base-package="com.demo.springdemo" /> 

This is telling to scan and to use annotations like @Controller ,@RestController , etc..

Upvotes: 1

Purushottam
Purushottam

Reputation: 148

add in your application-context

   <mvc:annotation-driven />
    <context:component-scan base-package="com.something"/>

in web.xml add these lines

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext.xml
    </param-value>
  </context-param>

https://github.com/Purushottam10/Demospring/tree/master/src/main/webapp/WEB-INF

Upvotes: 2

user
user

Reputation: 427

where is your dispatcher-servlet code. put this line in your dispatcher-servlet if not given

<context:component-scan base-package="com.something"/>

replace "com.something" with your base package of controller.

And Add a line in your applicationContext.xml

<mvc:annotation-driven />

Upvotes: 1

Related Questions