zoukankan      html  css  js  c++  java
  • SpringMVC中的返回值问题之一返回void

    返回值为void

    此方法需要引入一个json依赖

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
    </dependency>

    web.xml文件

    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
    <display-name>Archetype Created Web Application</display-name>


    <!--编码过滤器-->
    <filter>
    <filter-name>CharactorEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>CharactorEncoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--配置前端控制器-->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--初始化参数-->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc06Annotion.xml</param-value>
    </init-param>

    <!--意思:Tomact启动,就将servlet创建好放入内存中了-->
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>



    public class Student {
    private String name;
    private Integer age;

    public String getName() {return name;}
    public void setName(String name) {
    this.name = name;
    }

    public Integer getAge() {
    return age;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    }


    @Controller
    public class ThridAnnotion {
    /**
    * 返回void json
    * @param response
    * @throws IOException
    */
    @RequestMapping("/json")
    public void Jack(HttpServletResponse response) throws IOException {
    List<Student> list=new ArrayList<Student>();
    Student student=new Student();
    student.setName("李四");
    student.setAge(20);

    Student student1=new Student();
    student1.setName("张三");
    student1.setAge(22);

    list.add(student);
    list.add(student1);

    String json = JSON.toJSONString(list);
    //将json转发到浏览器
    response.getWriter().write(json);
    }

    }
    
    

    springmvc06Annotion.xml文件

    <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="cn.sjl.day03Annotion"></context:component-scan>
    <!--注解驱动-->
    <mvc:annotation-driven/>
    </beans>

    访问方式:







  • 相关阅读:
    HQL语句中类的别名语法以及作用?
    C#面向对象
    c#异步编程一
    c#接口
    c#Socket通信基本使用
    c#FTP基本使用
    c#XML的基本使用
    c#装箱与拆箱
    c#数组与集合
    c#中for与foreach的使用
  • 原文地址:https://www.cnblogs.com/sujulin/p/7771618.html
Copyright © 2011-2022 走看看