栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC:处理器方法的返回值(动力)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC:处理器方法的返回值(动力)

ModelAanView:即传递数据,又跳转页面

返回String,只进行页面的跳转工作

void:不能表示数据,也不能表示视图,在处理ajax的时候可以使用void返回值 

 

 web.xml:





    
        myweb
        org.springframework.web.servlet.DispatcherServlet


        
            
            contextConfigLocation
            
            classpath:springmvc.xml
        


        1

    


    
        myweb
        *.do
    

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter

        
        
            encoding
            utf-8
        
        
        
            forceRequestEncoding
            true
        
        
        
            forceReponsetEncoding
            true
        
    
    
        characterEncodingFilter
       
    


    
        
        
        
        
    


    

 

 MyController:

package com.bjpowernode.controller;

import com.bjpowernode.vo.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class MyController {


    //处理器方法返回String
    @RequestMapping(value ="/returnString-view.do")   //value里面可以写多个名字进行访问
    public String doReturnView(HttpServletRequest request,String name,Integer age){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnView,name="+name+"  age="+age);//这里先不调用,简化处理

        //如果还想存储数据,可以手工去做,我们可以使用request作用域手工添加到request作用域
        request.setAttribute("myname",name);
        request.setAttribute("myage",age);

        //show:逻辑视图名称,项目中配置了视图解析器
        //框架对视图执行forword转发操作
        return "show";
        //还可以返回完整路径,不用视图解析器,在配置文件中把视图解析器注释
        //return "/WEB-INF/view/show.jsp";

    }

    //处理器方法返回void ,响应ajax请求
    //手工实现ajax,json数据:代码存在重复:java对象转换为json;通过HttpServletResponse输出json数据
    @RequestMapping(value ="/returnVoid-ajax.do")   //value里面可以写多个名字进行访问
    public void doReturnVoidAjax(HttpServletResponse response,String name,Integer age) throws IOException { //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnVoidAjax,name="+name+"  age="+age);//这里先不调用,简化处理

        //处理ajax,使用json做数据的格式
        //Service调用完了,使用Student表示处理结果
        Student student=new Student();
        student.setName(name);
        student.setAge(age);

        String json="";
        //把结果的对象转为json格式的数据
        if(student!=null){
            ObjectMapper om=new ObjectMapper();
           json= om.writeValueAsString(student);
            System.out.println("Student转换为json=="+json);
        }

        //输出数据,响应ajax的请求
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw=response.getWriter();
        pw.println(json);
        pw.flush();
        pw.close();
    }


    //处理器方法返回一个Student,通过框架转为json,响应ajax请求
    //@ResponseBody注解作用:把处理器方法返回的对象转为json后,通过HttpServletResponse输出给浏览器。
    @RequestMapping(value ="/returnStudentJson.do")
    @ResponseBody
    public Student doStudentJsonObject(String name,Integer age){
        //调用Service,获取请求结果数据,Student对象表示结果数据
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);
        return student;//会被框架转换为json
    }
}

index.jsp:更换访问的url:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    
    
    


  处理器方法返回String表示视图名称

  
姓名:
年龄:


 返回json对象数组:返回List集合

MyController:

package com.bjpowernode.controller;

import com.bjpowernode.vo.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MyController {


    //处理器方法返回String
    @RequestMapping(value ="/returnString-view.do")   //value里面可以写多个名字进行访问
    public String doReturnView(HttpServletRequest request,String name,Integer age){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnView,name="+name+"  age="+age);//这里先不调用,简化处理

        //如果还想存储数据,可以手工去做,我们可以使用request作用域手工添加到request作用域
        request.setAttribute("myname",name);
        request.setAttribute("myage",age);

        //show:逻辑视图名称,项目中配置了视图解析器
        //框架对视图执行forword转发操作
        return "show";
        //还可以返回完整路径,不用视图解析器,在配置文件中把视图解析器注释
        //return "/WEB-INF/view/show.jsp";

    }

    //处理器方法返回void ,响应ajax请求
    //手工实现ajax,json数据:代码存在重复:java对象转换为json;通过HttpServletResponse输出json数据
    @RequestMapping(value ="/returnVoid-ajax.do")   //value里面可以写多个名字进行访问
    public void doReturnVoidAjax(HttpServletResponse response,String name,Integer age) throws IOException { //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnVoidAjax,name="+name+"  age="+age);//这里先不调用,简化处理

        //处理ajax,使用json做数据的格式
        //Service调用完了,使用Student表示处理结果
        Student student=new Student();
        student.setName(name);
        student.setAge(age);

        String json="";
        //把结果的对象转为json格式的数据
        if(student!=null){
            ObjectMapper om=new ObjectMapper();
           json= om.writeValueAsString(student);
            System.out.println("Student转换为json=="+json);
        }

        //输出数据,响应ajax的请求
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw=response.getWriter();
        pw.println(json);
        pw.flush();
        pw.close();
    }


    //处理器方法返回一个Student,通过框架转为json,响应ajax请求
    //@ResponseBody注解作用:把处理器方法返回的对象转为json后,通过HttpServletResponse输出给浏览器。
    
    @RequestMapping(value ="/returnStudentJson.do")
    @ResponseBody
    public Student doStudentJsonObject(String name,Integer age){
        //调用Service,获取请求结果数据,Student对象表示结果数据
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);
        return student;//会被框架转换为json
    }


    
     
    @RequestMapping(value ="/returnStudentJsonArray.do")
    @ResponseBody
    public List doStudentJsonObjectArray(String name, Integer age){

        List list=new ArrayList<>();
        //调用Service,获取请求结果数据,Student对象表示结果数据
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);

        list.add(student);

        student=new Student();
        student.setName("张三");
        student.setAge(28);
        list.add(student);

        return list;//会被框架转换为json数组
    }
}

