以下web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
[color=red] <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>[/color]</web-app>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以下applicationContext.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.5.xsd">
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:org/teddy/dao/sqlMapConfig.xml</value>
</property>
</bean>
<bean id="testDao" class="org.teddy.dao.TestUserDaoImpl">
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="loginTest" class="org.teddy.util.LoginTest"/>
</beans>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 总的来讲配置的时候主要发生的错误在于applicationContext中的sqlmapClient上。注意其中的value值,个人建议写成以上这样,classpath:后跟包名……文件名。如果直接在src下就直接写文件名就可以了。如果这个配置不正确在启动web服务器初始化容器的时候就会报错,告诉你ioFileNoFound。就是因为你的路径不对。
至于在struts的action中取得Spring的bean对象,要用到:
BeanFactory factory=
WebApplicationContextUtils.
getRequiredWebApplicationContext
(request.getSession().getServletContext());
然后你就可以调用控制层(util)的方法了。
在控制层访问持久层的方法(Dao层)时,Dao层需要继承org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
如果是hibernate的话就继承 org.springframework.orm.hibernate3.support.HibernateDaoSupport
然后获得对应模板然后就能获得对应的sqlMapClient(session)来对数据库进行操作了。
个人能力有限在学习中只了解了个大概……
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
[color=red] <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>[/color]</web-app>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
以下applicationContext.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.5.xsd">
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:org/teddy/dao/sqlMapConfig.xml</value>
</property>
</bean>
<bean id="testDao" class="org.teddy.dao.TestUserDaoImpl">
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="loginTest" class="org.teddy.util.LoginTest"/>
</beans>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 总的来讲配置的时候主要发生的错误在于applicationContext中的sqlmapClient上。注意其中的value值,个人建议写成以上这样,classpath:后跟包名……文件名。如果直接在src下就直接写文件名就可以了。如果这个配置不正确在启动web服务器初始化容器的时候就会报错,告诉你ioFileNoFound。就是因为你的路径不对。
至于在struts的action中取得Spring的bean对象,要用到:
BeanFactory factory=
WebApplicationContextUtils.
getRequiredWebApplicationContext
(request.getSession().getServletContext());
然后你就可以调用控制层(util)的方法了。
在控制层访问持久层的方法(Dao层)时,Dao层需要继承org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
如果是hibernate的话就继承 org.springframework.orm.hibernate3.support.HibernateDaoSupport
然后获得对应模板然后就能获得对应的sqlMapClient(session)来对数据库进行操作了。
个人能力有限在学习中只了解了个大概……