简体   繁体   English

在JSF 2.0中访问bean属性

[英]Accessing bean properties in JSF 2.0

I have the following model code, which I am supposed to use. 我有以下应该使用的模型代码。

    public class Client extends User {

    private String userName;

    public Client(String firstName, String lastName, String userName){
        super(firstName, lastName);
        this.userName = userName;
    }

    //getters and setters
}

public abstract class User {
    String firstName;
    String lastName;

   //getters and setters
}

Now I have created the following bean: 现在,我创建了以下bean:

@ManagedBean(name = "client")
@SessionScoped
public class ClientBean implements Serializable {

    private final long serialVersionUID = 1L; 
    private Client client;

    public Client getClient(){
        return client;
    }

    public void setClient(Client client){
        this.client = client;
    }

} }

Now I want to set the clients' firstName using this bean in an xhtml page: 现在,我想在xhtml页面中使用此bean设置客户的名字:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <head>
        <title>Register as a client</title>
    </head>
    <body>
        <h:form>
            First Name:<h:inputText value="#{???}"></h:inputText>
            <br/>                  
            <h:commandButton value="Register" action="registered?faces-redirect=true"/>
        </h:form>
    </body>
</html> 

Now my question is: How do I access the clients' firstName? 现在我的问题是:如何访问客户的名字? Am I supposed to create a new bean that represents user as well and extend it in ClientBean? 我是否应该创建一个代表用户的新bean并将其扩展到ClientBean中? (If so, what's the use of having model code at all? I would have double code everywhere?) Or is there any other simpler way to implement this in JSF 2.0? (如果是这样,那么完全拥有模型代码有什么用?我到处都有双重代码?)或者在JSF 2.0中还有其他更简单的方法来实现这一点吗?

You will need the following for the page to display the last name correctly. 该页面需要以下内容才能正确显示姓氏。

-- The class User must have a constructor as below along with getters and setters for firstname and lastname. -User类必须具有以下构造函数,以及用于firstname和lastname的getter和setter。

  public User (String firstName, String lastName)

-- Public getter and setter method for username in Client class. -Client类中用户名的公共getter和setter方法。

-- In the ClientBean class, I would suggest you change the name to clientBean. -在ClientBean类中,建议您将名称更改为clientBean。 Also, change the getter and setter method to public instead of private. 另外,将getter和setter方法更改为public而不是private。 You would need to create an object of type client and initialize the client object to some value if you need to display it on the screen. 如果需要在屏幕上显示该对象,则需要创建一个类型为client的对象并将其初始化为某个值。 In the code provided, you are not creating an object or giving any value to any of the name properties. 在提供的代码中,您不会创建对象或为任何名称属性提供任何值。

-- In the JSF page, you can access the values using "#{clientBean.client.firstName}" -在JSF页面中,您可以使用"#{clientBean.client.firstName}"访问值

Make getter and setter public 公开获取器和设置器

 public Client getClient(){
        return client;
    }

    public void setClient(Client client){
        this.client = client;
    }

If your Client is null , simply instantiate it. 如果您的Clientnull ,则只需实例化它。

private Client client = new Client();

You would take this approach with a managed bean and a pojo if you want to for instance persist the values in a database, or do some other magic stuff, like calling a webservice. 如果您想例如将值保留在数据库中,或者做一些其他魔术,例如调用Web服务,则可以对托管bean和pojo采取这种方法。

To access the first name you write #{client.client.firstName} Indeed it looks a bit awesome, so I suggest give the managed bean another name. 要访问您的名字,您可以编写#{client.client.firstName}确实确实很棒,所以我建议给托管bean再加上一个名字。

You could create an instance of the pojo in the managed bean: 您可以在托管bean中创建pojo的实例:

public class ClientBean implements Serializable {

    private Client client = new Client();
    ...
}

You could also include first name and last name in the managed bean directly, this would make sense in case you are creating the pojo during some action to save the values. 您还可以在托管bean中直接包含名字和姓氏,以防在某些操作中创建pojo来保存值时这样做。

JSF doesn't press you in a corset, instead you can choose the way that fits you. JSF不会紧身胸衣,而是可以选择适合您的方式。

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

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