index.jsp:更换url

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    
    
    


  处理器方法返回String表示视图名称

  
姓名:
年龄:


响应ajax,处理器返回的是String

My Controller:

package com.bjpowernode.controller;

import com.bjpowernode.vo.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MyController {


    //处理器方法返回String
    @RequestMapping(value ="/returnString-view.do")   //value里面可以写多个名字进行访问
    public String doReturnView(HttpServletRequest request,String name,Integer age){ //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnView,name="+name+"  age="+age);//这里先不调用,简化处理

        //如果还想存储数据,可以手工去做,我们可以使用request作用域手工添加到request作用域
        request.setAttribute("myname",name);
        request.setAttribute("myage",age);

        //show:逻辑视图名称,项目中配置了视图解析器
        //框架对视图执行forword转发操作
        return "show";
        //还可以返回完整路径,不用视图解析器,在配置文件中把视图解析器注释
        //return "/WEB-INF/view/show.jsp";

    }

    //处理器方法返回void ,响应ajax请求
    //手工实现ajax,json数据:代码存在重复:java对象转换为json;通过HttpServletResponse输出json数据
    @RequestMapping(value ="/returnVoid-ajax.do")   //value里面可以写多个名字进行访问
    public void doReturnVoidAjax(HttpServletResponse response,String name,Integer age) throws IOException { //doSome相当于doGet()---调用Service请求处理
        System.out.println("doReturnVoidAjax,name="+name+"  age="+age);//这里先不调用,简化处理

        //处理ajax,使用json做数据的格式
        //Service调用完了,使用Student表示处理结果
        Student student=new Student();
        student.setName(name);
        student.setAge(age);

        String json="";
        
        //把结果的对象转为json格式的数据
        if(student!=null){
            ObjectMapper om=new ObjectMapper();
           json= om.writeValueAsString(student);
            System.out.println("Student转换为json=="+json);
        }

        //输出数据,响应ajax的请求
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw=response.getWriter();
        pw.println(json);
        pw.flush();
        pw.close();
    }


    //处理器方法返回一个Student,通过框架转为json,响应ajax请求
    //@ResponseBody注解作用:把处理器方法返回的对象转为json后,通过HttpServletResponse输出给浏览器。
    
    @RequestMapping(value ="/returnStudentJson.do")
    @ResponseBody
    public Student doStudentJsonObject(String name,Integer age){
        //调用Service,获取请求结果数据,Student对象表示结果数据
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);
        return student;//会被框架转换为json
    }


    
     
    @RequestMapping(value ="/returnStudentJsonArray.do")
    @ResponseBody
    public List doStudentJsonObjectArray(String name, Integer age){

        List list=new ArrayList<>();
        //调用Service,获取请求结果数据,Student对象表示结果数据
        Student student=new Student();
        student.setName("李四");
        student.setAge(20);

        list.add(student);

        student=new Student();
        student.setName("张三");
        student.setAge(28);
        list.add(student);

        return list;//会被框架转换为json数组
    }

    
    
    @RequestMapping(value = "/returnStringData.do",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String doStringData(String  name,Integer age){
        return "Hellow SpringMVC 返回对象,表示数据";
    }

}

index.jsp:更换url和dataType

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/7
  Time: 17:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    
    
    


  处理器方法返回String表示视图名称

  
姓名:
年龄:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号