【问题标题】:Spring Controller not invoked for html views in mvc未为 mvc 中的 html 视图调用 Spring Controller
【发布时间】:2023-03-31 10:21:01
【问题描述】:

我想在 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>

主页 URL 工作 - 显示

https://localhost:8080/middlewareAutomation/

登录页面 URL 不工作 - 抛出 404,请求的资源不可用错误。

https://localhost:8080/middlewareAutomation/login

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 不,那也没用。首先,我没有看到请求被映射到控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多