托管属性不工作
我在 JSF 2 属性绑定方面遇到问题,老实说,我在这里碰壁了。
我想要完成的是:请求范围的 bean (loginBean) 处理登录操作并将用户名存储在会话范围的 bean (userBean) 中。我想通过@ManagedProperty将userBean注入到loginBean中,但是当调用loginBean.doLoginAction时,userBean被设置为null。
这是代码:
UserBean 类
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isLogged() {
if (username != null)
return true;
return false;
}
}
loginBean 类:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class LoginBean {
@ManagedProperty(value = "userBean")
private UserBean userBean;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserBean getUserBean() {
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public String doLoginAction() {
if (name.equals("kamil") && password.equals("kamil")) {
userBean.setUsername(name);
}
return null;
}
public String doLogoutAction() {
return null;
}
}
知道我在这里做错了什么吗?
I have problem with JSF 2 property binding and honestly I hit a wall here..
What I want to accomplish is this: a request-scoped bean (loginBean) process login action and stores username in the session-scoped bean (userBean). I want to inject userBean into loginBean via @ManagedProperty, but when loginBean.doLoginAction is called, userBean is set to null.
Here is the code:
UserBean class
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isLogged() {
if (username != null)
return true;
return false;
}
}
loginBean class:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class LoginBean {
@ManagedProperty(value = "userBean")
private UserBean userBean;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserBean getUserBean() {
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public String doLoginAction() {
if (name.equals("kamil") && password.equals("kamil")) {
userBean.setUsername(name);
}
return null;
}
public String doLogoutAction() {
return null;
}
}
Any ideas what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定一个 EL 表达式
#{}
,而不是普通字符串:或者更短,因为
value
属性已经是默认值:另请参阅:
You need to specify an EL expression
#{}
, not a plain string:or, shorter, since the
value
attirbute is the default already:See also: