[原]Java web学习系列之 Java web开发中的SSH整合(Struts、Spring、Hibernate)(下)
2012-02-28 13:47 雪夜&流星 阅读(265) 评论(0) 编辑 收藏 举报接上一篇:
创建dao接口类及方法:
public interface UsersDAO {
public void save(Users user);
public void delete(Users user);
public void update(Users user);
public void findById(String name);
}
创建dao类方法的实现类:
public class UsersDAOImpl extends HibernateDaoSupport implements UsersDAO{
public void delete(Users user) {
// TODO Auto-generated method stub
}
public void findById(String name) {
// TODO Auto-generated method stub
}
public void save(Users user) {
// TODO Auto-generated method stub
}
public void update(Users user) {
// TODO Auto-generated method stub
}
}
创建service接口类:
public interface UsersService {
public void deleteUsers(String name,String pwd);
public Users findUsers(String name,String pwd);
public void delete_find(String name,String pwd);
}
创建service接口类的实现类:
public class UsersServiceImpl implements UsersService {
//注入对象
private UsersDAO usersDAO=null;
public UsersDAO getUsersDAO() {
return usersDAO;
}
public void setUsersDAO(UsersDAO usersDAO) {
this.usersDAO = usersDAO;
}
public void deleteUsers(Users user) {
usersDAO.delete(user);
}
public void deleteUsers(String name, String pwd) {
// TODO Auto-generated method stub
}
public void delete_find(String name, String pwd) {
Users users=this.findUsers(name, pwd);
this.deleteUsers(users);
}
public Users findUsers(String name, String pwd) {
// TODO Auto-generated method stub
return null;
}
}
添加action:
public class LoginAction extends Action {
private UsersService usersService=null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
//判断注入是否成功
//System.out.println(usersService);
Users users=loginForm.getUsers();
usersService.delete_find(users.getUserName(), users.getPassword());
return mapping.findForward("index");
}
public UsersService getUsersService() {
return usersService;
}
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}
}
LoginForm 中方法的构造:
public class LoginForm extends ActionForm {
private Users users=new Users();
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
<struts-config>
<form-beans >
<form-bean name="loginForm" type="org.clarck.struts.form.LoginForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.clarck.struts.action.LoginAction">
<forward name="index" path="/index.jsp" />
</action>
</action-mappings>
在Struts—config.xml中配置文件:
<!-- 转交控制权给Spring -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
//转交控制权的配置文件必须在<message-resources />之上
<message-resources parameter="org.clarck.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
</struts-config>
在applicationContext.xml中配置Javabean:
新建dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 该文件只配置Service类 -->
<bean id="usersService" class="org.clarck.model.service.impl.UsersServiceImpl">
<property name="usersDAO" ref="usersDAO"/>
</bean>
</beans>
配置service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 该文件只配置Service类 -->
<bean name="/login" class="org.clarck.struts.action.LoginAction">
<property name="usersService" ref="usersService"/>
</bean>
</beans>
配置action.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 该文件只配置Action类 -->
<bean name="/login" class="org.clarck.struts.action.LoginAction">
<property name="usersService" ref="usersService"/>
</bean>
</beans>
在applicationContext.xml中导入配置好的xml文件:
<import resource="dao.xml"/>
<import resource="service.xml"/>
<import resource="action.xml"/>
在applicationContext.xml中配置事务管理器:
<!-- 配置事务管理器 -->
<bean id="transactionmanager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
在applicationContext.xml中配置事务管理策略:
<!-- 配置事务管理策略 -->
<tx:advice id="txAdvice" transaction-manager="transactionmanager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="login" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
在applicationContext.xml中配置事物拦截切:
<aop:config>
<!-- 配置事物拦截切面 -->
<aop:pointcut id="allBiz" expression="execution(* org.clarck.model.service.impl.*.*(..))"/>
<aop:advisor pointcut-ref="allBiz" advice-ref="txAdvice"/>
</aop:config>
在applicationContext.xml中配置配置日志对象的切点:
<!-- 配置日志对象的切点 -->
<bean id="log" class="org.clarck.model.interceptor.Log" />
在applicationContext.xml中配置配置日志拦截切面 :
<!-- 配置日志拦截切面 -->
<aop:config>
<aop:aspect ref="log">
<aop:before pointcut-ref="allBiz" method="before"/>
</aop:aspect>
</aop:config>
配置日志文件类:
public class Log {
public void before(JoinPoint point){
System.out.println("你调用了"+point.getSignature().getName()+"方法");
}
}