JSP手动配置Struts+Hibernate+Spring要领及步骤
2个回答
展开全部
为什么要手动配置呢,现在不都是快速开发吗。
ssh框架整合实例子-配置文件
主要配置文件如下:
1.web.xml:
配置actionservlet类和映射
配置Listener,加载spring配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<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>
</web-app>
2. struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm"
type="com.yanek.demo.ssh.bean.UserForm">
</form-bean>
</form-beans>
<global-forwards>
<forward name="error" path="/error.jsp"></forward>
</global-forwards>
<action-mappings>
<!--
<action path="/user" name="userForm"
type="com.yanek.demo.ssh.action.UserAction"
scope="request" validate="true" input="/register.jsp">
<forward name="success" path="/reg_success.jsp" />
</action>
-->
<action path="/user" name="userForm"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request" validate="false" input="/register.jsp"
parameter="method">
<forward name="success" path="/reg_success.jsp" />
<forward name="list" path="/list.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
3. 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.0.xsd">
<!-- 定义数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<!-- 指定连接数据库驱动 -->
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<!-- 指定连接数据库url -->
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh"
/>
<!-- 指定连接数据库用户名,密码为空 -->
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<!-- 定义Hibernate的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定Hibernate映射文件 -->
<property name="mappingResources">
<list>
<value>com/yanek/demo/ssh/vo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 指定使用方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<!-- 是否在控制台输出sql语句 -->
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<!-- 定义事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事务管理拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义代理自动管理事务 -->
<bean id="ProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定需要Spring管理事务的Bean -->
<property name="beanNames">
<list>
<value>userService</value>
</list>
</property>
<!-- 调用事务管理拦截器 -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 把DAO注入给Session工厂 -->
<bean id="userDAO" class="com.yanek.demo.ssh.dao.UserDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 把Service注入给DAO -->
<bean id="userService"
class="com.yanek.demo.ssh.service.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<!-- 把Action注入给Service -->
<bean name="/user"
class="com.yanek.demo.ssh.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>
ssh框架整合实例子-配置文件
主要配置文件如下:
1.web.xml:
配置actionservlet类和映射
配置Listener,加载spring配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<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>
</web-app>
2. struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="userForm"
type="com.yanek.demo.ssh.bean.UserForm">
</form-bean>
</form-beans>
<global-forwards>
<forward name="error" path="/error.jsp"></forward>
</global-forwards>
<action-mappings>
<!--
<action path="/user" name="userForm"
type="com.yanek.demo.ssh.action.UserAction"
scope="request" validate="true" input="/register.jsp">
<forward name="success" path="/reg_success.jsp" />
</action>
-->
<action path="/user" name="userForm"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request" validate="false" input="/register.jsp"
parameter="method">
<forward name="success" path="/reg_success.jsp" />
<forward name="list" path="/list.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
3. 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.0.xsd">
<!-- 定义数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<!-- 指定连接数据库驱动 -->
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<!-- 指定连接数据库url -->
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh"
/>
<!-- 指定连接数据库用户名,密码为空 -->
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<!-- 定义Hibernate的sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定Hibernate映射文件 -->
<property name="mappingResources">
<list>
<value>com/yanek/demo/ssh/vo/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 指定使用方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<!-- 是否在控制台输出sql语句 -->
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<!-- 定义事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事务管理拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义代理自动管理事务 -->
<bean id="ProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定需要Spring管理事务的Bean -->
<property name="beanNames">
<list>
<value>userService</value>
</list>
</property>
<!-- 调用事务管理拦截器 -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 把DAO注入给Session工厂 -->
<bean id="userDAO" class="com.yanek.demo.ssh.dao.UserDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 把Service注入给DAO -->
<bean id="userService"
class="com.yanek.demo.ssh.service.UserServiceImpl">
<property name="userDAO" ref="userDAO"></property>
</bean>
<!-- 把Action注入给Service -->
<bean name="/user"
class="com.yanek.demo.ssh.action.UserAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>
展开全部
通过自己简单的学习,总结如下:
struts主要表现为显示层,有就是说你从数据库得到的数据,想把数据呈现给用户,那struts就用的上了
hibernate是主要和数据库打交道,它轻量级的封装了一些JDBC方法,是开发更加的快速,当然hibernate还包括很多东西,
spring我的理解就是个大箱子,里面装的东西就是JAVABEAN,而这些JAVABEAN统一由spring来管理,那spring是怎么管理的呢?是通过applicationContext.xml文件,将这些JAVABEAN都写到这个文件里,好让spring知道他需要管理哪些JAVABEAN,这些JAVABEAN是包括ACTION,DAO接口,DAOIMPL接口实现类.
使用这三个框架整合开发的好处我觉得是降低了代码之间的偶合性,而且编码也规范了,因为用表现层的框架的好处之一就是无论谁写的代码,看上去都差不多.
当然SSH整合开发不是把三个框架加到工程下就可以了,而是需要一定的配置,让三个毫无关系的框架整合到一起,让他们三个互相认识,如何让他们互相认识?就要通过配置一些XXXX.xml文件来让他们认识
applicationContext.xml,这个里面的内容是我自己写的一个小项目里的相关配置,我想不难理解吧,估计都能看懂,其他的也按照这个方法加配置就可以了!
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.autoconmmit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/xy/myblog/vo/User.hbm.xml</value>
<value>com/xy/myblog/vo/Article.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userdao" class="com.xy.myblog.dao.UserDao" abstract="true">
</bean>
<bean id="userdaoimpl" class="com.xy.myblog.daoimpl.UserDaoImpl" parent="userdao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean name="/jsp/user" class="com.xy.myblog.struts.action.UserAction">
<property name="userdao">
<ref bean="userdaoimpl"/>
</property>
</bean>
struts-config.xml,将我写的这两个内容添加进去就可以了
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
web.xml文件下添加如下内容
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.xy.myblog.util.EncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>opensession</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
添加好上述内容后,基本上SSH项目的整合就已经OK了,因为本人没有进过软件公司,所以很多东西可能都不规范就凑合看吧!
struts主要表现为显示层,有就是说你从数据库得到的数据,想把数据呈现给用户,那struts就用的上了
hibernate是主要和数据库打交道,它轻量级的封装了一些JDBC方法,是开发更加的快速,当然hibernate还包括很多东西,
spring我的理解就是个大箱子,里面装的东西就是JAVABEAN,而这些JAVABEAN统一由spring来管理,那spring是怎么管理的呢?是通过applicationContext.xml文件,将这些JAVABEAN都写到这个文件里,好让spring知道他需要管理哪些JAVABEAN,这些JAVABEAN是包括ACTION,DAO接口,DAOIMPL接口实现类.
使用这三个框架整合开发的好处我觉得是降低了代码之间的偶合性,而且编码也规范了,因为用表现层的框架的好处之一就是无论谁写的代码,看上去都差不多.
当然SSH整合开发不是把三个框架加到工程下就可以了,而是需要一定的配置,让三个毫无关系的框架整合到一起,让他们三个互相认识,如何让他们互相认识?就要通过配置一些XXXX.xml文件来让他们认识
applicationContext.xml,这个里面的内容是我自己写的一个小项目里的相关配置,我想不难理解吧,估计都能看懂,其他的也按照这个方法加配置就可以了!
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.autoconmmit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/xy/myblog/vo/User.hbm.xml</value>
<value>com/xy/myblog/vo/Article.hbm.xml</value></list>
</property></bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userdao" class="com.xy.myblog.dao.UserDao" abstract="true">
</bean>
<bean id="userdaoimpl" class="com.xy.myblog.daoimpl.UserDaoImpl" parent="userdao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean name="/jsp/user" class="com.xy.myblog.struts.action.UserAction">
<property name="userdao">
<ref bean="userdaoimpl"/>
</property>
</bean>
struts-config.xml,将我写的这两个内容添加进去就可以了
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
web.xml文件下添加如下内容
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.xy.myblog.util.EncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>opensession</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
添加好上述内容后,基本上SSH项目的整合就已经OK了,因为本人没有进过软件公司,所以很多东西可能都不规范就凑合看吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询