如何在用户提交内容时验证表单(使用 Struts2)

发布于 2024-09-19 17:41:35 字数 1842 浏览 5 评论 0原文

我正在开发一个非常简单的 Struts2 应用程序,我遇到了这个奇怪的(并且很容易解决,我确信)问题:

当我直接访问登录网址时,它显示登录表单(用户名和密码文本字段)并带有这些错误:

Name field is required
Password is required

该字段是空的,因为这是用户第一次进入它,所以我想告诉Struts2验证系统不要验证,除非用户提交带有用户名和密码的登录表单。我确信我忽略或忘记了一些事情。

Login.java 操作代码:

package actions.access;

import actions.base.BaseAction;
import data.Webuser;

public class Login extends BaseAction {

    private Webuser user;

    public String execute() {
        if (user != null) {
            session = this.getSession();
            if (session.get("user-logged") == null ) {
                session.put("user-logged", user.getId());
            }
        }
        return SUCCESS;
    }

    public void validate() {
        if (user != null) {
            Webuser realUser = services.getWebuserByNickname(user.getNickname());
            if (realUser.getPass().equals(user.getPass())) {
                user.setId(realUser.getId());
            } else {
                addFieldError("user.nickname", "Nickname or password are wrong");
            }
        }
    }

    public Webuser getUser() {
        return user;
    }


    public void setUser(Webuser user) {
        this.user = user;
    }

}

Login-validation.xml 代码:

<field name="user.nickname">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message>Login name is required</message>
    </field-validator>
</field>

<field name="user.pass">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message>Password is required</message>
    </field-validator>
</field>

任何形式的帮助将不胜感激。 提前致谢。

I'm developing a very simple Struts2 aplication, and I get this strange (and easy to resolve, I'm sure) problem:

When I access directoly to login url, it shows login form (username and password text fields) with these errors:

Name field is required
Password is required

The field are empty, because it's the first time the user get into it, so I'd like to tell to Struts2 validation system not to validate unless user submit the login form with username and password. I'm sure I'm overlooking or forgeting something.

Login.java action code:

package actions.access;

import actions.base.BaseAction;
import data.Webuser;

public class Login extends BaseAction {

    private Webuser user;

    public String execute() {
        if (user != null) {
            session = this.getSession();
            if (session.get("user-logged") == null ) {
                session.put("user-logged", user.getId());
            }
        }
        return SUCCESS;
    }

    public void validate() {
        if (user != null) {
            Webuser realUser = services.getWebuserByNickname(user.getNickname());
            if (realUser.getPass().equals(user.getPass())) {
                user.setId(realUser.getId());
            } else {
                addFieldError("user.nickname", "Nickname or password are wrong");
            }
        }
    }

    public Webuser getUser() {
        return user;
    }


    public void setUser(Webuser user) {
        this.user = user;
    }

}

Login-validation.xml code:

<field name="user.nickname">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message>Login name is required</message>
    </field-validator>
</field>

<field name="user.pass">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message>Password is required</message>
    </field-validator>
</field>

Any kind of help will be appreciated.
Thanks in advance.

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

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

发布评论

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

评论(4

好菇凉咱不稀罕他 2024-09-26 17:41:36

您正在寻找的是使用相同的操作来显示表单,然后(提交后)处理表单。

public class LoginAction {
    @SkipValidation
    public String execute() throws Exception {
        return INPUT; // shows the form
    }

    public void validate() {
        // do your validations here...
    }

    public String submit() throws Exception {
        // process the form
        // redirect somewhere
    }
}

您的表单应提交到“login!submit”(即动态方法调用,它调用您的 submit() 方法)。您不需要为此使用两个操作。

What you are looking for is to use the same action to show a form and then (after submit) process the form.

public class LoginAction {
    @SkipValidation
    public String execute() throws Exception {
        return INPUT; // shows the form
    }

    public void validate() {
        // do your validations here...
    }

    public String submit() throws Exception {
        // process the form
        // redirect somewhere
    }
}

Your form should submit to "login!submit" (that's Dynamic Method Invocation, which invokes your submit() method). You don't need to use two actions for this.

千柳 2024-09-26 17:41:36

你需要两个动作。

  1. 您的登录操作重定向到您的login.jsp。

  2. 处理登录过程的 LoginSubmit 操作。

对于第二个,您可以定义验证。

you need two actions.

  1. Your Login Action which redirect to your login.jsp.

  2. A LoginSubmit Action which handle the login proccess.

for the second one you can define your validation.

东风软 2024-09-26 17:41:36

您需要一个重定向到 login.jsp 的操作和另一个处理登录过程和验证的操作。
在第一个操作中不要写任何内容,只需设置 result=success。

public class IndexAction extends ActionSupport
{


   public String execute() throws Exception
   {
      return SUCCESS;
   }
}

Struts.xml 中的条目如下所示:

<action name="login" class="com.test.web.LoginAction">
<result name="input">/pages/login.jsp</result>
    <result name="success" >/pages/welcome.jsp</result>    
</action>
<action name="index" class="com.test.web.IndexAction">
    <result name="success">/pages/login.jsp</result>
</action>

You require one action which redirect to login.jsp and another action which handles Login process and validation.
Inside first action do not write anything just set result=success.

public class IndexAction extends ActionSupport
{


   public String execute() throws Exception
   {
      return SUCCESS;
   }
}

Entries in Struts.xml are like follows:

<action name="login" class="com.test.web.LoginAction">
<result name="input">/pages/login.jsp</result>
    <result name="success" >/pages/welcome.jsp</result>    
</action>
<action name="index" class="com.test.web.IndexAction">
    <result name="success">/pages/login.jsp</result>
</action>
不一样的天空 2024-09-26 17:41:36

避免此问题的一种方法是,在 web.xml 文件中添加欢迎文件列表条目。这应该指向您的登录页面。一旦您启动应用程序,此登录页面就会打开,无需执行任何类型的操作和验证。

<welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
</welcome-file-list>

启动服务器后,您可以通过提供以下格式的 url 来访问您的应用程序

http://localhost:8080/ApplicationContext“​​
这里 8080 - 是默认的 HTTP 端口,在您的情况下可能会有所不同。
ApplicationContext - 是您的应用程序的上下文。

我认为其他事情应该很好。

One way to avoid this issue is, in your web.xml file have an entry for welcome file list. This should point to your login page. As soon you start your application this login page will open with out doing any kind of action and validation.

<welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
</welcome-file-list>

After starting your server, you can access your application by giving url in below format

"http://localhost:8080/ApplicationContext"
Here 8080 - is default HTTP port, it can be different in your case.
ApplicationContext- is the context of your application.

I think other thing should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文