下面就举SSH的例子来进行详细的説明:
登陆的Action:
1.
控制层(1).LoginAction:
只需要导入登陆的
ILoginService
接口
eg:
ILoginService loginService;
private Admin admin = null;
public String adminLogin() {
Admin _admin = loginService.adminLogin(admin.getAdminAccount(), admin
.getPassword());
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
if (_admin != null) {
session.removeAttribute("adminObj");
session.setAttribute("adminObj", _admin);
return SUCCESS;
} else {
return FLASE;
}
}
|
|
2.
业务逻辑层(1).登陆接口ILoginService的详细代码:
eg:
public interface ILoginService {
Admin adminLogin(String adminAccount, String password);
} |
(2).登陆LoginService的详细代码:
eg:
public class LoginService implements ILoginService{ ILoginDao loginDao;
public ILoginDao getLoginDao() { return loginDao; }
public void setLoginDao(ILoginDao loginDao) { this.loginDao = loginDao; }
public Admin adminLogin(String adminAccount, String password) { return loginDao.adminLogin(adminAccount, password); } } |
3.
数据访问层:
(1)登陆接口ILoginDao
eg:
public interface ILoginDao { Admin adminLogin(String adminAccount, String password);
}
|
(2)登陆DAO-loginDao
public class LoginDao extends HibernateDaoSupport implements ILoginDao{ public Admin adminLogin(String adminAccount, String password) { String hql = "from Admin as admin where admin.adminAccount='" + adminAccount + "' and admin.password='" + password + "'"; List list = this.getHibernateTemplate().find(hql); return (list.size() != 0) ? ((Admin) list.get(0)) : null; }
} |
4.
Spring配置文件
(1)(登陆的配置文件)-applicationContext_login.xml
eg:
(2)
总的Spring配置文件
lazy-init="true">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" abstract="false" singleton="true" lazy-init="default" autowire="default" dependency-check="default"> classpath:com/itanger/domain/
org.hibernate.dialect.MySQLDialect true
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED, readOnly
|
5.web.xml
xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_4.xsd"> SSH配置系统
jsp/start.jsp
log4jConfigLocation /WEB-INF/classes/log4j.xml contextConfigLocation /WEB-INF/applicationContext*.xml
struts2 struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /*
struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp struts-cleanup /*
action2 org.apache.struts2.dispatcher.FilterDispatcher action2 *.action
action2 *.jsp org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.Log4jConfigListener
|
以上的学习,你基本上对面向借口编程和SSH有了一点的了解,其中面向接口编程为什么好,为什么适用了面向接口后,对以后的维护有帮助,对代码的修改有帮助,这个你就在日常的工作中慢慢的体会吧,多说不益。。。。
阅读(1141) | 评论(0) | 转发(0) |