Springmvc学习-Springmvc05-return

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

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);
	}
}

在Spring MVC中,Servlet配置文件通常指的是`web.xml`和Spring MVC的配置文件(如`spring-mvc.xml`)。以下是配置方法和示例: ### `web.xml`配置 在`web.xml`中,需要配置`DispatcherServlet`,它是Spring MVC的核心Servlet,负责接收所有的HTTP请求并将其分发给相应的处理器。 ```xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定Spring MVC配置文件的位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 将所有请求映射到DispatcherServlet --> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` ### Spring MVC配置文件(`spring-mvc.xml`)配置 在Spring MVC配置文件中,需要配置一些必要的组件,如视图解析器、处理器映射器等。 ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启Spring MVC注解支持 --> <mvc:annotation-driven/> <!-- 扫描控制器所在的包 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans> ``` ### 控制器示例 以下是一个简单的控制器示例: ```java package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") @ResponseBody public String hello() { return "Hello, Spring MVC!"; } } ``` ### 总结 通过以上配置,`web.xml`将所有请求映射到`DispatcherServlet`,`DispatcherServlet`根据`contextConfigLocation`指定的配置文件加载Spring MVC的配置,配置文件中开启了注解支持、扫描控制器所在的包并配置了视图解析器,最后通过控制器处理请求并返回响应。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值