0
我想在tomcat之上用MAVEN創建簡單的Spring MVC應用程序。它有一個帶有登錄鏈接的主屏幕,點擊時應該重定向到登錄頁面。爲了實現這一點,我創建了一個登錄控制器來返回login.html。在主頁中單擊登錄時。我面臨的問題是MVC控制器沒有被觸發。請幫忙!!未在mvc中爲html視圖調用Spring控制器
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/
登錄頁面網址不工作 - 拋出404,請求資源不可用錯誤。
https://localhost:8080/middlewareAutomation/login
沒有,也沒有工作。首先,我沒有看到請求被映射到控制器。 – user3463832