Struts2 整合Spring使用jdbc连接Oracle数据库
1.首先加入Spring jar 包
commons-logging.jar
jta.jar
spring.jar
2.加入Struts2 jar包
commons-fileupload-1.2.1.jar
?
commons-logging-api-1.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
??struts2-spring-plugin-2.1.8.1.jar
?
注:struts2-spring-plugin-2.1.8.1.jar包是Struts2和Spring交互的桥梁,如果你仅仅使用Struts2的话不加也行,但是这里是必须的。
?
3.在Src下新建一个applicationContext.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:aop="http://www.springframework.org/schema/aop"
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.xsd">
<bean id="DbConnection" />
</property>
</bean>
<bean id="ui" />
</property>
</bean>
</beans>
注:具体内容请看下面解释:
?
4.编写连接数据库类DbConnection.java
?
?
package com.hw.util;
?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
?
/**
?* @author 李高松
?* @function 创建数据库连接
?* @version 2011年2月15日16:38:56
?*?
?*/
public class DbConnection {
private String url;
private String user;
private String password;
public void setUrl(String url) {
this.url = url;
}
public void setUser(String user) {
this.user = user;
}
public void setPasswrod(String passwrod) {
this.password = passwrod;
}
?
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
?
public Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
???return conn;
??}
}
注:url、user、password对应applicationContext.xml中的url、user、password。这里使用Spring的注入思想;配置方法为:
<bean id="DbConnection" />
</property>
</bean>
注:userDao中必须注入dbc属性,添加一个set方法,使用dbc进行数据库连接。
?
6.编写业务层代码:UserImpl.java,这里代码不再书写,在applicationContext.xml?中做注册配置如下
?
<bean id="ui" />
</property>
</bean>
注:UserImpl中必须注入dao属性,添加一个set方法,使用dao进行数据库的操作;
?
?
7.编写控制层代码:userAciton.java?
?
注:userAciton.java 中必须注入ui属性,添加一个set方法,使用ui调用相应的业务逻辑
?
8.编写struts.xml 配置文件
?
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="default" extends="struts-default" namespace="/"><action name="addAction" method="modify"><result type="redirect">queryAllAction</result></action><action name="del" method="move"><result type="redirect">queryAllAction</result></action></package></struts>
9.配置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"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param>
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
10.编写相应的UI界面这样就完成了
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<s:head/>
</head>
?
<body>
<div align="center">
<s:form action="addAction">
<s:textfield label="编号" name="user.id"></s:textfield>
<s:textfield label="用户名" name="user.username"></s:textfield>
<s:password label="密码" name="user.password"></s:password>
<s:textfield label="角色编号" name="user.rid"></s:textfield>
<s:submit label="提交"></s:submit>
<s:reset label="重置"></s:reset>
</s:form>
</div>
</body>
</html>
?
?
?