使用 Maven Jetty 外掛啟動專案時,訪問根路徑 https://localhost:8080/ 出現 No mapping found for HTTP request with URI [/] in DispatcherServlet 錯誤,無法自動跳轉到登入頁。
DispatcherServlet 沒有對應根路徑 / 的處理器映射,且預設歡迎頁面機制未生效。
在 Spring MVC 配置檔中新增:
<!-- 將沒有 mapping 的 URL 交給預設的 Servlet 處理 -->
<mvc:default-servlet-handler/>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>login.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
https://localhost:8080/mvc:default-servlet-handler 將請求轉交給容器預設 Servletwelcome-file-list 找到 login.jsphttps://localhost:8080/login.jsp@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:/login"; // 或 return "login";
}
}
mvc:default-servlet-handler:啟用靜態資源處理與預設 Servlet 轉發welcome-file-list:Servlet 容器層面的預設頁面機制這是 Spring MVC 專案部署時常見的配置問題,正確設定後即可實現根路徑自動跳轉登入頁。