一、如何将数据返回给前端界面
没有使用框架之前,我们将请求处理完成的结果(数据)返回给前端界面使用了两种方法,一种是存储在request或是session中,在前端通过el表达式去取出数据,并绑定在界面上,显示给用户。另一种是,利用ajax异步请求,我们将处理结果封装成JSON格式返回给请求的回调函数,然后再通过前端解析JSON数据将数据显示再界面上。同样的SpringMVC框架也有可以使用这两种方法。
1.1 保存在request或是session中
在springmvc框架中同样可以将数据保存在session或是request中,返回给前端界面。
request:作用范围是同一个请求中有效。
session:作用范围是同一次会话,即只要不关闭浏览器,我都可以调用。
//保存在request中
@RequestMapping(value = "/list1")
public String list1(HttpServletRequest request){
Student s = new Student(1,"asda",12,"阿萨的黄金卡",new Date());
request.setAttribute("s",s);
return "list.jsp";
}
//保存在session中
@RequestMapping(value = "/list3")
public String list3(HttpSession session){
Student s = new Student(3,"asdasda",12,"啊实打实",new Date());
session.setAttribute("user",s);
return "list.jsp";
}
除了以上两种方式之外,springmvc提供了一个另一个存储数据的类,model类,默认状态下,model类是和request作用一致,而这个类的好处就是,通用。上面使用的两种方式都是在tomcat服务器中,相当于工程和tomcat绑定了,如果开发使用的不是tomcat服务器,那自然也就不行了,而是用model就不会存在这个问题了。当然model类也可以转换类型,变成session类型的存储方式,就是在controller类上写一行注释@SessionAttributes(value={""})。
//默认类型下存储在model类中
@RequestMapping(value = "/list2")
public String list2(Model model){
Student s = new Student(2,"asda",12,"阿萨的黄金卡",new Date());
model.addAttribute("s",s);
return "list.jsp";
}
以session类型,存储在model中的代码和上面一样,只是要加注释
1.2 以JSON类型返回
在之前是使用JSON格式时,我们借助了Fastjson的jar包来封装我们要返回的数据,使用框架之后,在框架中内置了一个jackson的jar包可以让我们来封装JSON数据。
使用方法:1、导入jar包依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
2、编写controller方法
@ResponseBody,该注释就是声明该方法的返回值要转成JSON格式,框架看到这个注释会自动将我们返回的数据封装成JSON格式。
@Controller
public class JsonController {
@RequestMapping("json01")
@ResponseBody
public List<Student> json01(){
List<Student> list = new ArrayList<Student>();
list.add(new Student(1,"asda",12,"阿萨的黄金卡",new Date()));
list.add(new Student(2,"asda",12,"阿萨的黄金卡",new Date()));
list.add(new Student(3,"asda",12,"阿萨的黄金卡",new Date()));
list.add(new Student(4,"asda",12,"阿萨的黄金卡",new Date()));
int a = 10/0;
return list;
}
}
二、如何重定向
在框架中我们有些controller层的方法,返回的数据是一个页面,在默认的情况下,返回页面使用的是,请求转发,如果我们想使用重定向的话,就需要在返回的页面前面加上redirect:。这样返回的页面就会进行重定向跳转。
@RequestMapping("list5")
public String list5(){
System.out.println("!!!!!!!!!!!!!!!!!");
return "redirect:list.jsp";
}
三、全局异常处理类
全局异常处理类的作用是,当controller中方法在执行时,如果出现异常,就交给异常处理类来处理异常。
使用方法:1、创建异常处理类,在类上面加:@ControllerAdvice注解。
@ControllerAdvice //表示该为类controller的异常处理类
public class MyExceptinHandle {
@ExceptionHandler(value = RuntimeException.class) //当发生RuntimeException就会触发该方法
public String error(){
return "error.jsp";
}
@ExceptionHandler(value = Exception.class) //当发生Exception就会触发该方法
public CommonResult error2(){
return new CommonResult(,"",null);
}
}
2、将异常处理类添加到xml中,让sprngmvc可以扫描到这个包。
四、拦截器
与jdbc中的过滤器类似,都是来过滤拦截,不同的是过滤器是过滤掉某些资源,而拦截器只会拦截请求。
使用步骤: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的配置文件上。
<mvc:interceptors>
<mvc:interceptor>
<!--经过该拦截器的请求-->
<mvc:mapping path="/**"/>
<!--不经过该拦截器的请求-->
<mvc:exclude-mapping path="/list2"/>
<mvc:exclude-mapping path="/list3"/>
<!--bean表示你自定义的拦截器类路径-->
<bean class="com.xrx.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>