ssh框架搭建(提供可下载源码)
1*源码下载地址:https://pan.baidu.com/s/1qZo8ROK
FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6、2.1.8。StrutsPrepareAndExecuteFilter名字已经很能说明问题了,prepare与execute,前者表示准备,可以说是指filter中的init方法,即配制的导入;后者表示进行过滤,指doFilter方法,即将request请求,转发给对应的 action去处理。
FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.!
StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的.!
这样的改革当然是有好处的.!
为什么这么说.? 应该知道如果我们自己定义过滤器的话, 是要放在strtus2的过滤器之前的, 如果放在struts2过滤器之后,你自己的过滤器对action的过滤作用就废了,不会有效!除非你是访问jsp/html!
那我现在有需求, 我必须使用Action的环境,而又想在执行action之前拿filter做一些事, 用FilterDispatcher是做不到的.!
那么StrutsPrepareAndExecuteFilter可以把他拆分成StrutsPrepareFilter和StrutsExecuteFilter,可以在这两个过滤器之间加上我们自己的过滤器.!
给你打个比喻, 现在有病人要做手术, 现在struts2要做两件事, 搭病床(环境),执行手术.! 那么打麻药的工作呢.? 不可能要病人站着打吧, 所以必须有病床的环境,打完麻药之后再动手术.! 这个比喻非常形象了.!
如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher,
否则,用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
从Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能。
三个初始化参数:
1、config参数:指定要加载的配置文件。逗号分割。
2、actionPackages参数:指定Action类所在的包空间。逗号分割。
3、configProviders参数:自定义配置文件提供者,需要实现ConfigurationProvider接口类。逗号分割。
2*配置文件详解
项目结构图

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
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> <package name="s1" namespace="/zy" extends="struts-default"> <action name="registe" class="com.zy.controller.UserController" method="registe"> <result name="success">/index.jsp</result> </action> </package> </struts>
spring.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1.配置组件扫描器 --> <context:component-scan base-package="com.zy"></context:component-scan> <!-- 2.配置连接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 四大组件 --> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_test" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="maxPoolSize" value="10" /> <property name="minPoolSize" value="2" /> </bean> <!-- 3.配置sessionFactory工厂 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 引用上边的数据源 --> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext </value> </property> <!-- 4.配置映射 --> <property name="mappingResources"> <list> <value>mapping/user.hbm.xml</value> </list> </property> </bean> <!-- 5.配置事务管理 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <!-- 引用sessionFactory --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 6.配置AOP --> <aop:config> <!-- 配置切点 --> <aop:pointcut expression="execution(public * com.zy.service.impl.*.*(..))" id="myPointCut" /> <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" /> </aop:config> <!-- 7.建议 --> <tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true" /> <tx:method name="save*" /> <tx:method name="delete*" /> <tx:method name="update*" /> </tx:attributes> </tx:advice> </beans>
user.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zy.entity"> <class name="User" table="user"> <!-- 二级缓存的读写 --> <!-- <cache usage="read-write" /> --> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" column="name" /> <property name="age" column="age" /> <property name="address" column="address" /> </class> </hibernate-mapping>
3*主体代码详解
控制层
UserController
package com.zy.controller; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import com.zy.entity.User; import com.zy.service.UserService; //不需要注解controller需要添加一个jar包struts2-spring-p l u g i n-2.3.16.3.jar @SuppressWarnings("serial") public class UserController extends ActionSupport { @Autowired private UserService uService; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //********** public String registe() { uService.saveUser(user); return "success"; } }
服务层
userserviceimpl
package com.zy.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.zy.dao.UserDao; import com.zy.entity.User; import com.zy.service.UserService; @Component public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao ; @Override public void saveUser(User user) { userDao.registe(user); } }
数据库交互层
UserDaoImpl
package com.zy.dao.impl; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.zy.dao.UserDao; import com.zy.entity.User; @Component // 交给spring管理 public class UserDaoImpl implements UserDao { // 需要sessionFactory @Autowired private SessionFactory sf;// spring.xml配置过 @Override public void registe(User user) { // 事务在spring.xml中配置过 Session session = sf.getCurrentSession(); session.save(user); } }
详情参看源代码

浙公网安备 33010602011771号