本文主要解决映射配置跳转失败 报404问题
相关文件
com.liber.controller.UserController
package com.liber.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
// 配置映射
@RequestMapping("/quick")
public String save(){
System.out.println("Controller saving ...");
return "success.jsp";
}
}
src/main/resources/spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--controller组件扫描-->
<context:component-scan base-package="com.liber.controller"/>
</beans>
web/success.jsp
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2024-4-28
Time: 21:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>
问题:跳转失败 出现找不到properties
解决:
- 配置springmvc的前端控制器时,param名称固定为contextConfigLocation,否则报错。
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
- 配置applicationContext.xml文件时,一定要注意细节,如外部文件加载时路径一定要加classpeth:
<!-- 加载外部context文件--> <context:property-placeholder location="classpath:jdbc.properties"/>
问题:跳转失败 报错404
在“项目结构-工件”中导入库文件中所有库到WEB-INF/lib: