0

I have followed many links but didn't get luck anywhere. My controller is not calling and hence 404 error is coming up.

Calling url is : localhost:8080/DemoApp/login

Below is the snap of code.

LoginController.java

@Controller
public class LoginController {  
public LoginController(){
    System.out.println("in controller");
}

@GetMapping("/login")
public String doLogin(Model model)  throws Exception{

    System.out.println("Login access");

    return "login";
}

}

spring-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"
     xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
         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.3.xsd">
 
  <mvc:annotation-driven />
 <context:annotation-config />
  <mvc:default-servlet-handler/>
  <context:component-scan base-package="com.demoapp.LoginController" />
   <context:annotation-config />
  <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">      
  <property name="prefix">
  <value>/WEB-INF/views/</value>
  </property>
  <property name="suffix">
  <value>.jsp</value>
  </property>
  </bean>

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_2_5.xsd"
id="WebApp_ID" version="2.5">

  <display-name>DemoApp</display-name>
    <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-servlet.xml</param-value>
  </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

enter image description here

enter image description here

Ajay_Kumar
  • 1,381
  • 10
  • 34
  • 62
  • is the result any different if you use: http://localhost:8080/login ? You can amend the path to the one you're using if you set the property `System.setProperty("server.servlet.context-path", "/DemoApp");` in the class annotated `@SpringBootApplication` – Rob Evans Oct 23 '20 at 21:17

1 Answers1

0

At your controller class you need an extra annotation:

@Controller
@RequestMapping("/DemoApp")
stackstack293
  • 341
  • 3
  • 12