zoukankan      html  css  js  c++  java
  • SpringMVC (八)SpringMVC返回值类型

    SpringMVC的返回值类型有MedelAndView,String,void,Object数值型,集合类型等等

    前两种我们之前写案例的时候一直在用,现在看一下返回值是void

    第一种:返回viod

    返回值是void的话,就要结合ajax来写

    准备一个用户类

    package demo09Return;
    
    /**
     * Created by mycom on 2018/3/26.
     */
    public class UserInfo {
        private String username;
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }

    和控制器类

    package demo09Return;
    
    import com.alibaba.fastjson.JSON;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by mycom on 2018/3/26.
     */
    @Controller
    public class ReturnController {
    
        @RequestMapping("/one")
        public void doFirst(HttpServletResponse response) throws IOException {
            List<UserInfo> list=new ArrayList<UserInfo>();
            UserInfo user=new UserInfo();
            user.setUsername("");
            user.setPassword("1225");
            list.add(user);
            String data = JSON.toJSONString(list);
            response.getWriter().write(data);
    
        }
    }

    在页面上

    <%--
      Created by IntelliJ IDEA.
      User: mycom
      Date: 2018/3/26
      Time: 17:13
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min(1).js"></script>
    <script type="text/javascript">
        $(function () {
            $("input").click(function () {
                $.ajax({
                    url:"/one",
                    type:"POST",
                    success:function (data) {
                        $.each(eval("("+data+")"),function (i,dom) {
                            alert(dom.username);
                        })
                    }
                })
            })
        })
    </script>
    <head>
        <title>Title</title>
    </head>
    <body>
    <input type="button" value="按钮">
    </body>
    </html>

    在配置文件中

    <?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"
           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/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="demo09Return"></context:component-scan>
    
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    
    </beans>

    注意不要忘记修改web.xml中的classpath的值

     第二种:返回Object

    引入jar

    <!--返回Object类型的jar-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.1</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.5.1</version>
            </dependency>
     //返回int
        @RequestMapping("/first")
        @ResponseBody
        public Object doFirst(){
            return 1;
        }
    
        //返回String
        @RequestMapping(value = "/second",produces = "text/html;charset=UTF-8")
        @ResponseBody
        public Object doSecond(){
            return "明天会更好";
        }
    
        //返回对象
        @RequestMapping(value = "/third")
        @ResponseBody
        public Object doThird(){
            UserInfo user=new UserInfo();
            user.setUsername("");
            user.setPassword("123456");
            return user;
        }
    
        //返回集合
        @RequestMapping(value = "/four")
        @ResponseBody
        public Object doFour(String name){
            List<UserInfo> list=new ArrayList<UserInfo>();
            UserInfo user=new UserInfo();
            user.setUsername("");
            user.setPassword("1225");
            UserInfo user2=new UserInfo();
            user2.setUsername(name);
            user2.setPassword("123456");
            list.add(user);
            list.add(user2);
            String data = JSON.toJSONString(list);
           return list;
        }
    
    
        //返回map
        @RequestMapping(value = "/five")
        @ResponseBody
        public Object doFive(){
            Map<String,UserInfo> map=new HashMap<String, UserInfo>();
            UserInfo user=new UserInfo();
            user.setUsername("");
            user.setPassword("1225");
            UserInfo user2=new UserInfo();
            user2.setUsername("");
            user2.setPassword("123456");
            map.put(user.getUsername(),user);
            map.put(user2.getUsername(),user2);
            return map;
        }

    配置文件中:

    <?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"
           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/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="demo09Return"></context:component-scan>
    
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
        <!--注解驱动-->
        <mvc:annotation-driven/>
    
    
    </beans>

    测试页面:

    <%--
      Created by IntelliJ IDEA.
      User: mycom
      Date: 2018/3/26
      Time: 17:13
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min(1).js"></script>
    <script type="text/javascript">
        /*$(function () {
            $("input").click(function () {
                $.ajax({
                    url:"/four",
                    type:"POST",
                    success:function (data) {
                        $.each(function (i,dom) {
                            alert(dom.username);
                        })
                    }
                })
            })
        })*/
    
        $(function () {
            $("input").click(function () {
                $.ajax({
                    url:"/five",
                    type:"POST",
                    success:function (data) {
                        $.each(function (i,dom) {
                            alert(dom.username);
                        })
                    }
                })
            })
        })
    </script>
    <head>
        <title>Title</title>
    </head>
    <body>
    <input type="button" value="按钮">
    </body>
    </html>
  • 相关阅读:
    TCP源码—连接建立
    TCP系列02—连接管理—1、三次握手与四次挥手
    TCP系列01—概述及协议头格式
    ubuntu软件管理apt与dpkg
    318. Maximum Product of Word Lengths
    317. Shortest Distance from All Buildings
    316. Remove Duplicate Letters
    315. Count of Smaller Numbers After Self
    314. Binary Tree Vertical Order Traversal
    313. Super Ugly Number
  • 原文地址:https://www.cnblogs.com/my-123/p/8654603.html
Copyright © 2011-2022 走看看