PropertyNotFoundException:目标无法访问,“userObj”使用 jsf 按钮和请求范围返回 null
情况如下,我有一个页面来添加/更新用户 在预渲染方法中,根据参数创建一个新对象或获取现有对象
@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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的
h:commandButton
正在向服务器创建一个新请求,这会导致UserBean
在Request
。我能想到的解决方案有几种。
1)
看来您知道您的页面是否处于编辑模式。然后,您可以在调用
userObj
的 getter 时摆脱preRender 方法
,而是从数据库获取userObj
。然后,无论您的页面是否处于编辑模式,您都可以传递给您的add 方法
。因此,您必须修改您的commandButton
:(注意:您必须将值更改为当前的编辑模式)和您的
userBean
为:这应该让您了解如何操作有用。诀窍是使用
f:setPropertyActionListener
。2)您可以使用视图范围来解决该问题。问题是 spring 不提供开箱即用的功能。好消息是您可以自己构建视图范围。看看博客文章。
The problem is that your
h:commandButton
is creating a new request to the server which causes theUserBean
to creating a new instance of itself in scopeRequest
.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 theuserObj
from database when calling the getter ofuserObj
. Then you can pass to youradd method
if your page is in edit mode or not. Therefore you have to modify yourcommandButton
: (note: you have to change the value to your current edit mode)and your
userBean
to: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.