本文项目代码:https://download.csdn.net/download/qq_43499543/19350300
一、页面传值到控制器
1.使用Request
controller
//通过Request将页面中的值传到控制器
@Controller
public class MyController {
@RequestMapping("register")
public String register(){
return "register";
}
@RequestMapping("show")
public String show(HttpServletRequest request) throws UnsupportedEncodingException {
request.setCharacterEncoding("GBK"); //防止中文乱码
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name +" "+ age);
return "success";
}
}
jsp页面
register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/show" method="post">
姓名:<input type="text" name="name" id="name">
年龄:<input type="text" name="age" id="age">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<h2>成功!</h2>
</body>
</html>
运行Tomcat访问/regsiter地址
提交后自动跳转到/show页面
控制台输出了获取到的数据
2.使用属性进行页面传值
1)修改register.jsp中form的action的值为
<form action="${pageContext.request.contextPath}/show2" method="post">
2)编写Conteoller
//使用属性传值
@Controller
public class MyController2 {
@RequestMapping("register2")
public String register(){
return "register";
}
@RequestMapping("show2")
public String show2(String name,String age){
System.out.println(name + " " + age);
return "success";
}
}
运行,访问register2
注意:
使用属性传值时变量名要和表单组件中的name
值相同,如果不相同可以使用@RequestParam
注解,例如:
public String test(String name,@RequestParam("age") String bge)
3. 使用Bean对象传值
表单中name属性值会自动封装到Bean类中
1)创建Bean
User.java
package com.bean;
public class User {
private String name;
private String age;
//省略了get、set、构造器
@Override
public String toString() {
return name + " " + age;
}
}
2)修改register.jsp中form的action的值为
<form action="${pageContext.request.contextPath}/show3" method="post">
3)Controller
@Controller
public class MyController3 {
@RequestMapping("register3")
public String register(){
return "register";
}
@RequestMapping("show3")
public String show2(User user){
System.out.println(user);
return "success";
}
}
运行
4.映射URL绑定的占位符到方法入参
1)向Controller中添加方法
//4.映射URL绑定的占位符到方法入参
@RequestMapping("pathVariable/{id}")
public String pathVariable(@PathVariable("id") Integer id){
System.out.println(id);
return "../success"; //这里不加../会显示“文.件[/pathVariable/success.jsp] 未找到”,不知道为什么
}
2)向index.jsp中添加如下语句
<a href="pathVariable/1">Path Variable</a><br><br>
3)运行,点击连接,可以看到控制台输出id的值:1
5.绑定请求参数到控制器方法参数
1)向Controller中添加方法
@RequestMapping("/requestParam")
public String requestParam(@RequestParam(value = "name") String name, @RequestParam(value = "pwd") String pwd) {
System.out.println(name + " " + pwd);
return "success";
}
2)向index.jsp中添加如下语句
<a href="requestParam?name=pp&pwd=123">Request Param</a><br><br>
3)运行,点击连接,可以看到控制台输出name和pwd的值:pp 123
二、页面传值到控制器
1.使用Request、Session传值
1)编写页面
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
name:${requestScope.name}<br>
age:${sessionScope.age}<br>
</body>
</html>
2)编写Controller
@Controller
public class MyController {
@RequestMapping("test")
public String show(HttpServletRequest request, HttpSession session){
request.setAttribute("name","pp");
session.setAttribute("age","20");
return "show";
}
}
3)运行Tomcat访问/test
2.使用ModuleAndView传值
1)在Controller中添加方法
@RequestMapping("test2")
public ModelAndView show2(){
Map<String,Object> map = new HashMap<>();
map.put("message","传值");
//第一个参数对应jsp名
ModelAndView modelAndView = new ModelAndView("show",map);
return modelAndView;
}
2)修改show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
name:${requestScope.name}<br>
age:${sessionScope.age}<br>
message:${requestScope.message}<br>
</body>
</html>
3)运行,访问test2
3.使用ModelMap传值
1)在Controller中添加以下方法
@RequestMapping("test3")
public String show3(ModelMap modelMap){
modelMap.addAttribute("error","登陆失败");
return "show";
}
2)修改show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
name:${requestScope.name}<br>
age:${sessionScope.age}<br>
message:${requestScope.message}<br>
error:${requestScope.error}<br>
</body>
</html>
3)启动,访问test3