【发布时间】:2013-07-11 19:27:09
【问题描述】:
我在 JSF2.0 中有一个简单的应用程序。我在 Session 范围内创建了一个 ManagedBean。在这个托管 bean 中,我存储了一个属性。但是当我尝试从 jsp 访问该属性时,它没有被检索到。我正在存储 roleType 的值。 这是ManagedBean中的方法
public class LoginBean {
private String userID;
private List<String> roleNames;
private String roleType;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public List<String> getRoleNames() {
return roleNames;
}
public void setRoleNames(List<String> roleNames) {
this.roleNames = roleNames;
}
public String getRoleType() {
return roleType;
}
public void setRoleType(String roleType) {
this.roleType = roleType;
}
public String createAuth() throws SQLException
{
Authorisation authCall=com.validation.client.authorization.concern.AuthorisationCall.createAuthorisation(userID);
if(authCall.getUserRoles()==null || authCall.getUserRoles().size()<=0){
return "login";
}
List<String> roleNameList=new ArrayList<String>();
Iterator itr =authCall.getUserRoles().iterator();
String roleType=null;
while (itr.hasNext()){
UserRole userRole=(UserRole)itr.next();
roleNameList.add(userRole.getRoleName());
roleType=userRole.getRoleDescription();
}
setRoleNames(roleNameList);
setRoleType(roleType);
}
}
The jsp page where I am retrieving the property is :
<c:if test="${loginBean.roleType=='APPROVER'}">
<h:outputText value="loginBean.roleType"/>
<c:if>
在 faces-config.xml 我这样做是为了注册 bean
<managed-bean>
<description>temporary authentication</description>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.validation.client.authorization.concern.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
这是我在 JSF 中的第一次尝试,我真的很坚持。
【问题讨论】:
-
在 JSP 中访问托管 bean 不是一个好习惯。是这个要求还是你想试试?
-
@erencan 你在哪里读到的?我想说的是最常见的做法。
-
可能您的
roleType属性仍然是null,因为尚未分配值。你在哪里调用createAuth()方法?此外,对属性的访问必须是 EL 表达式:#{loginBean.roleType} -
@XtremeBiker 首先,您应该避免在 JSP 中使用 scriptlet,这是在 JSP 中访问托管 bean 的方式。 stackoverflow.com/a/3180202/892994。 JSP 中缺少 MVC 模式,它们有不同的生命周期。
-
如果您查看链接,您会注意到 jstl 是从 jsp 访问托管 bean 的另一种方式,这就是 OP 在其示例中所写的方式。事实上,这个问题本身与 scriptlet 无关。
标签: jsf