SpringMVC--向前台传递数据
在上篇博客基础上继续
第一种方式:返回一个ModelAndView
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import java.util.Date;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.neuedu.springmvc.po.User;
import com.sun.javafx.scene.accessibility.Attribute;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
/*向前台传递数据的第一种方式:
1.返回一个ModelAndView*/
@RequestMapping("/testReturnData") //新增代码
public ModelAndView testReturnData() {
ModelAndView modelAndView= new ModelAndView();
modelAndView.addObject("date", new Date());
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView; //新增代码
}
/*实体类*/
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println(user);
return "success";
}
@RequestMapping("/testParamsRequired")
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success";
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping...");
return "success";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>
<br></br>
<form action="testPojo" method="post">
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testPojo">
</form>
<br></br>
<a href="testReturnData"> Hello testReturnData</a> //新增代码
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h4>Success page</h4>
<hr>
<h4>
当前日期:${requestScope.date} //新增代码
用户名:${requestScope.username}
</h4>
</body>
</html>
输出:点击按钮---返回当前日期
第二种方式:返回一个ModelAndView
2.在springMVC的参数中可以直接使用的有:
HttpServletRequest
HttpServletResponse
HttpSession
Map
Model
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import java.util.Date;
import java.util.Map;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.neuedu.springmvc.po.User;
import com.sun.javafx.scene.accessibility.Attribute;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest {
/*向前台传递数据的第二种方式: //新增代码
2.在springMVC的参数中可以直接使用的有:
HttpServletRequest
HttpServletResponse
HttpSession
Map
Model //新增代码
*/
@RequestMapping("/testReturnData2")
public String testReturnData2(Map<String,Object> map) {
User user = new User("li", "1234", "female", "1834567@163.com", "15603320164");
map.put("username","zhang2");
map.put("user", user);
return "success";
}
/*向前台传递数据的第一种方式:
1.返回一个ModelAndView*/
@RequestMapping("/testReturnData")
public ModelAndView testReturnData() {
ModelAndView modelAndView= new ModelAndView();
modelAndView.addObject("date", new Date());
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
/*实体类*/
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println(user);
return "success";
}
@RequestMapping("/testParamsRequired")
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success";
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping...");
return "success";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>
<br></br>
<form action="testPojo" method="post">
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testPojo">
</form>
<br></br>
<a href="testReturnData"> Hello testReturnData</a>
<br></br>
<a href="testReturnData2"> Hello testReturnData2</a> //新增代码
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h4>Success page</h4>
<hr>
<h4>
当前日期:${requestScope.date}
用户名:${requestScope.username}
</h4>
<h4>
user:${requestScope.user.password}
用户名:${requestScope.username}
</h4>
</body>
</html>
输出:
第三种方式:返回一个Model
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import java.util.Date;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.neuedu.springmvc.po.User;
import com.sun.javafx.scene.accessibility.Attribute;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest { //新增代码
@RequestMapping("/testReturnData3")
public String testReturnData3(User user,Model model) {
model.addAttribute("user",user);
return "success";
} //新增代码
/*向前台传递数据的第二种方式:
2.在springMVC的参数中可以直接使用的有:
HttpServletRequest
HttpServletResponse
HttpSession
Map
Model
*/
@RequestMapping("/testReturnData2")
public String testReturnData2(Map<String,Object> map) {
User user = new User("li", "1234", "female", "1834567@163.com", "15603320164");
map.put("username","zhang2");
map.put("user", user);
return "success";
}
/*向前台传递数据的第一种方式:
1.返回一个ModelAndView*/
@RequestMapping("/testReturnData")
public ModelAndView testReturnData() {
ModelAndView modelAndView= new ModelAndView();
modelAndView.addObject("date", new Date());
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
/*实体类*/
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println(user);
return "success";
}
@RequestMapping("/testParamsRequired")
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success";
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping...");
return "success";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>
<br></br>
<form action="testPojo" method="post">
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testPojo">
</form>
<br></br>
<a href="testReturnData"> Hello testReturnData</a>
<br></br>
<a href="testReturnData2"> Hello testReturnData2</a>
<br></br>
<form action="testReturnData3" method="post"> //新增代码
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testReturnData3">
</form> //新增代码
</body>
</html>
输出:
第四种方式:返回一个Request
修改代码:SpringmvcTest.java
package com.neuedu.springmvc.controller;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.neuedu.springmvc.po.User;
import com.sun.javafx.scene.accessibility.Attribute;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Controller
//@RequestMapping("/test")
/*这是类*/
public class SpringmvcTest { //新增代码
@RequestMapping("/testReturnData4")
public String testReturnData4(User user,HttpServletRequest request){
request.setAttribute("user",user);
return "success";
} //新增代码
@RequestMapping("/testReturnData3")
public String testReturnData3(User user,Model model) {
model.addAttribute("user",user);
return "success";
}
/*向前台传递数据的第二种方式:
2.在springMVC的参数中可以直接使用的有:
HttpServletRequest
HttpServletResponse
HttpSession
Map
Model
*/
@RequestMapping("/testReturnData2")
public String testReturnData2(Map<String,Object> map) {
User user = new User("li", "1234", "female", "1834567@163.com", "15603320164");
map.put("username","zhang2");
map.put("user", user);
return "success";
}
/*向前台传递数据的第一种方式:
1.返回一个ModelAndView*/
@RequestMapping("/testReturnData")
public ModelAndView testReturnData() {
ModelAndView modelAndView= new ModelAndView();
modelAndView.addObject("date", new Date());
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
/*实体类*/
@RequestMapping("/testPojo")
public String testPojo(User user) {
System.out.println(user);
return "success";
}
@RequestMapping("/testParamsRequired")
public String testParamsRequired( @RequestParam (value="username",required=true) String username,Integer age) {
System.out.println("testParamsRequired,username:" + username);
System.out.println("testParamsRequired,age:" + age);
return "success";
}
@RequestMapping("/testParams")
public String testParams(@RequestParam("name") String username,
@RequestParam("pwd") String password) {
System.out.println("username:" + username);
System.out.println("password:" + password);
return "success";
}
@RequestMapping(value= {"/testUrl1","testUrl2"})
public String testUrl() {
System.out.println("testUrl");
return "success";
}
/* method=RequestMethod.POST 限定接收用什么方式的请求*/
@RequestMapping(value="/testMethod",method=RequestMethod.GET)
public String testMethod() {
System.out.print("testMethod...");
return "success";
}
/*既可以用在方法上,也可以用在类上,这是方法*/
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
System.out.println("testRequestMapping...");
return "success";
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello"> Hello Index</a>
<br></br>
<a href="testRequestMapping"> Hello testRequestMapping</a>
<br></br>
<a href="testMethod"> Hello testMethod</a>
<br></br>
<form action="testMethod" method="post">
<input type="submit" value="testMethod">
</form>
<br></br>
<a href="testUrl1"> Hello testUrl1</a>
<br></br>
<a href="testUrl2"> Hello testUrl2</a>
<br></br>
<a href="testParams?name=zhang&pwd=123"> Hello testParams</a>
<br></br>
<a href="testParamsRequired?username=zhang"> Hello testParamsRequired</a>
<br></br>
<form action="testPojo" method="post">
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testPojo">
</form>
<br></br>
<a href="testReturnData"> Hello testReturnData</a>
<br></br>
<a href="testReturnData2"> Hello testReturnData2</a>
<br></br>
<form action="testReturnData4" method="post"> //修改代码
username: <input type="text" name="username"/><br>
password: <input type="text" name="password"/><br>
gender: <input type="radio" name="gengder" value="male"/>男<br><input type="radio" name="gender" value="female"/>女<br>
email: <input type="text" name="email"/><br>
phone: <input type="text" name="phone"/><br>
province: <input type="text" name="address.province"/><br>
city: <input type="text" name="address.city"/><br>
<input type="submit" value="testReturnData4">
</form> //修改代码
</body>
</html>
输出: