struts2编辑页面验证?

发布于 2024-10-20 15:58:42 字数 231 浏览 1 评论 0原文

我有一个包含用户个人资料的表格,下面我给出了一个按钮(编辑个人资料),用户可以通过该按钮更新他/她的个人资料,当用户单击“编辑个人资料”时,将显示一个新页面,其中包含所有内容用户的信息,它是从数据库中填充的。

我面临的问题是,在此页面中没有进行验证,假设用户删除了他的用户名并尝试更新他的个人资料,它应该显示 * 用户名是必需的 相反,它抛出了一些错误,我填充验证在这里没有发生,因为数据来自数据库,但我不确定,任何人都可以帮助我。

I have a table which contains the profile of the user and below that i have given a button(Edit Profile) through which the user can update his/her profile, when a user click the Edit Profile a new page will be displayed which contains all the information of the user and it is populated from the database.

The problem i am facing is that validation is not happening here in this page, say a user delete his user name and tries to update his profile it should show * User Name is required
instead of that it is throwing some error, i fill validation is not happening here because the data are coming from the database but i am not sure, can anyone help me in this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-10-27 15:58:42

由于您没有发布任何代码,我不确定您正在使用什么验证方法或您尝试过什么。以下是使用 validate() 方法处理表单验证的示例操作。

我最初忘记提及您需要更改表单,以便它提交到“myAction!submit”而不仅仅是“myAction”。

public class MyAction extends ActionSupport {
    /**
     * Your profile, as loaded from the database.
     */
    private Profile profile;

    /**
     * SkipValidation is required so that the validate() method is not invoked
     * prior to the execute() method. Otherwise, all fields would be blank and
     * we would end up showing validation errors on all fields.
     */
    @SkipValidation
    @Override
    public String execute() throws Exception {
        return INPUT;
    }

    @Override
    public void validate() {
        if (StringUtils.isEmpty(profile.getUsername())) {
            addFieldError("profile.username", "User Name is required.");
        }

        // place any additional validations here...
        // you can and should create convenience methods for performing validations.
    }

    public String submit() throws Exception {
        // persist the changes and return an appropriate response
        // e.g., redirect somewhere
        return SUCCESS;
    }
}

Since you didn't post any code, I'm not sure what method of validation you are using or what you have tried. Here's an example action using the validate() method to handle form validation.

I originally forgot to mention that you will need to change for form so that it submits to "myAction!submit" rather than just "myAction".

public class MyAction extends ActionSupport {
    /**
     * Your profile, as loaded from the database.
     */
    private Profile profile;

    /**
     * SkipValidation is required so that the validate() method is not invoked
     * prior to the execute() method. Otherwise, all fields would be blank and
     * we would end up showing validation errors on all fields.
     */
    @SkipValidation
    @Override
    public String execute() throws Exception {
        return INPUT;
    }

    @Override
    public void validate() {
        if (StringUtils.isEmpty(profile.getUsername())) {
            addFieldError("profile.username", "User Name is required.");
        }

        // place any additional validations here...
        // you can and should create convenience methods for performing validations.
    }

    public String submit() throws Exception {
        // persist the changes and return an appropriate response
        // e.g., redirect somewhere
        return SUCCESS;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文