eclipse学习(第二章:初识ssh)——16.Struts2异常处理

前言

本文是参考自以下的网站做的实践和笔记记录https://www.w3cschool.cn/struts_2/struts_exception_handling.html

创建项目

1、初始化项目以及jar包拉取

在这里插入图片描述

2、创建ExceptionHandlingAction类

在这里手动制造一个错误,在第十行,null是不能直接调用equals方法的,它能作为括号内的内容,但是不能放在前面。

package com.czx.exception;

import com.opensymphony.xwork2.ActionSupport;

public class ExceptionHandlingAction extends ActionSupport {
	
	@Override
	public String execute() throws Exception {
		String a = null;
		if(a.equals("111")) {
		}
		return "success";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private String name;
	
	
}

3、创建struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="loginAll" extends="struts-default">
		<action name="login" class="com.czx.exception.ExceptionHandlingAction" method="execute">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

4、修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ssh_learn_exceptionHandling</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

5、创建前端页面

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<s:form action="login" method="post">
		名称:<input type="text" name="name"/><br/>
		<input type="submit" value="提交"/>	
	</s:form>
</body>
</html>

成功会到这里,success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
成功进入,名称为<s:property value="name"/>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
进入失败了,请联系管理员
</body>
</html>

6、测试

访问http://localhost:8080/ssh_learn_exceptionHandling/
在这里插入图片描述
输入任意内容后点击提交会发现,页面变成了这样子
这里显示空指针异常,然后第10行出现了问题,这里也有说明。
在这里插入图片描述

如果出现了异常,那么怎么抓取到呢

在上面的基础之上进行处理就行,修改struts.xml如下,这里分别写了两种异常抓取,一种是局部的,一种是全局的,都可以使用。
全局异常处理,记住了这里必须放在package包里面的最前面,不然会报错,你想想看,按顺序执行的话,你全局异常怎么能比单个操作还要慢加载呢

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
	<constant name="struts.devMode" value="true"></constant>
	<package name="loginAll" extends="struts-default">
		<!-- 	这里是全局异常处理,记住了这里必须放在最前面,不然会报错,你想想看,按顺序执行的话,你全局异常怎么能比单个操作还要慢加载呢 -->
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping>
		</global-exception-mappings>
	
		<action name="login" class="com.czx.exception.ExceptionHandlingAction" method="execute">
<!-- 			这里是局部异常处理,在单个action中有效 -->
<!-- 			<exception-mapping result="error" exception="java.lang.NullPointerException"></exception-mapping> -->
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
		
	</package>
</struts>

再次访问http://localhost:8080/ssh_learn_exceptionHandling/
在这里插入图片描述
随便填写一个内容然后提交会进入到如下界面
在这里插入图片描述

项目地址

项目地址
这个仓库里面的ssh_learn_exceptionHandling
https://gitee.com/mrchen13427566118/ssh_learn.git

如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值