|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java编译的是字节码,跟C++相反,启动不够快,效率不够高,难以精确控制内存,但是优点是编程比C++容易,代码比较安全但是容易留下性能隐患,跨平台靠字节码在各个平台复制(一处编译到处调试)
第五步:创建Action(userAction.Java)
这里我们将建产两个Action.
userAction.java将完成以下事情.
1.增加(后期数据筹办.主是提取部门材料.并传送给adduser.JSP)
2.修正(后期数据筹办)
3.删除
4.检察一切用户材料.
user1Action.java将完成以下事情
1.增加用户(对myuser表操纵)
2.修正用户(对myuser表操纵)
packagemystruts;
importorg.apache.struts.action.*;
importjavax.Servlet.http.*;
importmystruts.*;
importjava.util.*;
importjava.sql.*;
importConnectionPool.*;
publicclassuserActionextendsAction{
publicActionForwardexecute(ActionMappingactionMapping,ActionFormactionForm,HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse)throwsException
{
userdaodao=newuserdao();
HttpServletRequestrequest=httpServletRequest;
Stringaction;
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"))
{
Stringkey=request.getParameter("key");
request.setAttribute("dep",this.getDep());
request.setAttribute("userActionForm",dao.getUser(key));
return(actionMapping.findForward("add"));
}
if(action.equals("del"))
{
Stringkey=request.getParameter("key");
dao.delUser(key);
return(actionMapping.findForward("delview"));
}
if(action.equals("view"))
{
request.setAttribute("rs",dao.getAlluser());
return(actionMapping.findForward("view"));
}
returnnull;
}
//---------------------------------------------
publicCollectiongetDep()
{
getDbConnectiondb=newgetDbConnection();
Connectioncon=db.getCon();
ResultSetrs=null;
PreparedStatementps=null;
ArrayListrslist=newArrayList();
Stringsql="select*frommydep";
try{
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
HashMaprscol=newHashMap();
rscol.put("id",rs.getString("dep_id"));
rscol.put("name1",rs.getString("dep_name"));
rslist.add(rscol);
}
}
catch(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
returnrslist;
}
}
packagemystruts;
importorg.apache.struts.action.*;
importjavax.servlet.http.*;
publicclassuser1ActionextendsAction{
publicActionForwardexecute(ActionMappingactionMapping,ActionFormactionForm,HttpServletRequesthttpServletRequest,HttpServletResponsehttpServletResponse)throwsException
{
userActionFormuaf=(userActionForm)actionForm;
if(uaf.getAction().equals("add"))
{
newuserdao().userInsert(uaf);
return(actionMapping.findForward("view"));
}
if(uaf.getAction().equals("edit"))
{
newuserdao().userUpdate(uaf);
return(actionMapping.findForward("view"));
}
returnnull;
}
}
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。 |
|