整合时两个框架各自的分工如下:
Struts2负责流程,Spring负责对象的创建,Action和Service都由Spring框架负责创建。这是常用的集成合并方案。
1:首先导入相关jar包,见如下图片:
2:web.xml所有代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 用于指定Spring的配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
服务器启动时,通过监听器初始化Spring的配置环境 监听器,默认加载文件是:/WEB-INF/applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Struts2框架的核心调度器 -->
<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>
3:在src目录下直接创建配置文件:applicationContext.xml和struts.xml
applicationContext.xml详细代码如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:Oracle.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
autowire="default">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- 配置一个NamedParameterJdbcTemplate模板 使用构造函数注入器 -->
<bean id="namedjdbcTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="loginAction" class="com.sjx.action.LoginAction"
autowire="byName" scope="prototype">
<property name="iLoginService" ref="loginService"></property>
</bean>
<bean id="loginService" class="com.sjx.service.impl.LoginServiceImpl">
<property name="iloginDao" ref="loginDao"></property>
</bean>
<bean id="loginDao" class="com.sjx.dao.impl.LoginDaoImpl">
<property name="namedjdbcTemplate" ref="namedjdbcTemplate"></property>
</bean>
</beans>
struts.xml详细代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "dtds/struts-2.3.dtd">
<struts>
<package name="com.sjx" namespace="/" extends="struts-default">
<action name="Login" class="loginAction" method="login">
<result name="success" type="redirect">/success.jsp</result>
<result name="login" type="redirect">/login.jsp</result>
</action>
</package>
</struts>
由于此套整合包含调用oracle数据库进行操作,故还需引入数据库配置文件Oracle.properties,同样直接写在src目录下:
Oracle.properties:
jdbc.driverClass = oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl = jdbc:oracle:thin:@localhost:1521/ORCL
jdbc.user = scott
jdbc.password = tiger
4:Action类:
package com.sjx.action;
import java.util.List;
import java.util.Map;
import com.sjx.service.ILoginService;
public class LoginAction {
private ILoginService iLoginService;
public ILoginService getiLoginService() {
return iLoginService;
}
public void setiLoginService(ILoginService iLoginService) {
this.iLoginService = iLoginService;
}
public String login(){
List<Map<String, Object>> list = iLoginService.find();
System.out.println(list);
return "success";
}
}
dao包:包含接口和实现类:
接口(ILoginDao):
package com.sjx.dao;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
public interface ILoginDao {
/**
* @return the namedjdbcTemplate
*/
public abstract NamedParameterJdbcTemplate getNamedjdbcTemplate();
/**
* @param namedjdbcTemplate
* the namedjdbcTemplate to set
*/
public abstract void setNamedjdbcTemplate(
NamedParameterJdbcTemplate namedjdbcTemplate);
public abstract void add(String str);
public abstract List<Map<String, Object>> find();
}
实现类(LoginDaoImpl):
package com.sjx.dao.impl;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import com.sjx.dao.ILoginDao;
import com.sjx.model.DeptVO;
public class LoginDaoImpl implements ILoginDao {
private NamedParameterJdbcTemplate namedjdbcTemplate;
public void add(String str) {
System.out.println("struts2+spring");
}
public NamedParameterJdbcTemplate getNamedjdbcTemplate() {
return namedjdbcTemplate;
}
public void setNamedjdbcTemplate(
NamedParameterJdbcTemplate namedjdbcTemplate) {
this.namedjdbcTemplate = namedjdbcTemplate;
}
public List<Map<String, Object>>find() {
DeptVO deptvo = null;
String sql = "select * from dept";
List<Map<String, Object>> list = namedjdbcTemplate.getJdbcOperations().queryForList(sql);
return list;
}
}
实体类(DeptVO):
package com.sjx.model;
public class DeptVO {
private String deptNo;
private String dName;
private String loc;
public String getDeptNo() {
return deptNo;
}
public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
}
public String getdName() {
return dName;
}
public void setdName(String dName) {
this.dName = dName;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
service接口和实现类:
接口(ILoginService):
package com.sjx.service;
import java.util.List;
import java.util.Map;
public interface ILoginService {
public abstract void add(String str);
public abstract List<Map<String, Object>> find();
}
实现类(LoginServiceImpl):
package com.sjx.service.impl;
import java.util.List;
import java.util.Map;
import com.sjx.dao.ILoginDao;
import com.sjx.service.ILoginService;
public class LoginServiceImpl implements ILoginService {
public ILoginDao iloginDao;
public void add(String str) {
iloginDao.add(str);
}
public ILoginDao getIloginDao() {
return iloginDao;
}
public void setIloginDao(ILoginDao iloginDao) {
this.iloginDao = iloginDao;
}
public List<Map<String, Object>> find() {
List<Map<String, Object>> list = iloginDao.find();
return list;
}
}
该套整合框架除了可以使用tomcat容器启动并进行初始化,还可以独自写一个类,在该类的main方法中进行初始化:
代码如下:
package com.sjx.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sjx.action.LoginAction;
public class SpringTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
LoginAction la = (LoginAction) ac.getBean("loginAction");
String aa = la.login();
System.out.println(aa);
}
}