package ssh.action;
import java.text.DecimalFormat;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.dao.ModelDrivenBaseAction;
import ssh.entity.BookCard;
import ssh.service.IndexService;
//创建IndexAction(action类)继承ActionSupport接口
public class IndexAction extends ModelDrivenBaseAction<BookCard> {
private static final long serialVersionUID = 1L;
//声明service,但不给它创建具体的实现类的实例,
//编写execute()方法
public String execute() {
//获取IndexService实例,调用getAllBookCard()方法
//将结果保存到List集合里
System.out.println("IndexAction1-------");
List<BookCard> myBookCardList = is.getAll();
System.out.println("IndexAction2-------");
//将查询出来的结构集打印到控制台
System.out.println("结果集:"+myBookCardList.size());
//获取Context上下文对象
ActionContext ac = ActionContext.getContext();
//将myBookCardList集合添加到上下文对象里
ac.put("myBookCardList", myBookCardList);
//返回一个字符串
return "success";
}
}package ssh.action;
import java.text.DecimalFormat;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.dao.ModelDrivenBaseAction;
import ssh.entity.BookCard;
import ssh.service.IndexService;
//创建IndexAction(action类)继承ActionSupport接口
public class IndexAction extends ModelDrivenBaseAction<BookCard> {
private static final long serialVersionUID = 1L;
//声明service,但不给它创建具体的实现类的实例,
//编写execute()方法
public String execute() {
//获取IndexService实例,调用getAllBookCard()方法
//将结果保存到List集合里
System.out.println("IndexAction1-------");
List<BookCard> myBookCardList = is.getAll();
System.out.println("IndexAction2-------");
//将查询出来的结构集打印到控制台
System.out.println("结果集:"+myBookCardList.size());
//获取Context上下文对象
ActionContext ac = ActionContext.getContext();
//将myBookCardList集合添加到上下文对象里
ac.put("myBookCardList", myBookCardList);
//返回一个字符串
return "success";
}
}
package ssh.dao;
import javax.annotation.Resource;
import ssh.service.IndexService;
public class BaseAction {
protected IndexService is;
public IndexService getIs() {
return is;
}
public void setIs(IndexService is) {
this.is = is;
}
}
package ssh.dao;
import java.util.List;
import ssh.entity.BookCard;
//创建IndexDao(接口类)
public interface IndexDao<T> {
public List<T> getAll();
}
package ssh.dao;
import java.util.List;
import ssh.entity.BookCard;
//创建IndexDao(接口类)
public interface IndexDao<T> {
public List<T> getAll();
}
package ssh.dao;
import ssh.util.GenericsUtils;
import com.opensymphony.xwork2.ModelDriven;
public class ModelDrivenBaseAction<T> extends BaseAction implements ModelDriven<T> {
// ===================== 对ModelDriven的支持 ====================
// 收集页面数据,对象
protected T model;
public ModelDrivenBaseAction() {
try {
System.out.println("————————>ModelDrivenBaseAction");
// 通过反射获取T的真是类型
//ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
//Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
Class<T> clazz =GenericsUtils.getSuperClassGenricType(getClass(), 0);
model = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public T getModel() {
return model;
}
}
package ssh.service;
import java.util.List;
import ssh.dao.IndexDao;
import ssh.entity.BookCard;
//创建一个IndexService接口类
public interface IndexService extends IndexDao<BookCard>{
}
package ssh.service;
import java.util.List;
import org.hibernate.SessionFactory;
import ssh.dao.IndexDao;
import ssh.dao.IndexDaoImpl;
import ssh.entity.BookCard;
//创建IndexServiceImpl(实现类)实现IndexService接口
public class IndexServiceImpl extends IndexDaoImpl<BookCard> implements IndexService {
//dao实例使用注入方式
private IndexDao<BookCard> id;
//用于注入使用
public void setId(IndexDao<BookCard> id) {
System.out.println("IndexServiceImpl"+this.getClass());
this.id = id;
}
}
package ssh.util;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericsUtils {
/**
* 通过反射,获得定义Class时声明的父类的范型参数的类型.
* 如public BookManager extends GenricManager<Book>
*
* @param clazz The class to introspect
* @return the first generic declaration, or <code>Object.class</code> if cannot be determined
*/
public static Class getSuperClassGenricType(Class clazz) {
return getSuperClassGenricType(clazz, 0);
}
/**
* 通过反射,获得定义Class时声明的父类的范型参数的类型.
* 如public BookManager extends GenricManager<Book>
*
* @param clazz clazz The class to introspect
* @param index the Index of the generic ddeclaration,start from 0.
*/
public static Class getSuperClassGenricType(Class clazz, int index) throws IndexOutOfBoundsException {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
return Object.class;
}
if (!(params[index] instanceof Class)) {
return Object.class;
}
return (Class) params[index];
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 -->
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<!-- setIs(myIndexService) -->
<property name="is" ref="myIndexService"/>
</bean>
<!-- myIndexService = new ssh.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean>
<bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<!-- 把sessionFactory 注入给IndexDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://121.42.167.134:3306/test?useUnicode=true</property>
<property name="connection.username">root</property>
<property name="connection.password">tianqi110</property>
<!-- 每个数据库都有1个,针对特定的关系型数据库生成优化的SQL -->
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<!-- 设置默认的数据库连接池 -->
<property name="connection.pool_size">5</property>
<!-- 显示SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">true</property>
<!-- 根据schema更新数据表的工具 -->
<property name="hbm2ddl.auto">update</property>
<!-- 数据表映射配置文件 -->
<mapping resource="ssh/entity/BookCard.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?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">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 第1步:先定义一个包 -->
<package name="mypck001" extends="struts-default">
<!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet")
http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute
class="ssh.action.IndexAction" ** new ssh.action.IndexAction()
设计思想:关心了具体的实现类必须改为不要关注那个实现类 加入spring后,struts的action节点的class属性意义发生变化,直接引用spring帮忙创建的实例
-->
<action name="Index" class="myIndexAction">
<!-- 跳转是forward/WEB-INF/是防止jsp不经过action就可以访问-->
<!-- result接收返回的字符串,然后做对应的事情 -->
<result name="success">/WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>