大家好,又见面了,我是你们的朋友全栈君。
创建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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<form action="testGetMapping" method="get">
<button>测试@GetMapping注解</button>
</form>
<br>
<form action="testPostMapping" method="post">
<button>测试@PostMapping注解</button>
</form>
</body>
</html>
创建success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>success</title>
</head>
<body>
${requestScope.msg }
</body>
</html>
启动Tomcat访问index.jsp
点击【测试@GetMapping注解】
点击【测试@PostMapping注解】
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/126462.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...