web development

csdn第一篇文章。。。以后记得天天总结。。。。。

。又是一个大作业。。。学了点。。用老大的话来说“毫无技术含量的东西” ----ssh

做的又是这个白痴“学生选课系统”--:(。。。。。当然,做的时候发现自己就是个白痴。。。。。

这个选课系统用的是ssh。。。struts(读的时候别加k了!!)+spring+hibernate+ajax。。。也算第一次对mvc编程有了一定的概念。。。。。。。。

感觉这个些framework的关键点都在于其配置文件里。。。。

下面是文件结构

web.xml& nbsp;  configurate&initialize  struts  spring   ,注意sturts2由filter实现拦截功能。。。

 

代码
<filter>
<description>define the filerdispatcher of struts2</description>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern> //这里是拦截所有文件。。可以改成*.action拦截action
</filter-mapping>
<listener>
<description>initialize spring container</description> //初始话spring容器
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

 

 

applicationContext.xml      配置spring。。。由于是有spring实现和hibernate的交互。。这里用spring自动管理hibernate的sessionFactory 。。。。。然后就不要管了。。when some Dao need access persistent object,spring会自动将sessionFactory注入DAO组件。

 

代码
<bean id="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value
="com.mysql.jdbc.Driver">
</property>
<property name="url"
value
="jdbc:mysql://localhost/student_manage_system">
</property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property> //设置sessionFactory的几个properties
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources"> //载入hibernate映射。。
<list>
<value>po/StudentTable.hbm.xml</value>
<value>po/CourseTable.hbm.xml</value>
<value>po/ChoseTable.hbm.xml</value>
<value>po/TeacherTable.hbm.xml</value></list>
</property></bean>

 

 

 


看下StudentTable.hbm.xml

 

代码
<hibernate-mapping>
<class name="po.StudentTable" table="student_table" catalog="student_manage_system">
<id name="SId" type="java.lang.String">
<column name="s_id" length="10" />
<generator class="assigned" />
</id>
<property name="SName" type="java.lang.String">
<column name="s_name" length="45" />
</property>
//这里若干property
//
<set name="choseTables" inverse="true"> //这里设置关联table
<key>
<column name="ch_s_id" length="10" not-null="true" unique="true" />
</key>
<one-to-many class="po.ChoseTable" />
</set>
</class>
</hibernate-mapping>

 


好。。。。回到spring 的配置文件。。下面配置Dao组件。。。。sessionFactory是hibernate的东西。。用spring注入DAO

 

color: #0000ff;"><bean id="StudentTableDAO" class="po.StudentTableDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

 

 

下面该用spring 配置业务逻辑组件了。。这里我设置了一个StudentServceImp的逻辑组件

 

代码
<bean id="adm" class="service.AdminServiceImp"
abstract
="false" lazy-init="default" autowire="default"
dependency-check
="default">
<property name="studentDao" > //这里是用到的Dao组件
<ref bean ="StudentTableDAO" />
</property>

 



下面这段羊驼玩意目前也没完全搞懂。。。让spring自动进行事务控制

 

代码
<bean id="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
abstract
="false" lazy-init="default" autowire="default"
dependency-check
="default">
<property name="sessionFactory">
<ref bean="sessionFactory" /> //sessionFactory引用,在初始化和datasource连接的那里定义的
</property>
</bean>
<bean id="transactionInterceptor"
class
="org.springframework.transaction.interceptor.TransactionInterceptor"
abstract
="false" lazy-init="default" autowire="default"
dependency-check
="default"> //配置拦截器。。。。
<property name="transactionManager"> //拦截时先注入刚才定义的事务管理器
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes"> //定义事务传播属性
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property></bean>
<bean id="beanNameAotoProxyCreator"
class
="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
abstract
="false" lazy-init="default" autowire="default"
dependency-check
="default">
<property name="beanNames">
<list>
<value>adm</value> //id满足adm的bean自动生成业务代理。。。
<value>stu</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value> //刚才定义的拦截器
</list>
</property>
</bean>

 

 

下面用struts.xml

 

代码
<package name="struts2" extends="struts-default"> //先加个package。。。不能重名//extends内容用,隔开
<action name="SearchStu" class="struts.SearchStuAction"> //添加action及其使用的类,SearchStuAction返回一个字符传
<result name="success">/student.jsp</result> //如果返回值为“SUCCESS”就。。。
</action>
</package>

 

 

 


 

这就是其基本配置。。。过两天再谈下ajax的使用(用到了json)需要引入一些标准的js函数库。。

posted @ 2010-04-17 22:56  love && peace  阅读(159)  评论(0编辑  收藏  举报