【SSH (六)】struts2 整合 spring 并 连接数据库
原创
©著作权归作者所有:来自51CTO博客作者wx636dc453ed367的原创作品,请联系作者获取转载授权,否则将追究法律责任
这个项目大致在【SSH (三)】struts2项目搭建的基础上进行。
1,首先下载spring jar包,
http://spring.io/
选择右上角PROJECTS,选择Spring framework,进去以后如下图:

点击蓝色的连接,进入下一个页面,往下拉,到下图的位置,有下载地址:

2,添加jar包:
将所用到的spring jar包添加至项目的lib目录,除此之外,还要添加一个struts-spring-plugin的jar包,这个在之前下载的struts-all里面找,保证版本一致。
3,配置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>Struts2Project</display-name>
<!-- 配置struts2为项目的controller -->
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:res/beans-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
加一个监听器,载入spring容器,没什么。
主要是配置了spring的bean文件位置,如果bean很多,那么放在一个文件中不易于管理,这里按层的方式进行了划分,为的是更好的管理bean文件,目录结构:

在src/res目录下。spring.xml配置与spring相关的,如扫描包注解,data.xml配置数据库。
4,修改struts配置文件,
没有spring,struts配置文件action的class是class的全名,有了spring,那么action实例的创建由spring完成,要对class进行修改,改为action实例的bean的id。
例如,beans-action.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"
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-3.0.xsd">
<!-- 注册的Action -->
<bean id="loginAction" class="com.action.LoginAction"></bean>
<!-- more bean definitions go here -->
</beans>
那么相应的struts-login.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="login" extends="default">
<action name="loginform" class="loginAction" method="loginform">
<result name="success">/WEB-INF/jsp/login/login.jsp</result>
</action>
<action name="login" class="loginAction" method="login">
<result name="success" type="redirect"></result>
<result name="login" type="redirect"></result>
</action>
<action name="loginsuccess" class="loginAction" method="success">
<interceptor-ref name="authority"></interceptor-ref>
<result name="success">/WEB-INF/jsp/success.jsp</result>
<result name="login" type="redirect"></result>
</action>
</package>
</struts>
可以看到class改为了bean实例的id。
其他就是一些service和dao的编写。
5,配置数据库,
先创建db.properties:
#sql service数据库
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433; databaseName=Test
jdbc.username=sa
jdbc.password=sa
#mysql数据库
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#jdbc.username=root
#jdbc.password=
在beans-data.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"
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-3.0.xsd">
<!--引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:res/db.properties</value>
</list>
</property>
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
持久层框架选择jdbctemplate,在配置datasource和propertyConfigure。
导入jar包:
数据源使用的bdcp,那么需要引入dbcp的jar包,这个jar又依赖common-pool的jar,要是用c3p0的话就只需要c3p0的jar
再引入sql server的驱动包。
在dao层查数据:
package com.dao;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.formbean.UserForm;
import bean.User;
public class LoginDaoImp implements LoginDao{
@Resource
private JdbcTemplate JdbcTemplate;
@Override
public User getUserByUandP(UserForm userForm) {
// TODO Auto-generated method stub
User user = new User();
user.setInfo("heheda");
user.setUsername("ly");
user.setPassword("123");
String sql = "SELECT * FROM tttt";
List<Map<String, Object>> list = JdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
System.out.println(map.get("hehe"));
}
return new User();
}
}
查数据与登录无关,只是验证连接。
如下图访问:

点击Login:

eclipse控制台信息:

确实是数据库中的数据,说明数据库连接成功。
项目源码:
http://pan.baidu.com/s/1i4dqrKL