`
谢永兵
  • 浏览: 14832 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

三大框架综合

    博客分类:
  • J2EE
阅读更多
      Struts  +  Spring
首先我们先讨论action由谁来管理?
在struts中action是由requestprocessor来创建。

第一:由struts来管理action
第二:由spring中的ioc容器来创建

无论那种情况都要对IOC容器进行实例化,因为你在创建的时候肯定与业务对象进行交互,
而业务对象是有spring中的IOC容器来管理的

那么如何实例化IOC容器?
就是要加载一个配置文件,而这个配置文件加载对象,加载对象之间的描述
两种方式实例化:
第一:利用ServletContextListener实现
                第二:servlet加载来实现ContextLoaderServlet

在web-xml中配置:
<!-- 配置全局初始化参数 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/spring.xml</param-value>
</context-param>
<!-- 监听器ContextLoaderListener 监听容器 它是实现了ServletContextListener接口-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
这个<param-name>中contextConfigLocation 一定要写正确,因为后面的一个监听器,会读取这个参数,如果没有写正确。默认为spring配置文件,即:applicationContext.xml配置文件


Action控制权的两种方式
第一种:控制器Action显示定位Spring工厂,也就是Spring的容器ApplicationContext实例。并从中获取业务逻辑组件的引用。但是Action的创建并管理仍然由Struts的RequestProcessor创建并管理。Spring无法控制和管理Action

第二种:Spring管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件

实例:

如果我的action没有交给spring去管理时,我们使用第一种方式,即手工创建
ACTION类:
public class PetLoginAction extends Action{

private IUserService iuserService;

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();

PetLoginForm petlogin = (PetLoginForm)form;
String petname = petlogin.getPetname();
String password = petlogin.getPassword();
//获取一个IOC容器,并获取一个bean
iuserService = (IUserService) WebApplicationContextUtils.getWebApplicationContext(request.getSession()
                   .getServletContext()).getBean("userService");
boolean b = this.iuserService.validate(petname, password);
    if(b){
    return mapping.findForward("success");
    }else{
    return mapping.findForward("fail");
    }
}

}


IuserService接口:
public interface IUserService {
public boolean validate(String name,String password);
}


IuserServiceImpl实现类:
public class IUserServiceImpl implements IUserService {

public boolean validate(String name, String password) {
if (name.equals("pig") && password.equals("123")) {
return true;
} else {
return false;
}
}

}


PetLoginForm类:
public class PetLoginForm extends ActionForm {

private String petname;

private String password;

public String getPetname() {
return petname;
}

public void setPetname(String petname) {
this.petname = petname;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}


Spring.xml配置文件:
<beans>
<bean id="userService" class="com.**.bo.IUserServiceImpl"></bean>
</beans>


struts-config.xml配置文件:
<form-beans>
    <form-bean name="petloginform" type="com.**.form.PetLoginForm"></form-bean>
    <action-mappings>
    <action
           path="/petlogin"
           type="com.**.action.PetLoginAction"
           name="petloginform"
           validate="true"
           input="/**.jsp"
    >
    <forward name="success" path="/**.jsp"></forward>
    <forward name="fail" path="/**.jsp"></forward>
    </action>
</action-mappings>


在实际中不建议使用。

缺点: 和spring高度的耦合。Action的创建由Struts的中央控制器的请求处理器负责。

优点:struts开发者改变很小。接近原始的






第二种方式:Spring管理Action:
Struts中的五个扩展点:Action、ActioForm、RequestProcessor、plug-in
                       Processor

DelegatingRequestProcessor 继承RequestProcessor
获得请求路径,获得ioc容器,获得bean

实例:

ACTION类:
public class PetLoginAction extends Action{

private IUserService iuserService;

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

PetLoginForm petlogin = (PetLoginForm)form;
String petname = petlogin.getPetname();
String password = petlogin.getPassword();
System.out.println(petname+"----------------"+password);
boolean b = this.iuserService.validate(petname, password);
    if(b){
    return mapping.findForward("success");
    }else{
    return mapping.findForward("fail");
    }
}

public void setIuserService(IUserService iuserService) {
this.iuserService = iuserService;
}

}

其它的几个类跟上面一样。 我们需要改变一下配置文件

Spring.xml配置文件:
<beans>
<bean name="/petlogin" class="com.lovo.action.PetLoginAction">
  <property name="iuserService" ref="userService"></property>
</bean>
<bean id="userService" class="com.lovo.bo.IUserServiceImpl"></bean>
</beans>

struts-config.xml配置文件:
这需要添加一个
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
一切OK!!





Hibernate + spring + struts = ssh 综合演练
只列出自认为重要的部分,其它看看书就OK!

spring 与struts结合代码与上同:

hibernate部分代码:
IuserServiceImpl 实现类:
public class IUserServiceImpl implements IUserService {

private UserDao userDao;

public boolean validate(String name, String password) {
return this.userDao.checkUser(name, password);
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

}


Userdao 接口:
public interface UserDao {

public boolean checkUser(String name,String password);

}


UserDaoImpl 实现类:
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

public boolean checkUser(final String name, final String password) {
  Student student = (Student)this.getHibernateTemplate().execute(new HibernateCallback(){

public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery("from Student u where u.petname =:name and u.password =:password")
        .setParameter("name", name)
        .setParameter("password", password)
        .uniqueResult();
}});
  if(student!=null){
  return true;
  }else{
  return false;
  }
}




}


配置文件:

Spring.xml:
<beans>
<bean name="/petlogin" class="com.lovo.action.PetLoginAction">
  <property name="iuserService" ref="userService"></property>
</bean>
<bean id="userService" class="com.lovo.bo.IUserServiceImpl">
  <property name="userDao" ref="userDao"></property>
</bean>

<bean id="userDao" class="com.lovo.bo.UserDaoImpl">
  <property name="hibernateTemplate" ref="template"></property>
</bean>

<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref=""></property>
</bean>

<bean id="sfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
</beans>



Ok!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics