struts2 验证和准备方法相关 [英] struts2 validation and prepare method related

查看:29
本文介绍了struts2 验证和准备方法相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个 custprofileview 动作,它显示一个 JSP 页面,其中包含客户的所有详细信息,并且在我的 JSP 中,所有字段都像我的

In have a custprofileview Action which show a JSP page with all details of customer and in my JSP all fields are like my

<s:textfield name="custprofileVO.email" value="%{custprofileVO.email}" />
<s:textfield name="custprofileVO.phone" value="%{custprofileVO.phone}" />

依此类推,有一个提交按钮调用 Action updatecustprofile.

and do so on, and there is a submit button that calls Action updatecustprofile.

updatecustprofile 动作中,我没有直接映射属性,而是有一个成员变量 private CustprofileVO custprofileVO;带有 setter 和 getter.

In updatecustprofile Action, instead of directly mapping properties I have a member variable private CustprofileVO custprofileVO; with setter and getter.

CustprofileVO 类中,我有诸如 emailphone 之类的字段以及所有其他字段及其 setter 和 getter 方法.

In CustprofileVO Class I have fields like email, phone and all other fields with their setters and getters methods.

问题是:在 updatecustprofile 操作中我正在实现 Prepareable 接口,并在实现 prepare()方法我有 custprofileVO.setDefaultID("Select"); 并设置了另外 4 个字段,但是当我通过单击提交按钮运行程序时,我在第一行得到 NPEcustprofileVO.setDefaultID("Select");

Problem is: in updatecustprofile Action I am implementing Prepareable Interface, and in implementation of prepare() method I have custprofileVO.setDefaultID("Select"); and setting 4 more fields but when I run the program by clicking on submit button I get NPE in the very first line that is custprofileVO.setDefaultID("Select");

看起来框架没有实例化CustprofileVO custprofileVO.如果我手动实例化 custprofileVO 就在字段设置的上方(通过执行 custprofileVO = new CustprofileVO() 然后它可以工作.问题是-理想的struts2框架应该给我它没有做的实例,想了解原因.

It looks like the framework is not instantiating CustprofileVO custprofileVO. If I manually instantiate custprofileVO just above setting of the field (by doing custprofileVO = new CustprofileVO() then it works. Problem is- ideally struts2 framework should give me instance which it is not doing, want to understand the reason.

此外,如果我在 prepare 方法中手动设置 custprofileVO 它可以工作,但我也使用 XML 应用了验证,其中我的字段名称是 custprofileVO.email ,它的验证然后 custprofileVO.phone 验证.

Further if I manually set custprofileVO in prepare method it works but I have also applied validation using XML where my field name is custprofileVO.email ,its validation then custprofileVO.phone its validation.

当我尝试通过单击提交按钮进行验证时,验证运行但在屏幕上我看到所有字段的消息,因为所有文本框中的数据都被清空了.

When I try to validate on click of submit button validation runs but on screen I see messages for all fields as data in all textboxes gets blanked out.

为什么数据会被删除?

推荐答案

你不应该自己实例化一个来自 JSP 的对象.

You should not instantiate by yourself an object that is coming from JSP.

要在 prepare() 方法中获取它,由 Prepare Interceptor before Param Interceptor 运行, 你需要使用一个特殊的栈:<代码>paramsPrepareParamsStack

To get it in the prepare() method, run by the Prepare Interceptor before the Param Interceptor, you need to use a special stack: paramsPrepareParamsStack

 <!-- An example of the paramsPrepareParams trick. This stack
             is exactly the same as the defaultStack, except that it
             includes one extra interceptor before the prepare interceptor:
             the params interceptor.

             This is useful for when you wish to apply parameters directly
             to an object that you wish to load externally (such as a DAO
             or database or service layer), but can't load that object
             until at least the ID parameter has been loaded. By loading
             the parameters twice, you can retrieve the object in the
             prepare() method, allowing the second params interceptor to
             apply the values on the object. -->
        <interceptor-stack name="paramsPrepareParamsStack">
            <interceptor-ref name="exception"/>
            <interceptor-ref name="alias"/>
            <interceptor-ref name="i18n"/>
            <interceptor-ref name="checkbox"/>
            <interceptor-ref name="multiselect"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</param>
            </interceptor-ref>
            <interceptor-ref name="servletConfig"/>
            <interceptor-ref name="prepare"/>
            <interceptor-ref name="chain"/>
            <interceptor-ref name="modelDriven"/>
            <interceptor-ref name="fileUpload"/>
            <interceptor-ref name="staticParams"/>
            <interceptor-ref name="actionMappingParams"/>
            <interceptor-ref name="params">
                <param name="excludeParams">^class\..*,^dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,^parameters\..*,^action:.*,^method:.*</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>

这篇关于struts2 验证和准备方法相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