|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
J2ME在手机游戏开发的作用也是无用质疑的。至于桌面程序,可能有人说java不行,界面不好看,但是请看看NetBeans和Eclipse吧,他们都是利用java开发的,而他们的界面是多么的华丽,所以界面决不是java的缺点。还有一个不得不提的优点就是大多java人员都挂在嘴边的java的跨平台性,目前这确实也是java优点之一。终究把本人的这个测试做完了.但愿对想学struts的老手有所匡助.
(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驱动拷贝一份到Tomcat5.0commonlib目次下.请把struts.jarmystrutsWEB-INFlib目次下.
<Contextpath="/mystruts"docBase="D:wwwmystrutsmystrutsmystruts"debug="0"privileged="true">
<Resourcename="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParamsname="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:jtds:sqlserver://192.168.10.223:1433/mystruts</value>
</parameter>
</ResourceParams>
</Context>
WEB-INF/web.xml(到场struts标志库的撑持)
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
</web-app>
第二步:设置数据库
1.起首创立数据连接bean,并打包到ConnectionPool,它将从我们设置的连接池中获得可用连接.
//JavaDocument
packageConnectionPool;
importjavax.naming.*;
importjavax.sql.*;
importjava.sql.*;
importjava.io.*;
importjava.util.*;
publicclassgetDbConnection{
Connectionconn;
Statementstmt;
ResultSetrs=null;
Stringfoo="NotConnected";
intbar=-1;
publicgetDbConnection(){
try{
Contextctx=newInitialContext();
if(ctx==null)
thrownewException("Boom-NoContext");
DataSourceds=
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");
if(ds!=null){
conn=ds.getConnection();
}
}
catch(Exceptione){
e.printStackTrace();
}
}
publicConnectiongetCon()
{
returnconn;
}
publicvoidd_close()throwsSQLException
{
if(rs!=null){
try{rs.close();}catch(SQLExceptione){;}
rs=null;
}
if(stmt!=null){
try{stmt.close();}catch(SQLExceptione){;}
stmt=null;
}
if(conn!=null){
try{conn.close();}catch(SQLExceptione){;}
conn=null;
}
}
protectedvoidfinalize()throwsThrowable
{
try{d_close();}catch(SQLExceptione){;}
}
}
2.创立数据库.
数据库名:mystruts
部门表:mydep
_______________________________________
dep_iddep_name
_______________________________________
1dep1
2dep2
3dep3
4dep4
_______________________________________
用户表:myuser
______________________________________________
user_iduser_namedeptel
______________________________________________
1jack10769-2454042
2max20769-2454043
______________________________________________
(注:以上字段为了测试便利.全体利用字符型)
第三步:创立ActionForm(userActionForm.java)
packagemystruts;//请打包到mystruts下.
importorg.apache.struts.action.*;
importjavax.servlet.http.*;
publicclassuserActionFormextendsActionForm{
privateStringaction="add";
privateStringdep;
privateStringtel;
privateStringuser_id;
privateStringuser_name;
publicStringgetAction(){
returnaction;
}
publicvoidsetAction(Stringaction){
this.action=action;
}
publicStringgetDep(){
returndep;
}
publicvoidsetDep(Stringdep){
this.dep=dep;
}
publicStringgetTel(){
returntel;
}
publicvoidsetTel(Stringtel){
this.tel=tel;
}
publicStringgetUser_id(){
returnuser_id;
}
publicvoidsetUser_id(Stringuser_id){
this.user_id=user_id;
}
publicStringgetUser_name(){
returnuser_name;
}
publicvoidsetUser_name(Stringuser_name){
this.user_name=user_name;
}
publicActionErrorsvalidate(ActionMappingactionMapping,HttpServletRequesthttpServletRequest){
returnnull;
}
publicvoidreset(ActionMappingactionMapping,HttpServletRequesthttpServletRequest){
}
}
第四步:创建数据库操纵对象(完成对表的增删修正与查询)
packagemystruts;
importjava.sql.*;
importConnectionPool.*;
importjava.util.*;
publicclassuserdao{
getDbConnectiondb=null;
Connectioncon=null;
publicuserdao(){
}
//-------------------------------------------------------------------
publicvoiduserInsert(userActionFormuaf1)
{
db=newgetDbConnection();
con=db.getCon();
userActionFormuaf=uaf1;
PreparedStatementps=null;
Stringsql="insertintomyuser(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(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
}
//---------------------------------------------
publicvoiduserUpdate(userActionFormuaf1)
{
db=newgetDbConnection();
con=db.getCon();
userActionFormuaf=uaf1;
PreparedStatementps=null;
Stringsql="updatemyusersetuser_name=?,dep=?,tel=?whereuser_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(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
}
//---------------------------------------------
publicuserActionFormgetUser(Stringkey)
{
db=newgetDbConnection();
con=db.getCon();
ResultSetrs=null;
userActionFormuaf=newuserActionForm();
PreparedStatementps=null;
Stringsql="select*frommyuserwhereuser_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(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
System.out.println("mod"+key);
returnuaf;
}
//----------------------------------------
publicvoiddelUser(Stringkey)
{
db=newgetDbConnection();
con=db.getCon();
PreparedStatementps=null;
Stringsql="deletefrommyuserwhereuser_id=?";
try{
ps=con.prepareStatement(sql);
ps.setString(1,key.trim());
ps.executeUpdate();
}
catch(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
System.out.println("del"+key);
}
//----------------------------------
publicCollectiongetAlluser()
{
db=newgetDbConnection();
con=db.getCon();
ResultSetrs=null;
userActionFormuaf=newuserActionForm();
PreparedStatementps=null;
ArrayListrslist=newArrayList();
Stringsql="select*frommyuseru,mydepdwhereu.dep=d.dep_id";
try{
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next())
{
HashMaprscol=newHashMap();
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(SQLExceptione)
{
System.out.println("sqlerror");
}
finally
{
try{
con.close();
db.d_close();
}
catch(Exceptione){}
}
returnrslist;
}
//----------------------------------
}
第五步:创建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;
}
}
第六步:创建JSP视图页面(次要有两个jsp文件.增加页面.adduser.jsp检察页面viewuser.jsp)
adduser.jsp
<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>
<%@tagliburi="/WEB-INF/struts-template.tld"prefix="template"%>
<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<%@pagecontentType="text/html;charset=gb2312"%>
<html:html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<title>
adduser
</title>
</head>
<body>
<h1></h1>
<p>
<html:formaction="/user1Action.do"method="POST">
<html:hiddenproperty="action"/>
<br>
部门:
<html:selectproperty="dep">
<html:optionscollection="dep"property="id"labelProperty="name1"/>
</html:select>
<br>
ID:
<logic:equalname="userActionForm"property="action"scope="request"value="add">
<html:textproperty="user_id"/>
</logic:equal>
<logic:equalname="userActionForm"property="action"scope="request"value="edit">
<html:textproperty="user_id"readonly="true"/>
</logic:equal>
<br>
名字:<html:textproperty="user_name"/>
<br>
德律风:<html:textproperty="tel"/>
<br>
<html:submitproperty="submit"value="Submit"/><br>
<html:resetvalue="Reset"/>
</html:form>
</body>
</html:html>
viewuser.jsp
<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>
<%@tagliburi="/WEB-INF/struts-template.tld"prefix="template"%>
<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<%@pagecontentType="text/html;charset=GBK"%>
<html:html>
<head>
<title>
viewuser
</title>
</head>
<body>
<divalign="center"><ahref="userAction.do?action=add">add</a></div>
<tablewidth="60%"border="1"align="center">
<trbgcolor="#CCCCCC">
<td>ID</td>
<td>user_name</td>
<td>dep</td>
<td>tel</td>
<td>modify</td>
<td>del</td>
</tr>
<logic:iteratename="rs"id="user"scope="request"type="java.util.HashMap">
<tr>
<td><bean:writename="user"property="user_id"/></td>
<td><bean:writename="user"property="user_name"/></td>
<td><bean:writename="user"property="dep_name"/></td>
<td><bean:writename="user"property="tel"/></td>
<td><ahref="userAction.do?action=edit&key=<bean:writename="user"property="user_id"/>">modify</a></td>
<td><ahref="userAction.do?action=del&key=<bean:writename="user"property="user_id"/>">del</a></td>
</tr>
</logic:iterate>
</table>
</body>
</html:html>
建产一个转向页面(index.jsp)
<%@pagecontentType="text/html;charset=gb2312"%>
<html>
<head>
<title>UntitledDocument</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
</head>
<body>
<%response.sendRedirect("userAction.do?action=view");%>
</body>
</html>
递七步:创建struts设置文件(WEB-INF/config-struts.xml)
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-beanname="userActionForm"type="mystruts.userActionForm"/>
</form-beans>
<action-mappings>
<actiontype="mystruts.userAction"scope="request"path="/userAction">
<forwardname="view"path="/viewuser.jsp"/>
<forwardname="add"path="/adduser.jsp"/>
<forwardname="delview"path="/userAction.do?action=view"/>
</action>
<actionname="userActionForm"type="mystruts.user1Action"scope="request"path="/user1Action">
<forwardname="view"path="/userAction.do?action=view"/>
</action>
</action-mappings>
</struts-config>
运转程序:
启动tomcat
启动IE在地点栏中输出:http://localhost:8080/mystruts
OK.....
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。 |
|