技术开发 频道

整合JSF、Hibernate、Spring实现用户登录应用



四、表示层
我们实现了业务逻辑,接下来就看业务逻辑是如何与JSF页面相关联的。
首先我们定义了一个UserBean类,该类实现了一个verify()方法,如果登录成功,返回“success”字符串,如果失败,则返回“failure”字符串。
10. com.it168.logon.view.bean.UserBean.java代码片断
public String verify() { Userinfo user = serviceLocator.getUserService().login(this.username, this.password); if (user == null) { this.errorMessage = "登录失败"; this.loggedIn = false; return "failure"; } else { this.loggedIn = true; return "success"; } }

 

大家可能注意到了这段代码:

Userinfo user = serviceLocator.getUserService().login(this.username, this.password);

 

这里的serviceLocator是ServiceLocatorBean类的实例:
11. com.it168.logon.view.bean.ServiceLocatorBean.java
package com.it168.logon.view.bean; import javax.faces.context.FacesContext; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.it168.logon.model.service.IUserService; import com.it168.logon.model.service.impl.UserService; import com.it168.logon.view.servicelocator.IServiceLocator; public class ServiceLocatorBean implements IServiceLocator { private static final String USER_SERVICE_BEAN_NAME = "userService"; private ApplicationContext appContext; private UserService userService; public ServiceLocatorBean() { ServletContext context = (ServletContext) FacesContext .getCurrentInstance().getExternalContext().getContext(); this.appContext = WebApplicationContextUtils .getRequiredWebApplicationContext(context); this.userService = (UserService) this .lookupService(USER_SERVICE_BEAN_NAME); } public IUserService getUserService() { return this.userService; } public Object lookupService(String serviceBeanName) { return appContext.getBean(serviceBeanName); } }

 

ServiceLocatorBean类实现了业务逻辑与JSF页面上下文的关联。
现在来看一下JSF页面如何实现了与UserBean的绑定:
12. login.jsp代码片断
<f:view> <h:form rendered="true"> <div align="center"> <h:outputText escape="false" rendered="true" value="#{userBean.errorMessage}"></h:outputText> </div> <div align="center"> <h:outputText value="用户名: " /> <h:inputText id="username" required="true" value="#{userBean.username}"> </h:inputText> <h:message for="username" /> <br> </div> <div align="center"> <h:outputText value="密 码: " /> <h:inputSecret id="password" required="true" rendered="true" value="#{userBean.password}" style="width: 154px"> </h:inputSecret> <h:message for="password" /> <br> </div> <br> <div align="center"> <h:commandButton rendered="true" value="登录" action="#{userBean.verify}"></h:commandButton> </div> </h:form> </f:view>
其中,“userBean”从何而来呢?
13. faces-config.xml代码片断一
 
<managed-bean> <managed-bean-name>userBean</managed-bean-name> <managed-bean-class> com.it168.logon.view.bean.UserBean </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>serviceLocator</property-name> <value>#{serviceLocatorBean}</value> </managed-property> </managed-bean>
 

在faces-config.xml文件中的<managed-bean>节点中,我们定义了“userBean”对应的类。
14. faces-config.xml代码片断二
<navigation-rule> <from-view-id>/pages/login.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/pages/welcome.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>failure</from-outcome> <to-view-id>/pages/login.jsp</to-view-id> </navigation-case> </navigation-rule>

 

        在faces-config.xml文件中的<navigation-rule>节点中,我们定义了页面导航规则。

到目前为止,我们介绍了Hibernate、Spring和JSF如何在三层设计中发挥作用的,现在我们可以看一下实际效果,访问http://localhost:8080/Logon

(图1)
如果登录成功:


(图2)
登录失败,则:




0
相关文章