使用Spring MVC 实现页面提交数据的获取与返回
使用@RequestParam(required=true) ,表示页面请求必须传入参数
package com.hqh.student.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//配置为控制器
@Controller
public class FirstController {
//配置请求映射策略
@RequestMapping(value={"/hello"})
public String hello(@RequestParam(required=true) int id) {
System.out.println("FirstController.hello()");
return "hello";
}
}
如果请求的URL没有传入参数,会报错,提示没有传递需要的参数值
正确的访问方式:
http://localhost:8080/spring_mvc_01/hello?id=1
配置默认参数
package com.hqh.student.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//配置为控制器
@Controller
public class FirstController {
//配置请求映射策略
@RequestMapping(value={"/hello"})
public String hello(@RequestParam(required=true,defaultValue="100") int id) {
System.out.println(id);
return "hello";
}
}
如果没有传递参数,使用默认值,而不会报错
http://localhost:8080/spring_mvc_01/hello
结果:100
如果传入了参数,则使用传递的参数,默认值无效
http://localhost:8080/spring_mvc_01/hello?id=9
结果:9
定制页面传入的参数名
package com.hqh.student.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//配置为控制器
@Controller
public class FirstController {
//配置请求映射策略
@RequestMapping(value={"/hello"})
public String hello(@RequestParam(value="userId") int id) {
System.out.println(id);
return "hello";
}
}
指定页面传递的参数名称为userId,只有当参数名为userId时,才能正确访问
http://localhost:8080/spring_mvc_01/hello?userId=1
结果:1
向客户端返回数据
通过MAP传递数据,只要在方法的参数中定义一个Map,然后往Map中put值,页面通过key就能取到
太NB了!
@RequestMapping(value={"/say"})
public String say(String name,Map<String,Object> retMap) {
System.out.println("name="+name);
retMap.put("say", "spring mvc is very good!");
return "say";
}
WEB-INF/jsp/say.js
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${say}</h1>
</body>
</html>
请求:http://localhost:8080/spring_mvc_01/say?name=zhangsan
后台获取到的数据:name=zhangsan
页面获取到的数据:spring mvc is very good!
使用Model进行数据传递(推荐)
@RequestMapping(value="/shout")
public String shout(Model model) {
//没有key,key为Object.即,如果是String,则key为string
model.addAttribute("struts2 is bad!");
//通过key,value进行传递
model.addAttribute("spring_mvc", "so easy!");
return "shout";
}
WEB-INF/jsp/shout.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${string}</h1>
<h1>${spring_mvc}</h1>
</body>
</html>
URL:http://localhost:8080/spring_mvc_01/shout
返回给页面的结果:
struts2 is bad!
so easy!
获取request对象,session对象等
//获取request对象
@RequestMapping(value={"/execute"})
public String execute(HttpServletRequest request) {
//spring会自动将request对象传入
HttpSession session = request.getSession();
String uri = request.getRequestURI();
System.out.println(uri);
return "shout";
}