s2sh的配置

一、准备需要的jar包。一下是用到的jar包截图

对于每个jar包的作用可以参考http://blog.163.com/zzf_fly/blog/static/2095891582012112924734742/

http://blog.sina.com.cn/s/blog_6a4af8630101diwb.html

2.在myeclipse中添加web project

右击项目名称

分别选择Add struts ..,Add Spring..,Add hibernate,添加他们的支持。

由于myeclipse的版本的问题,我的不能通过这种方式添加struts2,于是我直接把jar包复制到lib文件中。

在myeclipse中连接mysql数据库。在myeclipse中选择window-show view-other-myeclipse Database-DEBrowser。

右击DB Browser视图的空白处,选择new

 

如图填写user name mysql的名字,密码设置的空。

点击Add JARs添加必要包,有mysql-connector-java-5.0.4-bin.jar和sqljdbc.jar包

 为了生成hibernate.cfg.xml文件,需要右击项目名称选择myEclipse-Add Hibernate Capabilities选项,增加hibernate支持

next,next后

db driver选择刚刚创建连接时命名的名字。

然后next,去掉一个选项

接着是生成实体类文件:

右击数据库的一张表选择

选择hibernate reverse Engineering,next后选择对应的目录

然后狂点next

然后就能看见生成的.java和。hbm.xml两个文件。

然后是配置SSH的配置文件。

初步配置applicationContext.xml

在src下新建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="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml" />
    </bean>
<!-- 定义事务管理器(申明式的事务)) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
<!-- 所有bean共享一个代理 -->
<bean id="transactionBase" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
            lazy-init="true" abstract="true"> 
        <!-- 配置事务管理器 --> 
        <property name="transactionManager" ref="transactionManager" /> 
        <!-- 配置事务属性 --> 
        <property name="transactionAttributes"> 
            <props> 
                <prop key="*">PROPAGATION_REQUIRED</prop> 
            </props> 
        </property> 
    </bean>  
    </beans>  

初步配置struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>

配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <!-- 在Web服务器启动时就加载Spring的核心配置文件 -->
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
        </context-param> -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext-*.xml,classpath:applicationContext*.xml
        </param-value>
    </context-param>

    <!-- spring ApplicationContext的载入 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>


    <!-- 配置管理Hibernate Session的过滤器 最终目的是为了解决hibernate懒加载的问题-->
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>

        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
    <!-- 当session设置为FlushMode.AUTO时,hibernate在进行查询的时候会判断缓存中的数据是否为脏数据,是则刷数据库,
    不是则不刷,而always是直接刷新,不进行任何判断。很显然auto比always要高效得多 -->



    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <filter-name>Struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

其中index.jsp文件为进入的初始界面

然后编写代码和jsp文件,实现各个功能和页面。
编写完后完整的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="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml" />
    </bean>
<!-- 定义事务管理器(申明式的事务)) -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
<!-- 所有bean共享一个代理 -->
<bean id="transactionBase" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
            lazy-init="true" abstract="true"> 
        <!-- 配置事务管理器 --> 
        <property name="transactionManager" ref="transactionManager" /> 
        <!-- 配置事务属性 --> 
        <property name="transactionAttributes"> 
            <props> 
                <prop key="*">PROPAGATION_REQUIRED</prop> 
            </props> 
        </property> 
    </bean>  
    
    <bean id="studentDaoxx" parent="transactionBase" > 
        <property name="target" ref="studentDao" />  
    </bean>
    
<!-- 配置dao -->
    <bean id="studentDao" class="com.dao.StudentDAOImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    
    <bean id="stuService" class="com.service.studentServiceImpl">
        <property name="studentDao">
            <ref bean="studentDao" />
        </property>
    </bean>
    
    <bean id="ListAction"
        class="com.action.ListAction" scope="prototype">
        <property name="stuService">
            <ref bean="stuService" />
        </property>
    </bean>

    
    
    
</beans>

 完整的struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <package name="student" extends="struts-default">
  
  
  <action name="listUser" class="ListAction">
   <result name="success">/listStudent.jsp</result>
  </action>
  
  
 </package>
</struts>


简单说明下当struts中执行action为listuser的动作时,会到applicationContext.xml中在这个action动作的代码位置com.action.ListAction",并执行这个方法,成功后显示listStudent.jsp界面。大一点来说<result name="success">/listStudent.jsp</result>包含了2个动作,一是把学生从数据库中找出来,二是把找出来的数据显示到

 listStudent.jsp上。

写完后吧项目附加到tomcat中,启动运行。http://localhost:8080/test46

对于启动tomcat过程中遇到的异常,正在整理中。

原文地址:https://www.cnblogs.com/yanmingup/p/3010480.html