Spring Controller not invoked for html views in mvc

141 Views Asked by At

I want to create simple Spring MVC application with MAVEN on top of tomcat. It has a home screen with a login link, which when clicked should redirect to the login page. To achieve this i have created a login controller to return login.html. When login is clicked in home page. The problem i'am facing is that the MVC controller is not being triggered. Please help!!

This is my folder structure: LoginController.java:

package com.bestbuy.tcs.mw.automation.controller;
@Controller
public class LoginController {
    //@Autowired
    //LoginService loginService;

    @RequestMapping("/login")
    public String showLogin(HttpServletRequest request, HttpServletResponse response){
        System.out.println("inside ");
        return "/login";
    }
}

/WEB-INF/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>MiddlewareAutomation</display-name>
    <welcome-file-list>
        <welcome-file>home.html</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value> 
    </context-param>
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>/</url-pattern>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

/WEB-INF/spring-mvc-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc">
    <!-- <import resource="classpath:jbr/config/user-beans.xml" /> -->
    <context:component-scan base-package="com.bestbuy.tcs.mw.controller" />    
    <context:annotation-config />
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
    </bean>
</beans>

/webapp/home.html:

<html>
<body>
<h2>Middleware Automation home!!!</h2>
<a href="login">Login</a>
</body>
</html>

/WEB-INF/views/login.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>middle ware automation login page</title>
</head>
<body>

</body>
</html>

Homepage URL working - displays

https://localhost:8080/middlewareAutomation/

Login Page URL NOT Working - throws 404, requested resource not available error.

https://localhost:8080/middlewareAutomation/login
1

There are 1 best solutions below

1
On

I think that the correct version of second URL should be

https://localhost:8080/middlewareAutomation/login.html