如何在用户提交内容时验证表单(使用 Struts2)
我正在开发一个非常简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找的是使用相同的操作来显示表单,然后(提交后)处理表单。
您的表单应提交到“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.
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.你需要两个动作。
您的登录操作重定向到您的login.jsp。
处理登录过程的 LoginSubmit 操作。
对于第二个,您可以定义验证。
you need two actions.
Your Login Action which redirect to your login.jsp.
A LoginSubmit Action which handle the login proccess.
for the second one you can define your validation.
您需要一个重定向到 login.jsp 的操作和另一个处理登录过程和验证的操作。
在第一个操作中不要写任何内容,只需设置 result=success。
Struts.xml 中的条目如下所示:
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.
Entries in Struts.xml are like follows:
避免此问题的一种方法是,在 web.xml 文件中添加欢迎文件列表条目。这应该指向您的登录页面。一旦您启动应用程序,此登录页面就会打开,无需执行任何类型的操作和验证。
启动服务器后,您可以通过提供以下格式的 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.
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.