52°

springMVC 异常

springMVC  异常

 

0.依赖(不只是本次案例所需)

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
&lt;groupId&gt;com.gxeom&lt;/groupId&gt;
&lt;artifactId&gt;bookshop&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

<dependencies>

<!--1.servlet-api jsp-api 2个jar包-->

<!--作用: 1. Springmvc 当中要用servlet对象 request session 2. Springmvc web.xml配置 dispatcherServlet 继承httpServlet 3.Springmvc 的注解 --> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency>

<!--2.数据库 3个jar包 -->

&lt;!--    mysql的连接驱动  版本要和自己的数据库版本相匹配  --&gt;
&lt;!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;mysql&lt;/groupId&gt;
    &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;version&gt;8.0.19&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--    连接池的依赖--&gt;
&lt;!-- https://mvnrepository.com/artifact/com.alibaba/druid --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
    &lt;artifactId&gt;druid&lt;/artifactId&gt;
    &lt;version&gt;1.1.10&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--    我为了使用JdbcTemplate 所以我们要导入spring-jdbc--&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

&lt;!--3.spring核心   3个jar包(SpEL)不必须导入 --&gt;

&lt;!--    spring  核心依赖 beans core context SpEL(不是必须导入)--&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-context --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-core --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

&lt;!--4. Spring-web+ Spring-mvc+json   3个jar包--&gt;

&lt;!--    要和web相结合 spring-web--&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-web --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--    Spring mvc --&gt;
&lt;!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--    springmvc配套的第三方json依赖--&gt;
&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
    &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
    &lt;version&gt;2.9.8&lt;/version&gt;
&lt;/dependency&gt;


&lt;!--5.aop+aspectj  3个jar包--&gt;

&lt;!--    AOP  ASPECTJ--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
    &lt;version&gt;5.2.5.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--aop切点--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
    &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
    &lt;version&gt;1.6.11&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
    &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
    &lt;version&gt;1.6.11&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--动态代理--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;cglib&lt;/groupId&gt;
    &lt;artifactId&gt;cglib&lt;/artifactId&gt;
    &lt;version&gt;2.1&lt;/version&gt;
&lt;/dependency&gt;



&lt;!-- 6.jstl标签    2个jar包
作用: 页面上去掉 scriptlet(脚本) 页面上获值并输出--&gt;


&lt;!--jstl+standard--&gt;
&lt;!-- https://mvnrepository.com/artifact/javax.servlet/jstl --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
    &lt;artifactId&gt;jstl&lt;/artifactId&gt;
    &lt;version&gt;1.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/taglibs/standard --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;taglibs&lt;/groupId&gt;
    &lt;artifactId&gt;standard&lt;/artifactId&gt;
    &lt;version&gt;1.1.2&lt;/version&gt;
&lt;/dependency&gt;


&lt;!--7.图片上传  2个jar包--&gt;
&lt;!--字节流+图片--&gt;
&lt;!-- https://mvnrepository.com/artifact/commons-io/commons-io --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;commons-io&lt;/groupId&gt;
    &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
    &lt;version&gt;2.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;
    &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;
    &lt;version&gt;1.4&lt;/version&gt;
&lt;/dependency&gt;

</dependencies>

</project>

 

 

1.xml配置(WEB-INF下)

 
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
&lt;!--转发控制器--&gt;
&lt;servlet&gt;
    &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;

<!-- 配置classpath contextConfigLocation 默认classpath WEB-INF--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring-MVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- <error-page>--> <!-- <error-code>404</error-code>--> <!-- <location>/fristex.do</location>--> <!-- </error-page>-->

&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
    &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

<!-- <servlet-mapping>--> <!-- <servlet-name>default</servlet-name>--> <!-- <url-pattern>.js</url-pattern>--> <!-- <url-pattern>.css</url-pattern>--> <!-- <url-pattern>.map</url-pattern>--> <!-- <url-pattern>.jpg</url-pattern>--> <!-- <url-pattern>*.png</url-pattern>--> <!-- </servlet-mapping>-->

</web-app>

 
 

 2.spring-mvc配置

 

<?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
">
<!--    <mvc:resources mapping="/js/**" location="js/"></mvc:resources>-->

<!-- mvc驱动--> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="com.tjetc.*"></context:component-scan> <bean id="sysExceptionResolver" class="com.tjetc.exeception.SysExetionResolve"></bean>

 

3.SysExeption类

package com.tjetc.exeception;

public class SysExeption extends Exception{

private String message;

public SysExeption(String message) {
    this.message = message;
}

@Override
public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

 

4.SysExetionResolve

 

package com.tjetc.exeception;

import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class SysExetionResolve implements HandlerExceptionResolver {

@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
    SysExeption e = null;
    if ( ex instanceof SysExeption){
        e= (SysExeption)ex;
    }else {
        e=new SysExeption("系统正在维护。。");
    }
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("errorMsg",e.getMessage());
    modelAndView.setViewName("error.jsp");
    return modelAndView;
}

}

 

5.FristController

 

package com.tjetc.controller;

import com.tjetc.exeception.SysExeption; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;

@Controller public class FristController { @RequestMapping("fristex.do") public String fristEX() throws SysExeption { try { System.out.println(1/0); }catch (Exception e){ e.printStackTrace(); throw new SysExeption("查询出错了。。。"); } return "success.jsp"; } }

 

6.error.jsp

<%--
  Created by IntelliJ IDEA.
  User: sun
  Date: 2020/6/8
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${errorMsg}
</body>
</html>

 

本文转载自博客园,原文链接:https://www.cnblogs.com/chaoesha/p/13068723.html

全部评论: 0

    我有话说: