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文件,内容如下:
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">
jdbc:oracle:thin:@localhost:1521:orcl
scott
tiger
注:具体内容请看下面解释:
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的注入思想;配置方法为:
jdbc:oracle:thin:@localhost:1521:orcl
scott
tiger
5.编写UserDao做CRUD操作,这里不再书写,这里需要在applicationContext.xml中做注册配置如下
注:userDao中必须注入dbc属性,添加一个set方法,使用dbc进行数据库连接。
6.编写业务层代码:UserImpl.java,这里代码不再书写,在applicationContext.xml中做注册配置如下
注:UserImpl中必须注入dao属性,添加一个set方法,使用dao进行数据库的操作;
7.编写控制层代码:userAciton.java
注:userAciton.java 中必须注入ui属性,添加一个set方法,使用ui调用相应的业务逻辑
8.编写struts.xml 配置文件
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
method="add">
/index.jsp
method="query">
/queryall.jsp
method="queryById">
/modifyuser.jsp
queryAllAction
queryAllAction
9.配置web.xml文件
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">
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
10.编写相应的UI界面这样就完成了
My JSP 'index.jsp' starting page