在mvc中没有为html视图调用Spring Controller

时间:2017-09-12 21:44:19

标签: java spring spring-mvc web-applications spring-web

我想在tomcat上用MAVEN创建简单的Spring MVC应用程序。它有一个带有登录链接的主屏幕,点击该链接时应重定向到登录页面。为了实现这一点,我创建了一个登录控制器来返回login.html。在主页中单击登录时。我面临的问题是MVC控制器没有被触发。请帮忙!!

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>

首页网址 - 显示

https://localhost:8080/middlewareAutomation/

登录页面网址无效 - 抛出404,请求的资源不可用错误。

https://localhost:8080/middlewareAutomation/login

1 个答案:

答案 0 :(得分:0)

我认为第二个URL的正确版本应为

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