Springmvc学习-Springmvc05-return

本文介绍SpringMVC中如何使用Model、Map、ModelMap传递数据到视图,设置响应编码格式,并封装JSON对象返回给前端。探讨了直接在Controller拼装JSON字符串的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springmvc05-return

1. 承载信息的对象
  • Model、Map<String, Object>、ModelMap
package com.caorui.handlers;

import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {

	@RequestMapping("/hello")  //承载信息的对象
	public String hello(String username, int age, Model model, Map<String, Object> map, ModelMap modelMap ) { 
		System.out.println(username + " " + age);
		model.addAttribute("username", username);
		map.put("age", age);
		modelMap.addAttribute("gender", "female");
		return "welcome";
	}
	
}

<%@ 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>
欢迎页面!+${username}+${age} + ${gender}
</body>
</html>

2. 设置响应编码格式、封装json对象返回给success
  • 设置响应编码格式
    @RequestMapping(value = “/hello”, produces = “text/html;charset=utf-8”) //响应编码格式
  • 封装json对象返回给success(data)
    @ResponseBody //放在方法上, 把返回的值封装成对象返回给success
package com.caorui.handlers;



import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.caorui.pojo.Star;

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {
	@RequestMapping(value = "/hello", produces = "text/html;charset=utf-8") //响应编码格式 
	@ResponseBody //把返回的值封装成对象返回给success
	public String hello() { 
		return "china:瓷器";
	}
}


  • 直接在controller里拼装json字符串,在页面用eval()方法转换成json对象
package com.caorui.handlers;



import java.io.IOException;


import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc")//该注解起了限定范围的作用
public class MyController {
	@RequestMapping(value = "/hello", produces = "text/html;charset=utf-8") //响应编码格式 
	public void hello(HttpServletResponse response) throws IOException { 
		String json = "{\"name\":\"weilong\",\"flavor\":\"hot\"}";
		response.getWriter().println(json);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值