Struts+Spring+hibernate,主要有四个配置文件:xxx.hbm,xml;applicationcontext.xml;web.xml;struts-config.xml。
Struts配置文件struts-config.xml:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" ></controller>
这一句没有会产生The server encountered an internal error (No action instance for path /manager could be created) that prevented it from fulfilling this request异常,且该异常在tomcat logs 里没有任何记录。
<!---------------------------配置Spring框架中的applicationContext.xml--------------------------------------->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
顺序不能错,否则会出问题;
Spring配置文件applicationcontext.xml:
<beans>
<!-------------------------数据库连接取得---------------------------------->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:oradb01</value>
</property>
<property name="username">
<value>flsp</value>
</property> 数据库名要正确
<property name="password">
<value>flsp</value>
</property>
</bean>
<!-----------------------Spring支持hibernate框架的配置,得到SessionFactory------------------------------>
<bean id="localSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/domain/ManagerForm.hbm.xml</value>
</list>
</property>
</bean>
<bean id="objectDao" class="com.dao.ObjectDao">
<property name="sessionFactory">
<ref bean="localSessionFactory" />
</property>
</bean>
<!----------------------------------Action类的依赖注入关系----------------------------->
<bean name="/manager" class="com.action.ManagerAction" singleton="false">
<property name="objectDao">
<ref bean="objectDao" />
</property>
</bean>
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value> 配置Spring映射
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<filter> 过滤器,不设置的话Java文件里的中文都是乱码
<filter-name>Set Character Encoding</filter-name>
<filter-class>
com.power.filters.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
xxx.hbm.xml:数据库表映射文件
<id name="id" type="java.lang.String"> 主键生成 Oracle数据库用sequence要在数据库建立序列
<column name="id" />
<generator class="sequence">
<param name="sequence">tb_cj_sequence</param>
</generator>
</id>