Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案
2013-09-02 22:57
429 查看
比如,当我要添加一个信用卡的时候,我需要信用卡的CardType, 这些数据是存在数据库中的,我要先通过action 的一个 create方法,取到信用卡的类型:
<action name="create" method="create" class="example.CreditCardAction"> <result name="success">input.jsp</result>
</action>
input.jsp
...
<s:select name="creditCard.creditCardTypeId" list="creditCardTypeList" listKey="creditCardTypeId"
listValue="ccType" />
....
当提交input.jsp 的时候,Validate 检查没有通过,这时我需要回到input.jsp,此时应该下拉列表框的CreditType应该被保留,只需要在example.CreditCardAction 实现 Preparable接口,并实现prepare 方法,然后在add的 action中加上
<interceptor-ref name="prepare"/>
<interceptor-ref name="defaultStack"/>
就可以了
prepare方法:
<action name="add" method="add" class="example.CreditCardAction">
<interceptor-ref name="prepare"/>
<interceptor-ref name="defaultStack"/>
<result name="input">input.jsp</result>
<result name="success" type="redirect-action">
<param name="namespace">/credit</param>
<param name="actionName">list</param>
</result>
</action>
这样,在验证前将首先调用 prepare方法,即使失败了回到input.jsp页面creditCardType选择框的值仍然存在。
<interceptor-ref name="defaultStack"/> 中的 defaultStack 是我们在struts.xml 中配置的,其中我们注释掉了 <interceptor-ref name="prepare"/> 这样在example.CreditCardAction中的其他Action就不会首先执行prepare方法,只有加上了 <interceptor-ref name="prepare"/> 的才会去首先执行 prepare方法。
<package name="project-default" abstract="true" extends="struts-default">
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<!--
<interceptor-ref name="prepare"/>
-->
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<!--
<interceptor-ref name="checkbox">
<param name="uncheckedValue">no</param>
</interceptor-ref>
-->
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
现在看下PrepareInterceptor拦截器的源代码:
public String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof Preparable) { //判断这个action是否是Preparable类的一个实例,也就是判断这个action有没有implement实现这个Preparable借口
try {
String[] prefixes;
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
}
catch (InvocationTargetException e) {
/*
* The invoked method threw an exception and reflection wrapped it
* in an InvocationTargetException.
* If possible re-throw the original exception so that normal
* exception handling will take place.
*/
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
/*
* The cause is not an Exception or Error (must be Throwable) so
* just re-throw the wrapped exception.
*/
throw e;
}
}
if (alwaysInvokePrepare) {
((Preparable) action).prepare();
}
}
return invocation.invoke();
}
<action name="create" method="create" class="example.CreditCardAction"> <result name="success">input.jsp</result>
</action>
input.jsp
...
<s:select name="creditCard.creditCardTypeId" list="creditCardTypeList" listKey="creditCardTypeId"
listValue="ccType" />
....
当提交input.jsp 的时候,Validate 检查没有通过,这时我需要回到input.jsp,此时应该下拉列表框的CreditType应该被保留,只需要在example.CreditCardAction 实现 Preparable接口,并实现prepare 方法,然后在add的 action中加上
<interceptor-ref name="prepare"/>
<interceptor-ref name="defaultStack"/>
就可以了
prepare方法:
<action name="add" method="add" class="example.CreditCardAction">
<interceptor-ref name="prepare"/>
<interceptor-ref name="defaultStack"/>
<result name="input">input.jsp</result>
<result name="success" type="redirect-action">
<param name="namespace">/credit</param>
<param name="actionName">list</param>
</result>
</action>
这样,在验证前将首先调用 prepare方法,即使失败了回到input.jsp页面creditCardType选择框的值仍然存在。
<interceptor-ref name="defaultStack"/> 中的 defaultStack 是我们在struts.xml 中配置的,其中我们注释掉了 <interceptor-ref name="prepare"/> 这样在example.CreditCardAction中的其他Action就不会首先执行prepare方法,只有加上了 <interceptor-ref name="prepare"/> 的才会去首先执行 prepare方法。
<package name="project-default" abstract="true" extends="struts-default">
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<!--
<interceptor-ref name="prepare"/>
-->
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<!--
<interceptor-ref name="checkbox">
<param name="uncheckedValue">no</param>
</interceptor-ref>
-->
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
</package>
现在看下PrepareInterceptor拦截器的源代码:
public String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof Preparable) { //判断这个action是否是Preparable类的一个实例,也就是判断这个action有没有implement实现这个Preparable借口
try {
String[] prefixes;
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
}
catch (InvocationTargetException e) {
/*
* The invoked method threw an exception and reflection wrapped it
* in an InvocationTargetException.
* If possible re-throw the original exception so that normal
* exception handling will take place.
*/
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
/*
* The cause is not an Exception or Error (must be Throwable) so
* just re-throw the wrapped exception.
*/
throw e;
}
}
if (alwaysInvokePrepare) {
((Preparable) action).prepare();
}
}
return invocation.invoke();
}
相关文章推荐
- Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案
- Struts 2, 值在验证失败回到原来页面的时候会丢失的解决方案
- Struts2 值在验证失败回到原来页面的时候会丢失的解决方案
- Struts2 值在验证失败回到原来页面的时候会丢失的解决方案
- Struts的action中用了addFieldError,页面第一次提交验证失败后,再次提交找不到action
- 当用户退出点击浏览器后退仍可回到原来页面的解决方案
- struts上传文件,验证失败表单数据丢失的原因
- 切换页面的时候出现一个页面闪一下,回到原来的界面
- struts上传文件,验证失败表单数据丢失的原因
- oracle导入提示“IMP-00010:不是有效的导出文件,头部验证失败”的解决方案
- Struts中用Filter实现每个页面的登录验证
- cookies提前过期,及验证视图状态 MAC 失败解决方案
- 注册信息的时候出现错误 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey>
- Struts 中 Validator页面验证
- IE访问页面的时候,受限制的解决方案。
- “无法在web服务器上启用调试。调试失败,因为没有启用Windows身份验证。”的解决方案
- 关于用户退出,点击浏览器后退仍可回到原来页面解决二
- 安装微软首发“OGA”(正版增值计划)验证失败后的有效解决方案
- 防止用户注销后后退到原来页面的javascript与jsp解决方案
- 铁通、长宽网络支付时“签名失败”问题分析及解决方案 [88222001]验证签名异常:FAIL[20131101100002-142]