mapping.findForward("error")获取的是struts-config.xml配置文件里面<forward name="error">

本文介绍了Struts1.3框架中错误转发(error forward)的使用方法,强调了必须在struts-config.xml配置文件中预先定义错误转发路径,否则在Action中调用mapping.findForward(error)将返回null。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时编写代码都没注意到,感觉挺有意思,因此记录下。

struts1.3中Action里面的 mapping.findForward("error")获取的是struts-config.xml配置文件里面<forward name="error"></forward>

如下:error必须要在struts-config.xml中配置,否则forward = null。

Java代码:

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserForm userForm = (UserForm) form;
		//这个error必须要在struts-config.xml中配置,否则 mapping.findForward("error") 获取的为null
        ActionForward forward = mapping.findForward("error");
		return forward;
	}
}

 

 struts-config.xml配置文件,在对应的action下必须要配置 <forward name="error" ></forward>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans>  
        <form-bean  
            name="userform"  
            type="com.hsp.form.UserForm"/>  
    </form-beans>  
  
    <action-mappings>  
        <action  
            path="/login"  
            type="com.hsp.action.LoginAction"  
            name="userform"  
            >
           <forward name="error" path="/error.jsp"></forward>         
        </action>  
    </action-mappings>  
  
</struts-config>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值