SpringMVC带数据给jsp页面

第一种方式:通过request域

java:用HttpServletRequest request传值

        request.setAttribute("user", username);
        request.setAttribute("password", password);存值
    @RequestMapping(value="test1")
    public String test1(String username,String password,HttpServletRequest request) {
        
        request.setAttribute("user", username);
        request.setAttribute("password", password);
        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            return "success";
        }
        else { //登录失败
            System.out.println(username);
            return "fail";
        }
        
    }

取值:在跳转到的success.jsp页面中写: ${requestScope.user }

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() +request.getContextPath()+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath %>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>new jsp</title>
</head>
<body>
    <h1>登录成功</h1>
    <!-- 通过request域保存数据 -->
    ${requestScope.user }
</body>
</html>

 

第二种方式:通过Model带数据

 java中:用Model,

model.addAttribute("user",username);存值
    @RequestMapping(value="test1")
    public String test1(String username,String passwor,Model model) {
        
        model.addAttribute("user",username);
        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            return "success";
        }
        else { //登录失败
            System.out.println(username);
            return "fail";
        }
        
    }

取值和第一种方法一样,在跳转后的jsp页面:${requestScope.user }

Model就是把HTTPServletRequest封装了。

 

第三种方式:通过Map集合带数据

 数据其实也是保存到request域中

    @RequestMapping(value="test1")
    public String test1(String username,String password,Map<String, Object> map) {
        
        map.put("user1", username);
        if("admin".equals(username)&&"123".equals(password)) { //登录成功
            return "success";
        }
        else { //登录失败
            System.out.println(username);
            return "fail";
        }
        
    }

 

 

统一异常处理(*):

当一个页面出错了,要设置成跳转到一个专门的页面。

    @RequestMapping(value="test1")
    public Integer test1() {
        int i = 10 / 0;
        return i; //这里会出错

    }
    
    //异常处理注解,括号里代表以后是Exception的错误,统统自动找这个方法
    @ExceptionHandler(Exception.class)
    public String excepHand(Exception ex) {
        ex.printStackTrace(); //把错误信息在控制台打印出来,前台的页面给用户看,项目快上线的时候都存到log4j中 
        return "error"; //跳到error.jsp页面
    }
    
    //精确匹配:如果错误是ArithmeticException的,那么直接找这个方法,就不找上面那个方法了
    @ExceptionHandler(ArithmeticException.class)
    public String excepHand2(Exception ex) {
        ex.printStackTrace(); //把错误信息在控制台打印出来,前台的页面给用户看,项目快上线的时候都存到log4j中 
        return "error"; //跳到error.jsp页面
    }

但是在这个类中写,只适用于当前类,要是每个类都写一个太费资源。

因此创建一个包:cn.java.controller.exception,创建class:GlobalException

package cn.java.controller.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice  //全局的,当前工程下任何错误异常都会找到这里来
public class GlobalException {
    
    @ExceptionHandler(Exception.class)
    public String excepHand(Exception ex) {
        ex.printStackTrace(); //把错误信息在控制台打印出来,前台的页面给用户看,项目快上线的时候都存到log4j中 
        return "error"; //跳到error.jsp页面
    }
}

有了这个,项目下任何错误都会进这个类。但是这个类必须在springmvc.xml能扫描到的范围内。

 

posted on 2018-06-02 12:06  lonske  阅读(187)  评论(0编辑  收藏  举报