标记在类上,把类标记为SpringMVCController对象;分发处理器扫描有该注解的类的方法,并检测该方法是否使用了@RequestMapping注解
用于处理请求地址映射;可以用于类和方法;如果用于类上;该类所有的响应请求的方法都以该地址为父路径
参数:
表单文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<style>
div{
width: 500px;
margin: 50px auto;
}
</style>
<body>
<div>
<h2>-------用户登录--------</h2>
<br/><br/>
<form action="loginN" method="post">
UserName:
<input type="text" name="userName" />
<br/><br/>
PassWord:
<input type="password" name="passWord"/>
<br/><br/>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
success文件:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆成功!!!!!title>
head>
<body>
<h1>登陆成功!!!!h1>
body>
html>
注:
value的值必须与表单上的form action的值一样
@Controller
public class UserController {
@RequestMapping(value = "/login1",method = RequestMethod.POST)
public String userGetUserInfo(String userName,String passWord){
//两个属性值必须与表单里的属性值一模一样
System.out.println("用户名:"+userName);
System.out.println("密码:"+passWord);
return "success";
}
}
@Controller
public class UserController {
@RequestMapping(value = "/login2",method = RequestMethod.POST)
public String getUserByRequest(HttpServletRequest hr) throws UnsupportedEncodingException {
hr.setCharacterEncoding("charset=utf-8");
String name=hr.getParameter("userName");
String password=hr.getParameter("passWord");
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
return "success";
}
}
bean文件:
package com.mvc.beans;
import lombok.Data;
/**
* @author Una
* @date 2022/8/12 14:17
* @description:
*/
@Data
public class User {
//属性必须和表单属性相同
private String userName;
private String passWord;
}
Controller文件
@Controller
public class UserController {
@PostMapping(value = "/login3")
public String getUser(User user){
System.out.println("用户名:"+user.getUserName());
System.out.println("密码:"+user.getPassWord());
return "success";
}
}
前端通过JSON的封装后提交给Controller,Controller通过Bean接收
<script type="text/javascript">
$(function () {
$("#button_submit").click(function () {
var name=$("#userName").val;
var pwd=$("#password").val;
var user={
userName:name
password:pwd
};//拼装成json格式
$.ajax({
type:"POST",
url:"/temp.do",
data:user,
success:function (data) {
console.info("成功");
},
error:function(e){
console.info("出错"+e);
}
});
});
});
</script>
@Controller
public class UserController {
@RequestMapping(value = "/temp.do")
public String userGetUserInfo(String userName,String passWord){
System.out.println("用户名:"+getUserName());
System.out.println("密码:"+getUserPwd());
return "success";
}
}