`
wangliya110
  • 浏览: 15641 次
  • 性别: Icon_minigender_2
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

三大框架复习

阅读更多
Struts
1.写web.xml配置文件,当服务器启动时,会先读取web.xml配置文件,此时加载ActionServlet类,这个类是Struts的入口。以.do结尾的URL都会被映射到ActionServlet中,它根据struts-config.xml中的配置,将请求分配到指定的action上。运行代码在不同的action实现。
<servlet>
<servlet-name>actionservlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>//用到struts时就必须写加载struts-config.xml配置文件
</servlet>
<servlet-mapping>
<servlet-name> actionservlet </servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.写用到的JavaBean类
3.写一个formbean的类 让它继承actionform类
4.写个stuts-config.xml文件  写在web-info下面
5.让写的action类继承Action或dispatchedAction
<struts-config>
<form-beans>
<form-bean name="fileform" type="com.fit.struts.formfile.Form" />
</form-beans>
<global-forwards>
<forward name="tolist" path="/WEB-INF/jsp/EmpList.jsp" />
</global-forwards>
<action-mappings>
<action path="/fileformaction" type="com.fit.struts.action.FileFormAction"
scope="request" name="fileform" parameter="m">
<forward name="success" path="/WEB-INF/page/Seccess.jsp" />
</action>
</action-mappings>
  </form-beans>
</struts-config>
当struts-config.xml文件被加载后:
   1.actionservlet将按配置文件,将提交的表单信息添加到form-bean中,form-bean中的属性必须和表单中的一致,可以是一个实体类。
   2.actionservlet按照配置文件,将请求分配到指定的action上。 parameter是用在action类继承dispatchaction里

Hibernate
1.在javabean类中写一个将javabean和表连接的配置文件 employee.hbm.xml
<hibernate-mapping  package="com.fit.entity">
<class name="Employee" table="employee">
<id name="email">//主键
<generator class="assigned"></generator>
</id>
<property name="ename" />
<property name="idcard" />
<property name="gender" />
<property name="birthday" />
<property name="address" />
<property name="mobile" />
<property name="hometel" column="home_tel" type=”String”/>
<property name="fax" />
<property name="intro" />
<property name="esource" />
<property name="photo" />
</class>
</hibernate-mapping>
2.在src根目录下建个hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:FITDEMO
</property>
<property name="connection.username">zxl</property>
<property name="connection.password">1988311</property>
<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<mapping resource="com/fit/entity/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
在给文件中 配置数据库连接信息 配置方言
<mapping resource="com/fit/entity/Employee.hbm.xml" /> 该配置是映射Employee.hbm.xml
3.建个HibernateUtil的工具类 通过sessionfactory的到session类
public class HibernateUtil {
private static final SessionFactory factory;
static {
factory = new Configuration().configure().buildSessionFactory();
}
public static Session getCurrentSession() {
return factory.getCurrentSession();
}
}
4.在过滤器中创建事物并提交
5.修改dao层中的类 session.
Spring+Hibernate
1.写application.xml的配置文件  写在src的根目录下  将连接数据库 得到sessionfactory类
<beans>
<bean id="data"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
//得到DriverManagerDataSource类
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:FITDEMO" />
<property name="username" value="cui" />
<property name="password" value="cui" />
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="data" />//得到sessionfactory类
<property name="mappingResources">
<list>
<value>com/_11fit/struts/bean/Employee.hbm.xml</value>
//连接Employee.hbm.xml
</list>
</property>
</bean>
<bean id="employeeDao" class="com._11fit.struts.dao.impl.EmployeeDAOHibernateImpl">
<property name="sessionFactory" ref="factory" />//得到一个dao类
</bean>
</beans>
2.dao层中连接数据库的类要继承HibernateDaoSupport接口
3.修改dao类   用getHibernateTemplate()模板
  getHibernateTemplate().delete(e) 删除
  getHibernateTemplate().loadAll(Employee.class) 得到全部信息
  getHibernateTemplate().get(Employee.class, email) 得到一条信息
  getHibernateTemplate().save(emp) 提交 保存信息
  getHibernateTemplate().update(emp) 修改
4.修改action类 通过ApplicationContext来得到bean
private ApplicationContext context = new ClassPathXmlApplicationContext(
"application.xml");//找到application.xml文件
private EmployeeDao edao = (EmployeeDao) context.getBean("employeeDao");
//employeeDao与配置文件中的 红字相同 得到一个employeeDao类的实例
Spring+Struts(方法一 代理bean)
1. 修改struts-config.xml 加一个托管的插件(加),加载文件时,先找代理的action bean
通过代理bean 找到真正的action
   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />//关联applicationContext.xml
</plug-in>
2. 修改applicationContext.xml的配置文件(加)
   <bean name="/emp"  class="com._fit.struts.action.EmpAction">
<property name="edao" ref="employeeDao" />
</bean>//将真正的action放到IoC容器中
3. 将action中的type改了
   <action path="/emp" type="org.springframework.web.struts.DelegatingActionProxy"
name="newform" scope="request" parameter="m">
<forward name="false" path="/WEB-INF/jsp/False.jsp" />
<forward name="updataemp" path="/WEB-INF/jsp/Updata.jsp" />
</action>
4. 修改action类 通过依赖注入的参数注入方法
  private EmployeeDAO edao;//定义的edao要和applicationContext.xml中的bean 配置相同
                           (红字)
public void setEdao(EmployeeDAO dao) {
edao = dao;
}
Spring+Struts(方法一继承ActionSupport)
1. 注册ServletContext监听器 在web.xml中添加 得到ContextLoaderListener类
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2. action类继承ActionSupport类(获DispatchActionSupport类)
3. 在Action内部获得ApplicationContext,查找所依赖的Bean
ApplicationContext context = getWebApplicationContext();
EmployeeDAO edao = (EmployeeDAO) context.getBean("employeeDao");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics