下载首页 | 资讯中心 | 下载分类 | 最近更新 | 排 行 榜 | 国产软件 | 国外软件 | 汉化补丁 |
文章搜索: 分类 关键字 收藏本站设为首页
您的位置:首页网页设计ASP程序 → 简单的struts应用开发__教程
简单的struts应用开发__教程
日期:2007-5-20 0:38:02 人气:57     [ ]
 (mystruts应用)目录结构
mystruts
|-WEB-INF
| |- lib
| | |- struts.jar
| |- classes
| | |- ConnectionPool
| | | |- getDbConnection.class
| | |-mystruts
| | |- user1Action.class
| | |- userAction.class
| | |- userActionForm.class
| | |- userdao.class
| |-struts-bean.tld
| |-struts-config.xml
| |-struts-html.tld
| |-struts-logic.tld
| |-struts-template.tld
|-adduser.jsp
|-index.jsp
|-viewuser.jsp

第一步: 开发环境配置

(如果你还没有相关软件. 请到http://www.apache.org 下载)

tomcat5.0 配置 (建立一个 mystruts 应用, 并拷贝相关文件)

请在你的tomcat安装目录中找到confCatalinalocalhost 子目录. 新建 mystruts.xml 文件
文件内容如下. 在这个文件中. 我们将配置一个联接池.请把相关的驱动.数据库用户,密码改成你自己的本机配置.
并把JDBC驱动拷贝一份到 Tomcat 5.0commonlib 目录下. 请把 struts.jar mystrutsWEB-INFlib 目录下.



auth="Container"
type="javax.sql.DataSource"/>



factory
org.apache.commons.dbcp.BasicDataSourceFactory



maxActive
10



maxIdle
5



maxWait
10000



username
sa


password
test



driverClassName
net.sourceforge.jtds.jdbc.Driver



url
jdbc:jtds:sqlserver://192.168.10.223:1433/mystruts




WEB-INF/web.xml (加入struts标记库的支持)





action
org.apache.struts.action.ActionServlet

debug
2


config
/WEB-INF/struts-config.xml

2


action
*.do


/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld


/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld


/WEB-INF/struts-logic.tld
/WEB-INF/struts-logic.tld


/WEB-INF/struts-template.tld
/WEB-INF/struts-template.tld



第二步: 配置数据库

1.首先创建数据联接bean, 并打包到 ConnectionPool, 它将从我们配置的联接池中取得可用联接.

// Java Document
package ConnectionPool;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import java.util.*;

public class getDbConnection {

Connection conn;
Statement stmt;
ResultSet rs=null;

String foo = "Not Connected";
int bar = -1;

public getDbConnection() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");

DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");

if (ds != null) {
conn = ds.getConnection();

}
}
catch(Exception e) {
e.printStackTrace();
}
}

public Connection getCon()
{
return conn;
}


public void d_close() throws SQLException
{
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}

}

protected void finalize() throws Throwable
{
try { d_close(); } catch (SQLException e) { ; }
}

}

2.创建数据库.

数据库名: mystruts

部门表: mydep

_______________________________________
dep_id dep_name
_______________________________________
1 dep1
2 dep2
3 dep3
4 dep4
_______________________________________

用户表: myuser
______________________________________________
user_id user_name dep tel
______________________________________________
1 jack 1 0769-2454042
2 max 2 0769-2454043
______________________________________________

(注: 以上字段为了测试方便. 全部使用字符型)


第三步: 创建ActionForm (userActionForm.java)



package mystruts; //请打包到mystruts下.

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class userActionForm extends ActionForm {
private String action="add";
private String dep;
private String tel;
private String user_id;
private String user_name;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}


第四步:建立数据库操作对象 (完成对表的增删修改与查询)

package mystruts;

import java.sql.*;
import ConnectionPool.*;
import java.util.*;

