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>

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值