views:

43

answers:

3

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.

A: 

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.

jogep
A: 

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>
lucentmind
A: 

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.

Kamal