Hello my friends,
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.