SSM-SPRINGMVC-24:SPRINGMVC Exception Advanced Custom exception

Source: Internet
Author: User

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

Custom exception, everyone, yes, nothing is inherited exception class operations, very simple, I do not much, but in the XML configuration file has a different operation, I will focus on the list to

Case Start:

1. Custom Exception class: Userageexception

Package cn.dawn.day17selfexceptionresolver.userexception; /*  */Publicclass  userageexception extends Exception    {public  userageexception () {        super ();    }      Public userageexception (String message) {        super (message);}    }

2. Custom Exception class: Usernameexception

Package cn.dawn.day17selfexceptionresolver.userexception; /*  */Publicclass  usernameexception extends Exception    {public  usernameexception () {        super ();    }      Public usernameexception (String message) {        super (message);}    }

3. Define the processor and processing method:

Package Cn.dawn.day17selfexceptionresolver;import Cn.dawn.day17selfexceptionresolver.userexception.userageexception;import Cn.dawn.day17selfexceptionresolver.userexception.usernameexception;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by Dawn on 2018/3/28.*/@Controller Public classZdyexceptioncontroller {@RequestMapping ("/zidingyiexception")     Publicstring Zidingyiexception (String Username,integer userage) throws Exception {if(!username.equals ("Admin")){            Throw NewUsernameexception ("login name is incorrect"); }        if(userage< -){            Throw NewUserageexception ("underage, go away"); }        return "Success"; }}

In its own XML large configuration: Simplemappingexceptionresolver with a exceptionmappings

<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc="Http://www.springframework.org/schema/mvc"Xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><!--package Scanner--<context:component-scanBase-package="Cn.dawn.day16exceptionhigh"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/day16/"></property> <property name="suffix"Value=". JSP"></property> </bean> <beanclass="Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--Default exception jumps page-<property name="Defaulterrorview"Value="Error"></property> <!--This can inject an exception object, just like the catch parameter (Exception ex)--<property name="Exceptionattribute"Value="ex"></property><!--Custom exceptions--<property name= "exceptionmappings" > <props> <prop key= "Cn.dawn.day16exceptionhigh.userexception.UserageException" >age</prop> <prop key= "cn.dawn.d Ay16exceptionhigh.userexception.UsernameException ">name</prop> </props> </property> </bean></beans>

Red is the top priority.

4. Change the central scheduler context configuration location for Web. XML to the new one above

5.jsp page

5.1age.jsp

<%@ page  pageencoding="UTF-8" contenttype="text/html;charset= UTF-8" language="java" iselignored="false "  %>server was cut by monkeys, siege lion in repair, also killed a program ape worship </body></ Html>

5.2name.jsp

<%@ page  pageencoding="UTF-8" contenttype="text/html;charset= UTF-8" language="java" iselignored="false "  %>server was cut by monkeys, siege lion in repair, also killed a program ape worship </body></ Html>

5.3login.jsp

<%@ page pageencoding="UTF-8"Contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>"${pagecontext.request.contextpath}/zidingyiexception"Method="Post">User name:<input name="username">Age:<input name="Userage"> <input type="Submit"Value="Login"/></form></body>

5.4success.jsp

<%@ page language="java" pageencoding="utf-8" iselignored ="false" %>"image/1.jpg  ">--%>

5.5error.jsp

<%@ page  pageencoding="UTF-8" contenttype="text/html;charset= UTF-8" language="java" iselignored="false"   %>server was cut by monkeys, siege lion in repair, also killed a program ape worship </body></ Html>

6. Launch Tomcat Access login.jsp

Ssm-springmvc-24:springmvc Exception Advanced Custom exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.