PropertyNotFoundException:目标无法访问,“userObj”使用 jsf 按钮和请求范围返回 null

发布于 2024-12-10 23:11:11 字数 1284 浏览 0 评论 0原文

情况如下,我有一个页面来添加/更新用户 在预渲染方法中,根据参数创建一个新对象或获取现有对象

@Component("user")
@Scope("request")
public class UserBean {


    private User userObj;
    private boolean editUser;


    public String addUser() throws Exception {

        if (editUser) {
            userService.updateUser(userObj);
        } else {
            userService.addUser(userObj);
        }
        return "users?faces-redirect=true";
    }

    public void preRender(ComponentSystemEvent event) throws Exception {

            System.out.println("############ PRERENDER #############");
            if (editUser) {
                userObj = userService.getUser(userID);
                pageTitle = "Updating " + userObj.getName();
                buttonTitle = "Save Changes";
            } else {
                userObj = new User();
                pageTitle = "Adding new user";
                buttonTitle = "Add User";

            }

        }

,在 jsf 页面中,我将预渲染称为:

<f:event id="event1" listener="#{user.preRender}" type="javax.faces.event.PreRenderComponentEvent" />

但是当我按下添加按钮时,如下所示:

<h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; "/> 

我遇到上述异常,请告知。

the situation is as follows, i have a page to add/update user
in the prerender method depending on a parameter either creating a new object or getting existing one

@Component("user")
@Scope("request")
public class UserBean {


    private User userObj;
    private boolean editUser;


    public String addUser() throws Exception {

        if (editUser) {
            userService.updateUser(userObj);
        } else {
            userService.addUser(userObj);
        }
        return "users?faces-redirect=true";
    }

    public void preRender(ComponentSystemEvent event) throws Exception {

            System.out.println("############ PRERENDER #############");
            if (editUser) {
                userObj = userService.getUser(userID);
                pageTitle = "Updating " + userObj.getName();
                buttonTitle = "Save Changes";
            } else {
                userObj = new User();
                pageTitle = "Adding new user";
                buttonTitle = "Add User";

            }

        }

and in the jsf page i call the prerender as:

<f:event id="event1" listener="#{user.preRender}" type="javax.faces.event.PreRenderComponentEvent" />

but when i press the add button which is as follows:

<h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; "/> 

i am getting above exception, please advise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2024-12-17 23:11:11

问题是您的 h:commandButton 正在向服务器创建一个新请求,这会导致 UserBeanRequest

我能想到的解决方案有几种。

1)
看来您知道您的页面是否处于编辑模式。然后,您可以在调用 userObj 的 getter 时摆脱 preRender 方法,而是从数据库获取 userObj。然后,无论您的页面是否处于编辑模式,您都可以传递给您的add 方法。因此,您必须修改您的 commandButton:(注意:您必须将值更改为当前的编辑模式)

  <h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; ">
      <f:setPropertyActionListener value="true" target="#{user.editUser}" />
  </h:commandButton>

和您的 userBean 为:

@Component("user")
@Scope("request")
public class UserBean {

private User userObj;
private boolean editUser;

public String addUser() throws Exception {
    userObj = getUserObj();
    if (editUser) {
        userService.updateUser(userObj);
    } else {
        userService.addUser(userObj);
    }
    return "users?faces-redirect=true";
}

public void setEditUser(boolean editUser) {
    this.editUser = editUser;
}

public User getUserObj() {
    if (editUser) {
        if(userObj == null) {
            userObj = userService.getUser(userID);
        }
        return userObj;
    } 
    else {
        return userObj = new User();
    }
}

public void setUserObj(User userObj) {
    this.userObj = userObj;
}

这应该让您了解如何操作有用。诀窍是使用f:setPropertyActionListener

2)您可以使用视图范围来解决该问题。问题是 spring 不提供开箱即用的功能。好消息是您可以自己构建视图范围。看看博客文章

The problem is that your h:commandButton is creating a new request to the server which causes the UserBean to creating a new instance of itself in scope Request.

There are several solutions I can think of.

1)
It seems that you know if your page is in edit mode or not. Then you can get rid of your preRender method and instead get the userObj from database when calling the getter of userObj. Then you can pass to your add method if your page is in edit mode or not. Therefore you have to modify your commandButton: (note: you have to change the value to your current edit mode)

  <h:commandButton value="#{user.buttonTitle}" action="#{user.addUser}" style="width: 105px; ">
      <f:setPropertyActionListener value="true" target="#{user.editUser}" />
  </h:commandButton>

and your userBean to:

@Component("user")
@Scope("request")
public class UserBean {

private User userObj;
private boolean editUser;

public String addUser() throws Exception {
    userObj = getUserObj();
    if (editUser) {
        userService.updateUser(userObj);
    } else {
        userService.addUser(userObj);
    }
    return "users?faces-redirect=true";
}

public void setEditUser(boolean editUser) {
    this.editUser = editUser;
}

public User getUserObj() {
    if (editUser) {
        if(userObj == null) {
            userObj = userService.getUser(userID);
        }
        return userObj;
    } 
    else {
        return userObj = new User();
    }
}

public void setUserObj(User userObj) {
    this.userObj = userObj;
}

This should give you a basic idea how it works. The trick is to use the f:setPropertyActionListener.

2) You could use the view scope to solve that issue. The problem is spring doesn't offer this out of the box. The good news is that you can build the view scope on your own. That a look at that blog post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文