SPRINGMVC (11) System exception processor boost version

Source: Internet
Author: User

Now I want to make it to my custom page based on the user name and age input format is correct

Define a user name exception and an age exception

Name exception

Package Demo13exceptionhigh; /*  */Publicclass  nameexception extends Exception    {//  In this class to inherit the exception class, in the implementation of the class two methods public      nameexception () {        super ();    }      Public nameexception (String message) {        super (message);}    }

Age abnormality

Package Demo13exceptionhigh; /*  */Publicclass  ageexception extends Exception    {public ageexception () {        super ();    }      Public ageexception (String message) {        super (message);}    }

Defining an exception Controller

Package Demo13exceptionhigh;

Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by mycom on 2018/3/30.
*/
@Controller
public class Exceptioncontroller {

@RequestMapping ("/first")
public string Dofirst (String Name,int age) throws Exception {
return different pages depending on the exception
if (!name.equals ("admin")) {
throw new Nameexception ("User name exception");
}
This anomaly is more than 60 years old.
if (age>60) {
throw new Ageexception ("Age does not match");
}
Return "Success";
}
}

In the Springmvc.xml configuration file

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--Package Scanner--<context:component-scanBase-package="Demo13exceptionhigh"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/error/"></property> <property name="suffix"Value=". JSP"></property> </bean> <!--exception processor--<beanclass="Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="Defaulterrorview"Value="Error"></property> <property name="Exceptionattribute"Value="ex"></property> <!--exception boost add something--<property name="exceptionmappings"> <props> <!--configuration exception type key is the exception type value is the page name to jump to <prop key="nameexception">nameException</prop> <prop key="ageexception">ageException</prop> </props> </property> </bean> <!--note-driven <mvc:annotation-driven/></beans>

Two pages

nameexception.jsp

<%--Created by IntelliJ. User:mycom Date:2018/3/ -Time : A: * to change ThisTemplate Use File | Settings |File Templates.--%><%@ page contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>name does not match ${ex.message}</body>

ageexception.jsp

<%--Created by IntelliJ. User:mycom Date:2018/3/ -Time : A: * to change ThisTemplate Use File | Settings |File Templates.--%><%@ page contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>age does not match ${ex.message}</body>

You can test it at the end.

SPRINGMVC (11) System exception processor boost version

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.