首页 > 其他 > 详细

SSH输入错误Action

时间:2016-05-28 12:58:17      阅读:210      评论:0      收藏:0      [点我收藏+]

在类型转化、输入验证校验 、文件上传等出错的时候,如Action中某个变量是int,而上传的值是"ABC",此时Action不会执行execute()函数,而是直接返回result name="input",如果没有定义result name="input"跳转的Action,就会报错误。可以自己定义一个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

原文:http://www.cnblogs.com/mstk/p/5537122.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!