Spring MVC @GetMapping和@PostMapping注解的使用

创建HelloWorldController

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class HelloWorldController {
	//只接受get方式的请求
	@GetMapping("/testGetMapping")
	public String testGetMapping(Model model) {
		model.addAttribute("msg","测试@GetMapping注解");
		return "success";
	}
	//只接受post方式的请求
	@PostMapping("/testPostMapping")
	public String testPostMapping(Model model) {
		model.addAttribute("msg","测试@PostMapping注解");
		return "success";
	}
}

创建index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




index


	

创建success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>




success


	${requestScope.msg }

启动Tomcat访问index.jsp

Spring MVC @GetMapping和@PostMapping注解的使用_第1张图片

点击【测试@GetMapping注解】

点击【测试@PostMapping注解】

你可能感兴趣的:(spring,mvc)