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

SpringMVC详解第二天

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

SpringMVC详解第二天

目录

一.如何把controller数据返回到网页并回显

1.数据回显

2.如何使用重定向跳转

3springmvc返回json数据

(1)什么时候需要返回json数据

(2)之前在servlet时如何返回json数据呢  

(3)springmvc如何返回json数据

3.springmvc的全局异常处理类

4.springmvc拦截器

       (1)创建一个类,并实现HandlerInterceptor

(2) 把该类注册到springmvc配置文件上  


一.如何把controller数据返回到网页并回显

1.数据回显

在学习servlet时我们使用request和session向网页传递数据:

request: 作用范围: 同一个请求内有效。setAttribute(key,value)

session:作用范围: 同一个会话有效,只要会话不关闭会一直有效。setAttribute(key,value)

网页如何获取保存的数据呢:可以使用EL表达式。${scope.key}

在SpringMVC框架中我们也可以使用request或session,还有我们框架提供的内置函数Model

package com.wt.controller;

import com.wt.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
public class HelloController02 {


    @RequestMapping("json02")
    public String json02(HttpServletRequest request){
        Student student = new Student(1,"山涛",23,"修武",new Date());
        //可以保存到request中,同一个请求
        request.setAttribute("s",student);
        return "hello01.jsp";//转发

    }

    //如果你使用request,你和tomcat容器绑定了。 建议使用Model
    @RequestMapping("json03")
    public String json03(Model model){
        Student student = new Student(3,"向秀",15,"山西",new Date());
        //保存到model中,同一个请求 和request是一样
        model.addAttribute("s2",student);
        return "hello01.jsp";
    }

    //保存到session范围
    @RequestMapping("json04")
    public String json04(HttpSession session){
        Student student = new Student(4, "王戎", 18, "开封", new Date());
        session.setAttribute("s3",student);
        return "hello01.jsp";    //转发
    }
    @RequestMapping("json05")
    public String json05(HttpSession session){
        Student student = new Student(4, "王戎", 18, "开封", new Date());
        Student student1 = new Student(5, "阮籍", 28, "焦作", new Date());
        session.setAttribute("s4",student);
        session.setAttribute("s5",student1);
        return "hello01.jsp";    //转发
    }
    //重定向
    @RequestMapping("json06")
    public String json06(){
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!");
        return "redirect:hello01.jsp";
    }
}

2.如何使用重定向跳转

重定向(Redirect)就是通过各种方法将各种网络请求重新定个方向转到其它位置(如:网页重定向、域名的重定向、路由选择的变化也是对数据报文经由路径的一种重定向

//重定向
    @RequestMapping("json06")
    public String json06(){
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!");
        return "redirect:hello01.jsp";
    } 

3springmvc返回json数据

(1)什么时候需要返回json数据

异步请求时,ajax请求时

(2)之前在servlet时如何返回json数据呢  

借助了Fastjson---手动把java对象转换为json格式的数据,并使用out.print(json)输出json数据。 关闭out对象。 

(3)springmvc如何返回json数据

        a.添加内置的jackson-databind的jar包   


  
    com.fasterxml.jackson.core
    jackson-databind
    2.13.2.1
  

        b. 在controller返回的数据类型变成javabean对象  

//    转为json格式传输数据
    @RequestMapping(value = "json01")
    @ResponseBody
    public List json01(){
        List list = new ArrayList();
        list.add(new Student(1,"山涛",23,"修武",new Date()));
        list.add(new Student(2,"嵇康",22,"许昌",new Date()));
        list.add(new Student(3,"向秀",15,"山西",new Date()));
        list.add(new Student(4,"王戎",18,"开封",new Date()));
//        request.setAttribute("list",list);
        return list;
    }

c.访问该路径

不同浏览器显示的样式可能不同,下图为火狐显示情况:

3.springmvc的全局异常处理类

(1)如何使用全局异常处理类

        a.创建一个异常类: @ControllerAdvice注解

 第一种方法:       

@ControllerAdvice //表示该为类controller的异常处理类
public class MyExceptinHandle {


     @ExceptionHandler(value = RuntimeException.class) //当发生RuntimeException就会触发该方法
     public String error(){
         return "error.jsp";
     }

    @ExceptionHandler(value = Exception.class) //当发生Exception就会触发该方法
    public String error2(){
        return "error2.jsp";
    }
}

//第二种方法
//        定义一个CommentResult实体类
@ControllerAdvice
public class MyHandler {
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public CommentResult erro(){
        System.out.println("进入了异常处理类");
        return new CommentResult(5000,"程序出现异常",null);
    }
}

b. 保证springmvc能够扫描到该类  

4.springmvc拦截器

过滤器: 过滤掉某些资源,

拦截器只会拦截controller层的资源路径

如何使用拦截器:

(1)创建一个类,并实现HandlerInterceptor

public class MyInterceptor implements HandlerInterceptor {

    //拦截器的处理方法。
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("经过了该拦截器");

        return true;//true:表示拦截器放行 false:不放行
    }
}

(2) 把该类注册到springmvc配置文件上  

    
        
            
            
            
            
            
            
            
        
    

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

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

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