刚来公司不久,由于公司项目是买了的,现在不想受他们的限制,所以需要重新打一个框架了,废话不多说了 直接上代码吧
首先struts2 -----2.3.28
spring3------3.2.6
所有集成的包,仅做参考。
首先还配置web.xml吧
<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>
<!-- 添加spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
注意的是吧资源文件放在classpath下 他会找到的
看下我的项目结构
首先配置struts 文件
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!<span style="color:#ff0000;">-- 将action托管给spring </span>-->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="user" class="com.core.database.action.UserAction">
<!-- 定义逻辑视图和物理资源之间的映射 -->
<result name="success">/WEB-INF/jsp/a.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
这里只是简单的配置了一下
接下来就是数据操作了 建库 建表 我这里只有一个表 简单案列嘛 你懂得
user id name
然后就是spring 配置了(重点)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc-mysql.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="txManager" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="save*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="edit*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="del*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="handle*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="notify*">PROPAGATION_REQUIRED,-DataAccessException</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<import resource="bean.xml"/>
</beans>
使用的是c3p0 数据源 用JdbcTemplate 来操作数据
UserDao
public interface UserDao {
public List<User> getUserAll();
}
UserDaoImpl
public class UserDaoImpl implements UserDao{
private JdbcTemplate template;
public JdbcTemplate getTemplate() {
return template;
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@Override
public List<User> getUserAll() {
String sql = "select * from user ";
return (List<User>) template.query(sql, new BeanPropertyRowMapper(User.class));
}
需要在spring文件配置 dao
<bean id="userDao" class="com.core.database.dao.impl.UserDaoImpl">
<property name="template">
<ref bean="jdbcTemplate" />
</property>
</bean>
好接下来就是测试,见证奇迹的时候道了
private static Log log = LogFactory.getLog(SpringTest.class);
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
log.info("spring配置文件ok");
UserDao ormDao = (UserDao) ac.getBean("userDao");
// DataSources d = new DataS
UserDaoImpl b = new UserDaoImpl();
System.out.println(ormDao.getUserAll().size());
}
是不是可以查询了~~~不可以的就找bug吧
接下来就是action了
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public String execute() throws Exception{
System.out.println(userDao.getUserAll().size());
return SUCCESS;
}
}
继承的是 ActionSupport 具体为什么就不多说了 配置在上面已经给出了
接下来最后一步 在spring 配置文件中配置 action 让他管理
<bean id="userDao" class="com.n525m.core.database.dao.impl.UserDaoImpl">
<property name="template">
<ref bean="jdbcTemplate" />
</property>
</bean>
<bean id="user" class="com.n525m.core.database.action.UserAction">
<property name="userDao" ref="userDao" />
</bean>
恩 基本就是这样了,一个简单的就搭建完成了,