JSF最简单的例子(3)

作者在 2008-03-14 16:47:00 发布以下内容

后面的就只有DAO层和Serivce层了,这二个类很简单的了。

DAO:

/*
 * Created on 2007-9-13
 *
 * Copyright (c) 2006 Prudential Services Asia. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Prudential Services Asia. ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with Prudential.
 */
package com.jsf.dao;

import com.jsf.bean.UserBean;

/**
 * LoginDao
 *
 * @author Shuai.yang
 * @date 2007-9-13
 * @version 1.0
 */
public class LoginDao {
 private UserBean user;

 /**
  * @return Returns the user.
  */
 public UserBean getUser() {
  return user;
 }

 /**
  * @param user
  *            The user to set.
  */
 public void setUser(UserBean user) {
  this.user = user;
 }

 public boolean checkUser() {
  boolean reslut = false;
  if (user.getUserName().equals("a") && user.getUserPwd().equals("a")) {
   reslut = true;
  }
  return reslut;
 }
}
通过XML配置文件就可以把UserBean注入到DAO层来进行操作了。。。

同样的可以把DAO注入到Service层去进行业务逻辑操作:

Service:

/*
 * Created on 2007-9-10
 *
 * Copyright (c) 2006 Prudential Services Asia. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of Prudential Services Asia. ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with Prudential.
 */
package com.jsf.service;

import com.jsf.dao.LoginDao;

/**
 * LoginService
 *
 * @author Shuai.yang
 * @date 2007-9-10
 * @version 1.0
 */
public class LoginService {
 private LoginDao loginDao;

 /**
  * @return Returns the loginDao.
  */
 public LoginDao getLoginDao() {
  return loginDao;
 }

 /**
  * @param loginDao
  *            The loginDao to set.
  */
 public void setLoginDao(LoginDao loginDao) {
  this.loginDao = loginDao;
 }

 /**
  *
  * @return
  *
  * @author Shuai.yang
  * @date 2007-9-10
  */
 public String loginAction() {
  if (loginDao.checkUser()) {
   return "welcome";
  }
  return "false";
 }
}
最后是web.xml:

<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.face</url-pattern>
 </servlet-mapping>

 <!-- Welcome files -->
 <welcome-file-list>
  <welcome-file>/page/index.jsp</welcome-file>
 </welcome-file-list>

</web-app>

-----------------------------------------------------------------

到此所有的主要代码都已完了。。。

J2ee | 阅读 3014 次
文章评论,共1条
恋轩念伊人
2008-03-15 11:17
1
de zou zou a
游客请输入验证码
文章归档