[英]Calling Jsp page from controller in Spring MVC
我是Spring MVC的新手。 我有一個異常的contoller,在捕獲異常之后我想重定向到error.jsp頁面並顯示異常消息(ex.getMessage())。 我不想使用Spring的異常處理程序,而是必須以編程方式重定向到error.jsp。
@RequestMapping(value = "http/exception", method = RequestMethod.GET)
public String exception2()
{
try{
generateException();
}catch(IndexOutOfBoundsException e){
handleException();
}
return "";
}
private void generateException(){
throw new IndexOutOfBoundsException();
}
private void handleException(){
// what should go here to redirect the page to error.jsp
}
我不確定你為什么要從你的方法中返回String
; Spring MVC中的標准是使用@RequestMapping
注釋的方法返回ModelAndView
,即使您沒有使用Spring的異常處理程序。 據我所知,你不能將你的客戶端發送到error.jsp而不返回某個ModelAndView
。 如果您需要幫助理解Spring控制器的基本思想,我發現本教程展示了如何在Spring MVC中創建一個簡單的“Hello World”應用程序,它有一個簡單的Spring控制器的好例子。
如果您希望您的方法在遇到異常時返回錯誤頁面,否則返回正常頁面,我會這樣做:
@RequestMapping(value = "http/exception", method = RequestMethod.GET)
public ModelAndView exception2()
{
ModelAndView modelAndview;
try {
generateException();
modelAndView = new ModelAndView("success.jsp");
} catch(IndexOutOfBoundsException e) {
modelAndView = handleException();
}
return modelAndView;
}
private void generateException(){
throw new IndexOutOfBoundsException();
}
private ModelAndView handleException(){
return new ModelAndView("error.jsp");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.