SpringMVC使用ResponseEntity

  1. sping环境搭建

步骤略,此处简单叙述环境搭建的配置文件。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1"> 
    
    <description>Spring MVC环境搭建</description>
    <!-- 在web.xml中context-param标签并不是必须存在的,
          如果不存在,则默认是contextConfigLocation,
          而参数value则默认是/WEB-INF/applicationContext.xml。-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring*.xml</param-value>
    </context-param>
    
    <!-- 在web.xml中,如果是配置spring,则必须要有listener标签。  -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring mvc下的配置文件的位置和名称 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>   
    
</web-app>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd 
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">
 <!-- 开启注解 -->
    <mvc:annotation-driven />
        <!-- 配置自动扫描的包 -->
        <context:component-scan base-package="com.sxx.springmvc.controller"></context:component-scan>

        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- jsp页面的存放位置 -->
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <!-- 文件后缀名 -->
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>
  1. 代码示例
    前端代码:
<%@ 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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我的第一个页面</title>
</head>
<body>
<a href="helloworld">hello world</a>
<form action="${pageContext.request.contextPath}/profile" method="post">
Name: <input id="name" name="name" type="text"/>
Email: <input id="email" name="email" type="text"/>
<button type="submit">submit</button>
</form>

<input id="responseEntityTest" type="button" value="request" onclick="responseEntity()">
<script>
function responseEntity(){
    $.post("${pageContext.request.contextPath}/responseEntity",{},function(data){
        alert(data.message);
    });
}
</script>

</body>
</html>

controller:

 @RequestMapping("/responseEntity")
    public ResponseEntity<Map<String,Object>> responseEntity(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("message", "Hello Wrold");
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }
  1. 结果
    alert出“hello world“


    res.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容