Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 如何在try-catch中重定向到我的错误视图?_Java_Spring_Jsp_Try Catch - Fatal编程技术网

Java 如何在try-catch中重定向到我的错误视图?

Java 如何在try-catch中重定向到我的错误视图?,java,spring,jsp,try-catch,Java,Spring,Jsp,Try Catch,我在try-catch中返回错误视图 当我的方法retrunmodelandview时,它工作得很好,但是当我的方法返回字符串时,我如何返回我的视图呢 像这样 @RequestMapping(value="/librairie/supprimerLivre/{isbn}", method = RequestMethod.GET) public String supprimerLivre(@PathVariable("isbn") String isbn, HttpServletReques

我在try-catch中返回错误视图 当我的方法retrunmodelandview时,它工作得很好,但是当我的方法返回字符串时,我如何返回我的视图呢

像这样

   @RequestMapping(value="/librairie/supprimerLivre/{isbn}", method = RequestMethod.GET)
public String supprimerLivre(@PathVariable("isbn") String isbn, HttpServletRequest request){
    try{
      gestPanier = new GestPanier(request);
      //rechercher le livre qui correspond a l'isbn passer en parametre
    LivreAchete livre = gestPanier.getListe().stream().filter(c -> c.getIsbn().equals(isbn)).findFirst().get();

    //supprimer le livre
    gestPanier.supprimer(livre);
    return "redirect:/librairie/afficherPanier";  
    }
    catch(Exception ex){
        return ModelAndView  return new ModelAndView("Error", "model",new ErrorviewModel("/librairie/paiement", ex.getMessage(), ex));
    }
}

我无法返回ModelAndView,因为我的方法有一个字符串返回,但如何重定向到我的视图?

您可以通过使用throws而不是try-catch来解决此问题

@RequestMapping(value="/librairie/supprimerLivre/{isbn}", method = RequestMethod.GET)
public String supprimerLivre(@PathVariable("isbn") String isbn, HttpServletRequest request) 
throws Exception{
    gestPanier = new GestPanier(request);
    LivreAchete livre = gestPanier.getListe().stream().filter(c -> c.getIsbn().equals(isbn)).findFirst().get();

    gestPanier.supprimer(livre);
    return "redirect:/librairie/afficherPanier";
}
然后处理调用该方法的异常

try{
    supprimerLivre(isbn, request); //That's where you call the method
} catch (Exception e) {
    ModelAndView model = new ModelAndView("Error", "model",new ErrorviewModel("/librairie/paiement", ex.getMessage(), ex));
}

您可以尝试以下方法(这是基于控制器的异常处理的Spring方式之一):


如果您想了解更多有关Spring MVC异常处理的信息,请参阅(我的示例基于该网站上描述的其中一种方法)。

使用“重定向”是什么意思?此外,Model和View是否是一个包含错误的“框”?如果是,您可以使用javax.swing.JOptionPanei替换它,意思是返回“redirect:/librarie/afficherPanier”;你对box是什么意思?我不明白如何返回ModelAndView???我的方法返回字符串。。。公共字符串supprimerLivre(@PathVariable(“isbn”)字符串isbn,HttpServletRequest请求)引发异常{您无法返回ModelAndView。您正在使用字符串作为返回值。这里您只是在另一个位置处理异常。如果您想返回ModelAndView,可以将字符串替换为对象,但很容易出错。
    // Name of the function is not important (just an example)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(RuntimeException.class) // Or handle some custom exception of yours
    public ModelAndView supprimerLivreHandler(HttpServletRequest request, Exception ex) {
        return new ModelAndView("Error", "model",new ErrorviewModel("/librairie/paiement", ex.getMessage(), ex));
    }

    @RequestMapping(value="/librairie/supprimerLivre/{isbn}", method = RequestMethod.GET)
    public String supprimerLivre(@PathVariable("isbn") String isbn, HttpServletRequest request){
        try{
            gestPanier = new GestPanier(request);
            //rechercher le livre qui correspond a l'isbn passer en parametre
            LivreAchete livre = gestPanier.getListe().stream().filter(c -> c.getIsbn().equals(isbn)).findFirst().get();

            //supprimer le livre
            gestPanier.supprimer(livre);
            return "redirect:/librairie/afficherPanier";
        }
        catch(Exception ex){
            // When this Exception is thrown, the supprimerLivreHandler function will be called
            throw new RuntimeException(); // Or throw some custom exception of yours
        }
    }