该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2011-02-27
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" class="com.hw.util.DbConnection"> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="user"> <value>scott</value> </property> <property name="passwrod"> <value>tiger</value> </property> </bean> <bean id="UserDao" class="com.hw.dao.UserDao"> <property name="dbc"> <ref bean="DbConnection" /> </property> </bean> <bean id="ui" class="com.hw.service.UserImpl"> <property name="dao"> <ref bean="UserDao" /> </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; } } <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="user"> <value>scott</value> </property> <property name="passwrod"> <value>tiger</value> </property> </bean> 5.编写UserDao做CRUD操作,这里不再书写,这里需要在applicationContext.xml 中做注册配置如下 <bean id="UserDao" class="com.hw.dao.UserDao"> <property name="dbc"> <ref bean="DbConnection" /> </property> </bean> 注:userDao中必须注入dbc属性,添加一个set方法,使用dbc进行数据库连接。 6.编写业务层代码:UserImpl.java,这里代码不再书写,在applicationContext.xml 中做注册配置如下 <bean id="ui" class="com.hw.service.UserImpl"> <property name="dao"> <ref bean="UserDao" /> </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" class="com.hw.action.UserAction"
method="add">
<result name="success">/index.jsp</result>
</action>
<action name="queryAllAction" class="com.hw.action.UserAction"
method="query">
<result name="success">/queryall.jsp</result>
</action>
<action name="queryById" class="com.hw.action.UserAction"
method="queryById">
<result name="success">/modifyuser.jsp</result>
</action>
<action name="update" class="com.hw.action.UserAction" method="modify">
<result type="redirect">queryAllAction</result>
</action>
<action name="del" class="com.hw.action.UserAction" method="move">
<result type="redirect">queryAllAction</result>
</action>
</package>
</struts> 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> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 3977 次