cool hit counter SSH Fast Forward] - struts2 Model Driven - ModelDriven_Intefrankly

SSH Fast Forward] - struts2 Model Driven - ModelDriven


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/47144849

The action-LoginAction that handles user login in the previous blog, SSH Quick Advance - A Simple Example of struts2, is.

package com.danny.user.action;

public class LoginAction {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute() {
        if("admin".equals(username) && "admin".equals(password))
        {
            return "success";
        }else{
            return "error";
        }
    }
}

You can find that the properties in the LoginAction and the input in the jsp page form are one and the same, which is struts2's property-driven. But it's a landing page with just a username and password, what if the client needs you to make a page like this?

I guess you'll just have to lol~~

At this point, if you use the first way, that is, property-driven to pass the value, you can certainly imagine how messy it can be in the action, the code to define the property alone can take up 200 lines ~~

So this is a good time to consider using another way of passing values in struts2 - model driven.

For simplicity, the example from the previous blog is also used to illustrate.

If the LoginAction in the previous blog needs to be model driven, then just add a model User.java and modify the LoginAction.

User.java

package com.danny.user.vo;

public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

LoginAction.java

package com.danny.user.action;

import com.danny.user.vo.*;
import com.opensymphony.xwork2.ModelDriven;

 // To implement a model-driven Action you need to implement the ModelDriven interface
public class LoginAction  implements ModelDriven{

     // Model instances must be defined and instantiated
    private User user=new User();

     // Implement the getModel method for getting the instance object
    @Override
    public Object getModel() {
        return user;
    }

     //Citing model examples    
    public String execute() {
        if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword()))
        {
            return "success";
        }else{
            return "error";
        }
    }
}

The LoginAction modifications are. 1. implements the ModelDriven interface. 2, removed the definition of fields and properties, instead of defining and instantiating the model object, the implementation of getModel method used to obtain the object, in other member methods can be directly referenced in this object.

Note that the value of the name attribute of the page form input must be the same as the name that follows the get and set methods of the model (e.g., User.java here). For example, if the name property of an input in a form is "username", then the corresponding get method is named "getUsername" or "getusername" (case-insensitive).


Recommended>>
1、Seeing the future of intelligent transportation era is emerging
2、The first thing you need to do is to get a good idea of what youre doing
3、Southeast Asia Japan Digital Asset Trading Platform Build Development
4、Li Yunze New finance is the core competence for future development
5、WISeKey Launches First Full Blockchain IoT Platform Between China and US

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号