简体   繁体   English

Struts2如果标签不起作用

[英]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. 我正在编写一个登录页面,当无效用户尝试登录时,我将重定向到登录操作,错误参数等于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. 如果无效用户尝试登录,则会重定向到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. 我试图通过使用if标签访问错误参数向用户显示错误消息,但它不工作消息未显示。

<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: 粗略地说,IMO看起来应该更像这样:

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. 您将获得包含表单的"input"结果,并使用您想要的任何样式显示任何操作错误。 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. 不相关,但我loginUser代码完全移出操作并进入实用程序/服务类,但至少将它包含在一个单独的方法中,您可以更轻松地模拟它。 It certainly does not belong in the execute method. 这当然不会在属于execute方法。

You need to provide the getter and setter of field 'error' to access it from value stack. 您需要提供字段'error'的gettersetter以从值堆栈访问它。

public int getError() 
{
   return error;
}

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

And try to access it in OGNL: 并尝试在OGNL中访问它:

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

Or using JSTL: 或者使用JSTL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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