springMVC—接受参数以及返回参数

    springMVC将请求发送到controller时需要接受参数,在controller中处理完业务逻辑以后也会将参数再次转发给其他页面,在strtus2中我们会使用到模型驱动,在springMVC中页提供多多种灵活了接受和返回参数的方式。下面就总结一下。

controller接受参数

    在这里可以直接使用url方法获取参数比如:http://localhost:8080/springmvc-2/toPerson1.do?name=123

1.参数列表中可以直接定义request,response,session

@RequestMapping(value="/toPerson.do")
public String toPerson(HttpServletRequest request){
	String name = request.getParameter("name");
	String[] favs = request.getParameterValues("fav");
	System.out.println(name);
	for(int str : fav){
		System.out.println(str);
	}
	return"index";
}

2.在参数列表中直接定义要接收的参数和参数的数据类型

@RequestMapping(value="/toPerson1.do")
public String toPerson1(String name, int[] fav){
	System.out.println("name: "+name +"birthday:"+birthday);
	for(int str : fav){
		System.out.println(str);
	}
	return"index";
}


3.实体类的接受

    在参数列表中直接定义要接受的实体类,请求中传递的参数名要和实体类中的set方法后的字符串匹配,否则无法注入。只要有请求到达就会按照参数列表中的实体类来创建对象。

    springmvc中的controller是单例的但是不会带来并发问题,因为所有的参数的接受都是在参数列表中,并不是成员变量,每次请求,传递的参数都是一个新的实例,不会产生并发问题。

@RequestMapping(value="/toPerson4.do")
public String toPerson4(Person person){
	System.out.println(person.toString());
	System.out.println(user.toString());
	System.out.println(name);
	System.out.println(bir);
	return"index";
}  

controller返回参数

1.使用map返回到页面,把数据返回到页面上

@RequestMapping(value="/toPerson5.do")
public ModelAndView toPerson5() throws Exception{
	Person person = new Person();
	person.setId(1);
	person.setName("lisi");
	person.setBir(newSimpleDateFormat("yyyy-MM-dd").parse("1985-04-22"));
	Map<String,Object>map=new HashMap<String,Object>();
	map.put("p", person);
	return new ModelAndView("toPerson",map);
}

 返回值类型是String,在参数列表中直接来定义一个Map,这个map不是用来接受参数的,而是用来把参数写到页面上去的

@RequestMapping(value="/toPerson6.do")
public ModelAndView toPerson6(Map<String,Object> map) throws Exception{
	Person person = new Person();
	person.setId(1);
	person.setName("lisi");
	person.setBir(newSimpleDateFormat("yyyy-MM-dd").parse("1985-04-22"));
	map=newHashMap<String,Object>();
	map.put("p",person);
	returnnew ModelAndView("toPerson",map);
}      

2.使用spring 提供的model返回到页面

 返回值类型是String代表viewName, 参数列表使用Model用来把数据写到页面上

@RequestMapping(value="/toPerson7.do")
public String toPerson7(Model model) throws Exception{
	Personperson = new Person();
	person.setId(1);
	person.setName("lisi");
	person.setBir(newSimpleDateFormat("yyyy-MM-dd").parse("1985-04-22"));
	model.addAttribute("p",person);
	return"toPerson";
}  

toPerson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
//http://localhost:8080/springmvc-1/
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<!-- 在所有请求前边加上basePath   http://localhost:8080/springmvc-1/-->
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<h1>${p.id }</h1>
  	<h1>${p.name }</h1>
  	<h1><fmt:formatDate value="${p.bir }" pattern="yyyy-MM-dd"/></h1>
  </body>
</html>

    总之,在接受参数的方式中springMVC可以定义requet去接受,可以直接定义参数列表,在接受实体对象的时候也可以定义参数列表接受数据。在返回数据的时候可以使用Map返回数据,也可以使用springMVC提供的Mode返回数据。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值