Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更新列表错误,使其显示在JSP页面上?_Java_Jsp_Servlets - Fatal编程技术网

Java 如何更新列表错误,使其显示在JSP页面上?

Java 如何更新列表错误,使其显示在JSP页面上?,java,jsp,servlets,Java,Jsp,Servlets,我有点纠结于在JSP页面上显示的列表中有单独的错误。评论区是我需要帮助的地方。我试着想清楚这一点,但我无法思考为if或else块放置什么 // Validate Parameters List<String> errors = validate(nationalID, lastName, firstName, dateOfBirth); if (errors.isEmpty()){ // Input v

我有点纠结于在JSP页面上显示的列表中有单独的错误。评论区是我需要帮助的地方。我试着想清楚这一点,但我无法思考为if或else块放置什么

// Validate Parameters
            List<String> errors = validate(nationalID, lastName, firstName, dateOfBirth);

            if (errors.isEmpty()){
                // Input valid

                // Commit Edit

                // Forward to Display

            } else {
                // Input NOT valid

                // Attach errors

                // Attach old input

                // Forward to Edit.jsp
            }



private List<String> validate(String nationalID, String lastName, String firstName,
        String dateOfBirth) {

    List<String> errors = new ArrayList<>();

    if (nationalID == null || nationalID.length() != 5 || !nationalID.matches("[A-Za-z]{2}[0-9]{3}")) {
        errors.add("Please check that your National ID is in the correct format");
    }

    if (lastName.isEmpty()) {
        errors.add("Last Name cannot be empty");

    }

    if (firstName.isEmpty()) {
        errors.add("First Name cannot be empty");

    }

    try {
        LocalDate.parse(dateOfBirth);
    } catch (DateTimeParseException e) {
        errors.add("Date of Birth is not formatted correctly");

    }

    return errors;
}
//验证参数
列表错误=验证(国有、姓氏、姓氏、出生日期);
if(errors.isEmpty()){
//输入有效
//提交编辑
//向前显示
}否则{
//输入无效
//附加错误
//附加旧输入
//转发到Edit.jsp
}
私有列表验证(字符串国有化、字符串姓氏、字符串姓氏、,
字符串(出生日期){
列表错误=新建ArrayList();
如果(nationalID==null | | nationalID.length()!=5 | |!nationalID.matches(“[A-Za-z]{2}[0-9]{3}”)){
错误。添加(“请检查您的国家ID格式是否正确”);
}
if(lastName.isEmpty()){
错误。添加(“姓氏不能为空”);
}
if(firstName.isEmpty()){
错误。添加(“名字不能为空”);
}
试一试{
parse(出生日期);
}捕获(DateTimeParse异常){
错误。添加(“出生日期格式不正确”);
}
返回错误;
}
RequestDispatcher=null;
HttpSession session=request.getSession();
if(errors.isEmpty()){
session.setAttribute(“status”,errors.isEmpty());
dispatcher=getServletContext().getRequestDispatcher(“/Home.jsp”);
//编写JDBC或任何东西并更新db值
}否则{
dispatcher=getServletContext().getRequestDispatcher(“/Login.jsp”);
session.setAttribute(“status”,errors.isEmpty());
ArrayList old_data=新ArrayList();
旧数据。添加(国有化);
旧数据。添加(姓氏);
旧数据。添加(名字);
旧数据。添加(出生日期);
session.setAttribute(“旧_数据”,旧_数据);
}
试一试{
转发(请求、响应);
}捕获(ServletException e){
e、 printStackTrace();
}
RequestDispatcher dispatcher = null;
HttpSession session = request.getSession();

if (errors.isEmpty()){
            session.setAttribute("status", errors.isEmpty());
            dispatcher = getServletContext().getRequestDispatcher("/Home.jsp");
            // Write your JDBC or whatever and update your db values

        } else {

            dispatcher = getServletContext().getRequestDispatcher("/Login.jsp");
            session.setAttribute("status", errors.isEmpty());
            ArrayList<String> old_data = new ArrayList<String>();
            old_data.add(nationalID);
            old_data.add(lastName);
            old_data.add(firstName);
            old_data.add(dateOfBirth);
            session.setAttribute("old_data", old_data);


        }

        try {
               dispatcher.forward(request,response);
        } catch (ServletException e) {
               e.printStackTrace();
        }