在類型轉化、輸入驗證校驗 、文件上傳等出錯的時候,如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>
這樣,當發生輸入錯誤時,就會返回自己定義的信息:
