Spring MVC 首次使用(映射配置)

本文主要解决映射配置跳转失败 报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:

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值