public class userdao {
getDbConnection db=null;
Connection con=null;

public userdao() {
}
//-------------------------------------------------------------------
public void userInsert(userActionForm uaf1)
{
db=new getDbConnection();
con=db.getCon();
userActionForm uaf=uaf1;
PreparedStatement ps = null;
String sql="insert into myuser (user_id,user_name,dep,tel) values(?,?,?,?)";
try {

ps = con.prepareStatement(sql);
ps.setString(1,uaf.getUser_id());
ps.setString(2,uaf.getUser_name());
ps.setString(3,uaf.getDep());
ps.setString(4,uaf.getTel());
ps.executeUpdate();
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
}
//---------------------------------------------
public void userUpdate(userActionForm uaf1)
{
db=new getDbConnection();
con=db.getCon();
userActionForm uaf=uaf1;
PreparedStatement ps = null;
String sql="update myuser set user_name=?,dep=?,tel=? where user_id=?";
try {

ps = con.prepareStatement(sql);
ps.setString(1,uaf.getUser_name());
ps.setString(2,uaf.getDep());
ps.setString(3,uaf.getTel());
ps.setString(4,uaf.getUser_id());
ps.executeUpdate();
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
}
//---------------------------------------------
public userActionForm getUser(String key)
{
db=new getDbConnection();
con=db.getCon();
ResultSet rs=null;
userActionForm uaf=new userActionForm();
PreparedStatement ps = null;
String sql="select * from myuser where user_id=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1,key.trim());
rs=ps.executeQuery();
if(rs.next())
{
uaf.setUser_id(rs.getString("user_id"));
uaf.setUser_name(rs.getString("user_name"));
uaf.setDep(rs.getString("dep"));
uaf.setTel(rs.getString("tel"));
uaf.setAction("edit");
}
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
System.out.println("mod"+key);
return uaf;
}
//----------------------------------------
public void delUser(String key)
{
db=new getDbConnection();
con=db.getCon();
PreparedStatement ps = null;
String sql="delete from myuser where user_id=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1,key.trim());
ps.executeUpdate();
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
System.out.println("del"+key);
}
//----------------------------------
public Collection getAlluser()
{
db=new getDbConnection();
con=db.getCon();
ResultSet rs=null;
userActionForm uaf=new userActionForm();
PreparedStatement ps = null;

ArrayList rslist = new ArrayList();

String sql="select * from myuser u,mydep d where u.dep=d.dep_id";
try {
ps = con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
HashMap rscol = new HashMap();
rscol.put("user_id",rs.getString("user_id"));
rscol.put("user_name",rs.getString("user_name"));
rscol.put("dep",rs.getString("dep"));
rscol.put("dep_name",rs.getString("dep_name"));
rscol.put("tel",rs.getString("tel"));
rslist.add(rscol);
}
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
return rslist;
}
//----------------------------------
}

第五步:建立Action (userAction.java)

这里我们将建产两个Action .

userAction.java 将完成以下工作.
1.添加(前期数据准备. 主是提取部门资料.并传递给adduser.jsp)
2.修改(前期数据准备)
3.删除
4.查看所有用户资料.

user1Action.java 将完成以下工作
1.添加用户(对myuser表操作)
2.修改用户(对myuser表操作)

package mystruts;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import mystruts.*;
import java.util.*;
import java.sql.*;
import ConnectionPool.*;

public class userAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
{
userdao dao=new userdao();
HttpServletRequest request=httpServletRequest;
String action;
if(request.getParameter("action").equals("") || request.getParameter("action")==null)
{
action="add";
}
else
{
action=request.getParameter("action");
}
if(action.equals("add"))
{
request.setAttribute("dep",this.getDep());
return (actionMapping.findForward("add"));
}
if(action.equals("edit"))
{
String key=request.getParameter("key");
request.setAttribute("dep",this.getDep());
request.setAttribute("userActionForm",dao.getUser(key));
return (actionMapping.findForward("add"));
}
if(action.equals("del"))
{
String key=request.getParameter("key");
dao.delUser(key);
return (actionMapping.findForward("delview"));
}
if(action.equals("view"))
{
request.setAttribute("rs",dao.getAlluser());
return (actionMapping.findForward("view"));
}

return null;
}
//---------------------------------------------
public Collection getDep()
{
getDbConnection db=new getDbConnection();
Connection con=db.getCon();
ResultSet rs=null;

PreparedStatement ps = null;

ArrayList rslist = new ArrayList();

String sql="select * from mydep";
try {
ps = con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
HashMap rscol = new HashMap();
rscol.put("id",rs.getString("dep_id"));
rscol.put("name1",rs.getString("dep_name"));
rslist.add(rscol);
}
}
catch (SQLException e)
{
System.out.println("sql error");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exception e) {}
}
return rslist;
}
}



package mystruts;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class user1Action extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
{
userActionForm uaf = (userActionForm) actionForm;
if(uaf.getAction().equals("add"))
{
new userdao().userInsert(uaf);
return (actionMapping.findForward("view"));
}
if(uaf.getAction().equals("edit"))
{
new userdao().userUpdate(uaf);
return (actionMapping.findForward("view"));
}
return null;
}
}


第六步:建立JSP视图页面 (主要有两个jsp文件. 添加页面. adduser.jsp 查看页面 viewuser.jsp)

adduser.jsp

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=gb2312" %>






 







部门:





ID:








名字:


电话:









viewuser.jsp

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" %>

























ID user_name dep tel modify del
        ">modify ">del




建产一个转向页面 (index.jsp)
<%@ page contentType="text/html; charset=gb2312" %>







<% response.sendRedirect("userAction.do?action=view"); %>



递七步:建立struts配置文件 (WEB-INF/config-struts.xml)



















运行程序:
启动tomcat
启动IE 在地址栏中输入:http://localhost:8080/mystruts

OK.....
出处:本站原创 作者:佚名
 阅读排行
01.精美qq空间横幅代码
02.最酷qq个性女生网名
03.最新又有免费QQ秀啦《..
04.巧用透明FlaSh扮靓你的..
05.花之神匠代码(最新代码..
06.最新QQ空间免费导航
07.最新免费个人形象设置..
08.最新qq空间flash代码m..
09.CSS技术结合图像实现动..
10.Photoshop光影魔术师:..
11.QQ音速种子狂刷
12.最新QQ空间透明代码
13.PS实例教程:教你制作结..
14.Photoshop光影魔术师:..
15.制作背景图__教程
16.用Photoshop制作漂亮的..
17.如何获得QQ音速种子
18.≤QQ空间代码≥在日志..
19.网页浮动广告的制作代..
20.用Photoshop制作大红灯..
21.常用CSS
22.Photoshop给靓丽美女打..
 推荐文章
·Photoshop 表现技法之..
·快速将你的相片矢量化..
·PHOTOSHOP制作炽热的太..
·用Photoshop制作美丽的..
·流行杀手的娃娃工厂__..
·打造8号台球__教程
·PHOTOSHOP制作待机MM图..
·用Photoshop帮MM做纹身..
·PHOTOSHOP美眉着色绝招..
·PHOTOSHOP花露的制作_..
·PHOTOSHOP渐变工具的巧..
·PHOTOSHOP手绘奥兰多-..
·高难度抠图两种方法__..
·Photoshop高尔夫球的制..
·Photoshop打造精美玉佩..
Eqxia_COM下载站 版权所有 Copyright© 2001-2005 Www.eqxia.COM, All Rights Reserved.