三大框架-ssh整合(三)
struts整合spring:spring创建action
1.编写action类,并将其配置给spring ,spring可以注入service
2.编写struts.xml
3.表单jsp页面
4.web.xml 配置
1.确定配置文件contextConfigLocation
2.配置监听器 ContextLoaderListener
3.配置前端控制器 StrutsPrepareAndExecuteFitler
action类如下:
import com.itheima.UserService.UserService; import com.itheima.domain.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User> { //1 封装数据 private User user = new User(); @Override public User getModel() { return user; } //2 service private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String register(){ userService.register(user); return "success"; } }
struts.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 开发模式 --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 底层自动从spring容器中通过名称获得内容, getBean("userAction") --> <action name="userAction_*" class="userAction" method="{1}"> <result name="success">/messag.jsp</result> </action> </package> </struts>
jsp页面:index.jsp(注册页面) messag.jsp(注册成功页面)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="${pageContext.request.contextPath}/userAction_register" method="post"> 用户名:<input type="text" name="username"/> <br/> 密码:<input type="password" name="password"/> <br/> 年龄:<input type="text" name="age"/> <br/> <input type="submit" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Insert title here</title> </head> <body> 注册成功 </body> </html>
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <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> <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>
其他的User类,UserDao类,UserService类,以及对应的数据库的表t_user与之前的整合(二)均是一模一样
applicationContext.xml文件中在前面整合hibernate的基础上只增加如下代码:即整合struts
<!-- 6 配置action --> <bean id="userAction" class="com.itheima.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean>
则最后的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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.1加载properties文件 --> <!-- 1.2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///ee19_spring_day03"></property> <property name="user" value="root"></property> <property name="password" value="123"></property> </bean> <!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory * configLocation确定配置文件位置 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 1)dataSource 数据源 2)hibernateProperties hibernate其他配置项 3) 导入映射文件 mappingLocations ,确定映射文件位置,需要“classpath:” ,支持通配符 【】 <property name="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> mappingResources ,加载执行映射文件,从src下开始 。不支持通配符* <property name="mappingResources" value="com/itheima/domain/User.hbm.xml"></property> mappingDirectoryLocations ,加载指定目录下的,所有配置文件 <property name="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property> mappingJarLocations , 从jar包中获得映射文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> </props> </property> <property name="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property> </bean> <!-- 3 dao --> <bean id="userDao" class="com.itheima.UserDao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 4 service --> <bean id="userService" class="com.itheima.UserService.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 5 事务管理 --> <!-- 5.1 事务管理器 :HibernateTransactionManager --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 5.2 事务详情 ,给ABC进行具体事务设置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="register"/> </tx:attributes> </tx:advice> <!-- 5.3 AOP编程,ABCD 筛选 ABC --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/> </aop:config> <!-- 6 配置action --> <bean id="userAction" class="com.itheima.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> </beans>