我正在使用spring控制器从表中获取值。
如果某些id的表中没有数据,我在catalina.out文件中无法获得任何异常。 我正在生成一个WAR并放在tomcat7的webapps文件夹中。
我想在没有id的数据时显示错误页面。 现在它进入了默认错误页面。 我需要显示其他错误页面,其中显示“此Id没有记录”。
任何人都可以暗示一下这个吗?
这是班级,
@Controller public class GenerateController extends AbstractController { @Resource private UserService userService; @Resource private LoanService loanService; @Resource private GroupService groupService; protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String groupID = request.getParameter("groupId"); Integer conv = Integer.valueOf(Integer.parseInt(groupID)); Group group = this.loanService.getGroupById(conv.intValue()); GroupLoan groupLoan = this.groupService.getGroupLoanByLoanId( ((Loan)group.getLoans().get(0)).getId().longValue()); LoanDTO loan = this.loanService.getLoanById(groupLoan.getLoanId().longValue()); GroupDTO groupDto = this.groupService.getGroupById(loan.getGroupLoan().getGroupId().longValue()); ModelAndView modelAndView = new ModelAndView(); return modelAndView; } }I am using spring controller to get the values from tables.
If data is not there in table for some id's, I couldn't get any exception in catalina.out file. I am generating a WAR and placed in webapps folder of tomcat7.
I would like to show an error page while getting no datas for id's. Now it went to default error page. I need to show other error page which says "There is no record for this Id" for this.
Can anyone give a hint in this?
Here is class,
@Controller public class GenerateController extends AbstractController { @Resource private UserService userService; @Resource private LoanService loanService; @Resource private GroupService groupService; protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { String groupID = request.getParameter("groupId"); Integer conv = Integer.valueOf(Integer.parseInt(groupID)); Group group = this.loanService.getGroupById(conv.intValue()); GroupLoan groupLoan = this.groupService.getGroupLoanByLoanId( ((Loan)group.getLoans().get(0)).getId().longValue()); LoanDTO loan = this.loanService.getLoanById(groupLoan.getLoanId().longValue()); GroupDTO groupDto = this.groupService.getGroupById(loan.getGroupLoan().getGroupId().longValue()); ModelAndView modelAndView = new ModelAndView(); return modelAndView; } }最满意答案
你可以通过几种方法实现这一目标,
1.)从Controller本身return a view
String message = "There is no record for this Id" +id; return new ModelAndView("myPage", "message", message);myPage可以是你的错误页面。
2.)从Controller ,抛出一些自定义异常,比如GroupNotFoundException 。
有关详细说明,请参阅此处。
There are few ways you can achieve this,
1.) From Controller itself, return a view
String message = "There is no record for this Id" +id; return new ModelAndView("myPage", "message", message);where myPage can be your error page.
2.) From Controller , throw some custom exception, say GroupNotFoundException.
Please refer this for detailed explanation.
更多推荐
发布评论