SpringMVC响应到JSP页面

目录

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

2.如何使用重定向跳转

3.springmvc返回json数据

3.1 什么时候需要返回json数据

3.2 之前在servlet时如何返回json数据

3.3 springmvc如何返回json数据

3.3.1 添加jackson的依赖包

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

 4.springmvc的全局异常处理类

4.1 如何使用全局异常处理类

4.2 如果是ajax请求返回的应该是一个json数据

 5.springmvc拦截器

5.1 如何使用拦截器:

 6.整体布局展示


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

在使用servlet时,如何把数据保存并可以在网页上获取该数据:

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

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

        pagecontext:作用范围:当前页面有效。

        application:作用范围:同一个应用有效。

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

示例展示:

        数据保存到reques及session的方法都有两种,一种是与Tomcat容器绑定,另一种是

org.springframework.ui.Model包下提供的model方法,在使用时注意model默认是在request中使用,要在@Collection下运用@SessionAttribute方法,将在value下的存储对象所存储的数据变为存储在session中的数据。

@Controller
@SessionAttributes(value = {"stu3","stu4"}) //设置哪些model的key在session范围
public class MyController {
    @RequestMapping("/student")
    public String hello(Student student){
        System.out.println(student);
        return "index.jsp";
    }
    @RequestMapping("/list")
    public String list01(HttpServletRequest httpServletRequest){
        Student student1=new Student("小王",new Date(),1,"郑州");
        httpServletRequest.setAttribute("stu",student1);
        return "eltest.jsp";  //springmvc采用请求转发的跳转。
    }
    @RequestMapping("/list01")
    public String list02(Model model){//model对象可以理解为request对象。凡是在该对象中保存的
//数据,作用范围同一个请求有效。如果使用request,就和tomcat容器绑定了。 建议使用Model.
        Student student2=new Student("小22222",new Date(),1,"郑州");
        model.addAttribute("stu1",student2);
        return "eltest.jsp";
    }
//上面两种方法的作用是一样 都是保存在request范围。
//下面的为保存到session范围
    @RequestMapping("/list02")
    public String list03(HttpSession httpSession){
        Student student3=new Student("哈哈哈哈哈",new Date(),1,"郑州");
        httpSession.setAttribute("stu2",student3);
        return "eltest.jsp";
    }
@RequestMapping("/list03")
    public String list04(Model model){
        Student student4=new Student("小布偶",new Date(),1,"上海");
        Student student5=new Student("Tom",new Date(),0,"北京");
        model.addAttribute("stu3",student4);//默认在request范围
        model.addAttribute("stu4",student5);
        return "eltest.jsp";
    }
}

测试:

${requestScope.stu}
${requestScope.stu1}
${sessionScope.stu2}
${sessionScope.stu3}
${sessionScope.stu4}

2.如何使用重定向跳转

springmvc采用请求转发的跳转。如何变为重定向跳转,只需要在方法的返回字符串的内容时加上redirect:

 @RequestMapping("/list05")
    public String list5(){
        System.out.println("!!!!!!!!!!!!!!!!!");
        return "redirect:list.jsp"; //当springmvc看到返回的字符串中含有redirect:时 它认为你要进行重定向跳转
    }

3.springmvc返回json数据

3.1 什么时候需要返回json数据

        异步请求时,ajax请求时。

3.2 之前在servlet时如何返回json数据

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

3.3 springmvc如何返回json数据

3.3.1 添加jackson的依赖包

 <!--jackson依赖 可以把java对象转换为json对象-->
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.2.2</version>
    </dependency>

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

@Controller
public class MyController01 {
    @RequestMapping("/jsontest")
    @ResponseBody //作用:把java对象转换为json对象
    public List test01(){ //这里返回的类型为java对象。 之前都是返回字符串
        Student stu=new Student("小小",new Date(),0,"商丘");
        List<Student> list=new ArrayList<Student>();
        list.add(stu);
        return list;
    }
}

 上面返回的时间类型的json数据显示的为毫秒,正常情况应该显示时间格式。解决方法:

 4.springmvc的全局异常处理类

全局异常处理类的作用: 当controller发生异常,则有全局异常类来处理并执行相应的处理方法。

4.1 如何使用全局异常处理类

[1] 创建一个异常类: @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";
    }
}

[2] 保证springmvc能够扫描到该类。

        注意要将此处的范围变大,可以使用此方法,还可以直接在后面加逗号,然后再写一个路径。

 4.2 如果是ajax请求返回的应该是一个json数据

 @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public Map error(){
        Map map=new HashMap();
        map.put("code","5000");
        map.put("msg","错误");
        return map;
    }

5.springmvc拦截器

过滤器: 过滤掉某些资源

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

5.1 如何使用拦截器:

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

public class MyIntercepter implements HandlerInterceptor {
//拦截器的处理方法。
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("经过了该拦截器");
        return true;//true:表示拦截器放行 false:不放行
    }
}

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

<!--拦截器的配置-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--mapping:哪些路径需要经过拦截器
               /**: 表示n层路径
               /*:表示一层路径
                -->
            <mvc:mapping path="/**"/>
            <!--exclude-mapping:设置不经过该拦截的路径-->
            <mvc:exclude-mapping path="/list02"/>
            <mvc:exclude-mapping path="/list03"/>
            <!--bean表示你自定义的拦截器类路径-->
            <bean class="com.qy151wd.intercepter.MyIntercepter"/>
        </mvc:interceptor>
    </mvc:interceptors>

6.整体布局展示

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值