简体   繁体   中英

Struts2 if tag not working

I am writing a login page that when a invalid user tries to login I redirect to the login action with a error parameter equal to 1.

private String username;
private String password;
private int error;
@Override
public String execute()
{
    //validate user input
    if (username == null || password == null || username.isEmpty() || password.isEmpty())
    {
        error = 2;
        return LOGIN;   
    }

    LoginModel loginModel = new LoginModel(username, password);

    HttpBuilder<LoginModel, User> builder = new HttpBuilder<LoginModel, User>(User.class);
    builder.setPath("service/user/authenticate");
    builder.setModel(loginModel);

    IHttpRequest<LoginModel, User> request = builder.buildHttpPost();
    User user = request.execute(URL.BASE_LOCAL);

    //redirects to login page
    if (user == null)
    {
        error = 1;
        return LOGIN;
    }
    else
    {   
        return SUCCESS;
    }
}

   //Getters/Setters

If a invalid user trys to login it redirects to localhost:8080/app/login.action?error=1. I am trying to display a error message to user by access the error parameter by using the if tag but its not working the message is not displaying.

<s:if test="error == 1">
<center>
    <h4 style="color:red">Username or Password is invalid!!</h4>
</center>

What am I doing wrong?

As far as I'm concerned what you're doing wrong is completely ignoring the framework.

Roughly, IMO this should look more like this:

public class LoginAction extends ActionSupport {

    private String username;
    private String password;

    @Override
    public String validate() {
        if (isBlank(username) || isBlank(password)) {
            addActionError("Username or Password is invalid");
        }

        User user = loginUser(username, password);
        if (user == null) {
            addActionError("Invalid login");
        }
    }

    public User loginUser(String username, String password) {
        LoginModel loginModel = new LoginModel(username, password);

        HttpBuilder<LoginModel, User> builder = new HttpBuilder<LoginModel, User>(User.class);
        builder.setPath("service/user/authenticate");
        builder.setModel(loginModel);

        IHttpRequest<LoginModel, User> request = builder.buildHttpPost();
        return request.execute(URL.BASE_LOCAL);
    }

}

You would have an "input" result containing the form, and display any action errors present using whatever style you wanted. If it's critical to change the styling based on which type of login error it is you'd have to play a few more games, but that seems excessive.

Unrelated, but I'd move that loginUser code completely out of the action and into a utility/service class, but at least with it wrapped up in a separate method you can mock it more easily. It certainly does not belong in the execute method.

You need to provide the getter and setter of field 'error' to access it from value stack.

public int getError() 
{
   return error;
}

public void setError(int error) 
{
   this.error = error;
}

And try to access it in OGNL:

<s:if test="%{#error==1}"></s:if>

Or using JSTL:

<c:if test="${error==1}"></c:if>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM