ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

@GetMapping无效解决办法

2021-11-08 13:05:06  阅读:347  来源: 互联网

标签:xml 解决办法 www 无效 springframework mvc GetMapping org


@GetMapping无效解决办法

在SSM框架中,可以使用注解来减轻开发的工作量,掌握每个注解的作用以及底层实现机制便于分析问题。最近遇到@GetMapping无效的情形,下面就解决方法进行总结。

原因分析

https://www.jianshu.com/p/69e9f9ed5b36 里,对@GetMapping无效的原因进行了详细分析。这里不在重述。

解决办法

1、在配置xml文件里,添加:

<mvc:annotation-driven />

2、为了避免添加注解驱动后引起编译问题,还需要在xml配置文件的beans里添加如下属性。

xmlns:mvc="http://www.springframework.org/schema/mvc"

以及给xsi:schemaLocation增加赋值项:

http://www.springframework.org/schema/mvc
  	   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

修改后,springmv-config.xml配置文件内容大致如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
	   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-4.3.xsd
	   http://www.springframework.org/schema/mvc
  	   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	   http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  	<mvc:annotation-driven />
	<!-- 指定需要扫描的包 -->
	<context:component-scan base-package="com.example.controller" />
	<!-- 定义视图解析器 --> 
	<bean id="viewResolver" class=
    "org.springframework.web.servlet.view.InternalResourceViewResolver">
	     <!-- 设置前缀 -->
	     <property name="prefix" value="/WEB-INF/jsp/" />
	     <!-- 设置后缀 -->
	     <property name="suffix" value=".jsp" />
	</bean>
</beans>  

验证

1、测试代码

@Controller
@RequestMapping(value = "/hello")
public class FirstController {
	@GetMapping(value = "/3")
	public String test3(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		// 向模型对象中添加数据
		model.addAttribute("msg", "这是我的第二个Spring MVC程序");
		// 返回视图页面
		return "first";
	}
}

2、测试结果
在这里插入图片描述

标签:xml,解决办法,www,无效,springframework,mvc,GetMapping,org
来源: https://blog.csdn.net/zhangju978/article/details/121205378

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有