SSH输入错误Action

时间:2023-03-10 05:20:08
SSH输入错误Action

在类型转化、输入验证校验 、文件上传等出错的时候,如Action中某个变量是int,而上传的值是"ABC",此时Action不会执行execute()函数,而是直接返回result name="input",如果没有定义result name="input"跳转的Action,就会报错:

No result defined for action com.xxx.action.XXXXAction and result input

可以自己定义一个Action,遇到此类情况时返回自己定义的信息。

首先定义输入错误Action类:

@SuppressWarnings("serial")
public class InputErrorAction extends ActionSupport { public String execute(){
int status;
Map<String, Object> map = new HashMap<String, Object>();
status = -1001;
map.put("Status", status);
map.put("Desc", "输入错误未通过验证"); // 返回结果
try{
ResUtil.toJson(ServletActionContext.getResponse(), map);
}catch (IOException e){
e.printStackTrace();
} return null;
} }

在applicationContext.xml中为该类定义一个bean:

    <!-- 输入错误 -->
<bean id="inputErrorAction" class="com.xkssh.action.InputErrorAction">
</bean>

在struts中定义一个Action:

        <!-- 输入错误未通过验证 -->
<action name="input_error" class="inputErrorAction">
</action>

为其他Action定义result name="input"时跳转的Action:

        <action name="xkgwc_delete" class="xkgwcDeleteAction">
<result name="success"/>
<result name="input" type="redirectAction">
<param name="actionName">input_error</param>
</result>
</action>

这样,当发生输入错误时,就会返回自己定义的信息:

SSH输入错误Action