@RequestMapping 中的getpost请求传参
package com.painter.controller;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/1015:54
*/
@Controller
@RequestMapping("/member")
public class MemberController {
/**
* http://localhost:8080/member/add?id=10
* @param username 访问/add 时传递的参数值 名字需要和form表单的文本输入框的name属性值相同
* @param password
* @return
*
* * @RequestMapping(value = "/add",method = RequestMethod.POST) 表示只接受post请求
* * @RequestMapping(value = "/add",method = RequestMethod.GET) 表示只接受get请求
*/
@ResponseBody
@RequestMapping(value = "/add")
public String add(String username,String password){
System.out.println("username:"+username+" password:"+password);
return "add OK";
}
}
form表单 中的文本输入框的那么必须和接口中的方法的形参的名字相同,这样才能将表单中的参数作为调用接口时传的参数
<form action="${pageContext.request.contextPath}/member/add" method="post" style=" position: absolute;margin-left: 600px;margin-top: 300px;">
<div>
<input type="text" id="username" name="username" placeholder="用户名"> <br><br>
<input type="password" id="password" name="password" placeholder="密码"><br><br>
<input type="submit" name="submit" id="submit">
</div>
</form>