平时编写代码都没注意到,感觉挺有意思,因此记录下。
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>