1 登陆中没有绑定vo的情况下如何验证账号和密码:
a:解决方案,可以在页面随便绑定的am中的方法,比如commit就行了,然后在am的实现类中编写账号和密码验证,具体方法如下,在am实现类的中代码:
/**
* 验证账号是否存在
* @param username
* @return
*/
public boolean hasUsername(String username){
ViewObjectImpl usernameVo= getTUserView1() ;
usernameVo.setWhereClause(null);
usernameVo.setWhereClause("Username='"+username+"'");
usernameVo.executeQuery();
return usernameVo.getEstimatedRowCount()>0;
}
/**
* 验证账号和密码是否正确
* @param username
* @param password
* @return
*/
public boolean valdateUsernameAndpassword(String username,String password){
ViewObjectImpl usernameVo= getTUserView1() ;
usernameVo.setWhereClause(null);
usernameVo.setWhereClause("Username='"+username+"' and Password='"+password+"'");
System.out.println(usernameVo.getQuery());
usernameVo.executeQuery();
return usernameVo.getEstimatedRowCount()>0;
}
b:MB中的方法:
public void loginValidate(ActionEvent actionEvent) {
Boolean has=false;
if (this.getUsername() != null && this.getPassword() != null) {
OperationBinding hasUsernameMethod = ADFUtils.findOperation("hasUsername");
hasUsernameMethod.getParamsMap().put("username", this.getUsername());
hasUsernameMethod.execute();
has = (Boolean) hasUsernameMethod.getResult();
if (Boolean.FALSE.equals(has)) {
this.setLoginAction(null);
JSFUtils.addFacesInformationMessage("账号不存在,请输入正确的账号!");
return;
}else{
OperationBinding usernameAndPassword = ADFUtils.findOperation("valdateUsernameAndpassword");
usernameAndPassword.getParamsMap().put("username", this.getUsername());
usernameAndPassword.getParamsMap().put("password", this.getPassword());
usernameAndPassword.execute();
has = (Boolean) usernameAndPassword.getResult();
if(Boolean.FALSE.equals(has)){
this.setLoginAction(null);
JSFUtils.addFacesInformationMessage("密码错误,请输入正确的密码!");
return;
}else{
ADFContext context=ADFContext.getCurrent();
context.getSessionScope().put("user", this.getUsername());
this.setLoginAction("login");
}
}
} else {
this.setLoginAction(null);
JSFUtils.addFacesInformationMessage("请输入账号和密码!");
return;
}
}
c:登陆页面代码:
<af:form id="f1">
<af:panelFormLayout id="pfl1">
<af:inputText label="账号" id="it1" value="#{LoginBean.username}"/>
<af:inputText label="密码" id="it2" value="#{LoginBean.password}">
<af:clientListener method="onFieldEnterKey" type="keyPress"/>
</af:inputText>
<f:facet name="footer">
<af:button text="登陆" id="b1" action="#{LoginBean.getLoginAction}"
actionListener="#{LoginBean.loginValidate}"/>
</f:facet>
</af:panelFormLayout>
</af:form>
2 响应回车事件:
<af:resource type="javascript">
function onFieldEnterKey(event) {
if (event.getKeyCode() == AdfKeyStroke.ENTER_KEY) {
var inputTextField = event.getSource();
var defaultButton = inputTextField.findComponent('b1');
AdfActionEvent.queue(defaultButton);
}
}
</af:resource>
3登陆后的Session问题:
因为session的值需要在VO,AM中都能访问,所以用AdfContext类来存,代码如下:
ADFContext context=ADFContext.getCurrent();
context.getSessionScope().put("user", this.getUsername());
然后在页面直接#{sessionScope.user}即可.
4 关于AM获取的session,这个类除了用ADF自带的权限的时候就使用,此类中提供了一些方法比如getUserData(),getUserRoles(),isUserInRole()来获取用户与角色相关信息.
实际应用中,ADF的权限满足不了我们的需求,但我们可以自己实现的权限功能也提供上述的方法以供页面能方便的控制权限。