SpringMVC映射@RequestMapping(“/res/{a}/{b}“)时出现拼接路径错误问题

本文介绍了一个关于SpringMVC框架中使用Restful风格URL出现路径拼接错误的问题及解决办法。通过调整视图解析器的前缀设置,解决了路径混乱的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Restful风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制,但今天却只要一用这个,springmvc拼接出来的路径就会有问题!


contrller层的代码:

@Controller
public class HelloController{

    @GetMapping("/res/{a}/{b}")
    public String restful(@PathVariable int a,@PathVariable int b, Model model) {
        int res=a+b;
        model.addAttribute("msg",res);
        return "hello";
    }

}

目录结构:

 这里为什么把我的请求路径加到上去了呢?

 检查springmvc-servlet.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.konan.controller"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

最终发现就是这里出了问题!!!

 就在视图拼接这里,WEB-INF前面要写‘/’的,不然的话就会拼接路径就会紊乱,具体原因我也不知道,待日后研究吧

改成:<property name="prefix" value="/WEB-INF/jsp/"/>

成功后访问:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓晓的雨夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值