配置文件放置位置:
建立 SourceFolder,这样配置文件每次操作自动同步到src路径下/WEB-INF/classes 目录下.
web.xml配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</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>
注意事项:
参数contextConfigLocation可以读取任意的applicationContext-xxx.xml,不需要每次创建一个applicationContext.xml,都需要import一次
Spring配置文件applicationContext.xml
<!-- 注解事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="mappingLocations">
<value>classpath:com/bywangxp/invoice/**/vo/*Model.hbm.xml</value>
</property>
</bean>
<!-- DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myerp"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
注意事项:
参数mappingLocations参数,读取hibernate的全部映射文件classpath:com/bywangxp/invoice/**/vo/*Model.hbm.xml
关于classpath:表示的是WEBROOT下class文件的存放路径,因为src下的java程序都会编译成class文件保存在WEB-INF路径的classes路径下
hibernate的映射文件是和model放在同一个文件夹下的,自动保存到了classpath路径下
struts2的配置文件struts.xml
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor" class="com.bywangxp.invoice.util.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="invoiceStack">
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="invoiceStack"></default-interceptor-ref>
<global-results>
<result name='nologin'>/WEB-INF/jsps/login.jsp</result>
<result name="loginsuccess">/WEB-INF/jsps/login.jsp</result>
</global-results>
<action name='login'>
<result>/WEB-INF/jsps/login.jsp</result>
</action>
<action name='context'>
<result>/WEB-INF/jsps/context.jsp</result>
</action>
<action name="emp_*" class="com.bywangxp.invoice.emp.web.EmpAction" method='{1}'>
<result name='loginfail'>/WEB-INF/jsps/login.jsp</result>
<result name='loginsuccess'>/WEB-INF/jsps/main.jsp</result>
</action>
<action name="page_*" >
<result>/WEB-INF/jsps/{1}.jsp</result>
</action>
<action name="dep_*" class="com.bywangxp.invoice.dept.web.DepAction" method='{1}'>
<result name='list'>/WEB-INF/jsps/dept/list.jsp</result>
<result name='input'>/WEB-INF/jsps/dept/input.jsp</result>
</action>
</package>
注意事项:
<interceptors>
<interceptor name="LoginInterceptor" class="com.bywangxp.invoice.util.interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="invoiceStack">
<interceptor-ref name="LoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="invoiceStack"></default-interceptor-ref>
关于自定义拦截器
1.写一个类继承intercepor ,该类有三个需要实现的方法,但是我们需要的只是其中一个Inteceptor()方法,这时候有一个更合适的AbstractInteceptor,
该类只有一个未实现的方法,我们拦截的规则就放在这里
另外actionInvocation 提供的方法可以获取到action和proxy对象,通过action可以到当前访问的方法,以及action的完整名字,可以针对具体的方法做不同的操作
方法放行 invocation.invoke();
2.让拦截规则有效,默认的defaultStack有独特的功能,帮我们把页面的数据传递过来,如果没有这个默认的拦截器stack,我们从页面传递过来的数据就丢失了,
所以在拦截规则上一定要带上它!!!!
关于页面传参到action中并封装到对象中:
在action中 设置public 修饰一个实例化对象 public EmpModel em=new EmpModel;
页面配置如下:
<s:form action='emp_x' method='post'>
<s:textfield name='em.name' size='20'>
<s:password name='em.pwd' size='20'>
</s:form>