|
常用鏈接
留言簿(2)
隨筆檔案
小鳥
李俊平
搜索
最新評論

閱讀排行榜
評論排行榜
Powered by: 博客園
模板提供:滬江博客
|
|
|
|
|
發(fā)新文章 |
|
|
2006年11月7日
? Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ??????? Dim path As String ??????? path = Application.StartupPath + "\Send.ini" ??????? TextBox1.Text = GetINI("Send", "Send1", "", path) ??????? TextBox2.Text = GetINI("Send", "Send2", "", path) ??????? Dim IsSms As Integer = GetINI("Send", "IsSms", "", path) ??????? If (IsSms = 1) Then ??????????? Me.RadioButton1.Checked = True ??????? ElseIf (IsSms = 0) Then ??????????? Me.RadioButton2.Checked = True ??????? End If
??? End Sub ??? Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String ??????? Dim Str As String = LSet(Str, 256) ??????? GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName) ??????? Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1) ??? End Function ??? Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long ??????? WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName) ??? End Function ??? Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32 ??? Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
?
??? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ??????? Try ??????????? Dim path As String ??????????? path = Application.StartupPath + "\Send.ini" ??????????? WriteINI("Send", "Send1", TextBox1.Text, path) ??????????? WriteINI("Send", "Send2", TextBox2.Text, path) ??????????? If (Me.RadioButton1.Checked = True) Then ??????????????? WriteINI("Send", "IsSms", 1, path) ??????????? ElseIf (Me.RadioButton2.Checked = True) Then ??????????????? WriteINI("Send", "IsSms", 0, path)
??????????? End If ??????????? MsgBox("配置設置已經(jīng)成功!!!!")
??????? Catch ex As Exception ??????????? MsgBox("錯誤!!!!") ??????? End Try
??? End Sub
看了大峽搞的級聯(lián)菜單,我也班門弄斧一把,嘿嘿,花了一點時間搞了個級聯(lián)菜單貼上來看看。本例中只要你選擇成員分類名稱就會自動顯示成員名稱: 首先在eclipse中建一個項目,名稱你自己做主了,這里為Easyjf-menu,對應的瀏覽器頁面代碼為: Easyjf-menu.jsp
看了大峽搞的級聯(lián)菜單,我也班門弄斧一把,嘿嘿,花了一點時間搞了個級聯(lián)菜單貼上來看看。本例中只要你選擇成員分類名稱就會自動顯示成員名稱:
?? 首先在eclipse中建一個項目,名稱你自己做主了,這里為Easyjf-menu,對應的瀏覽器頁面代碼為:
?Easyjf-menu.jsp
?<@page contentType=”text/html;charser=UTF-8” language=”java”%>
?<head>
?? ……..
?</head>
?<script language=”javascript”>
?? var XMLHttpReq;
?? var currentSort;
?? //創(chuàng)建xmlhttprequset對象
?? function createXMLHttpRequest(){
?? if(window.XMLHttpRequest){
?? XMLHttpReq=new XMLHttpRequest();
?}
else if (window.ActiveXObject){
??try{
??? ?XMlHttpReq=new ActiveXObject(“Msxml2.XMLHTTP”);
??? }catch(e){}
?????? try{
?XMLHttpRequest=new ActiveXObject(“Microsoft.XMLHTTP”);
}catch(e){}
}
}
//發(fā)送請求函數(shù)
function sendRequest(url){
?createXMLHttpRequest();
?XMLHttpReq.open(“GET”,url,true);
?XMLHttpReq.onreadystatechange=processResponse;
?XMLHttpReq.send(null);
}
//處理返回信息函數(shù)
function processResponse(){
?? if(XMLHttpRequest.readyState==4){
?? if(XMLHttpRequest.status==200){
?updateMenu();
}else{alert(“您請求的頁面有異常!”)}
}
}
//更新菜單函數(shù)
function updateMenu(){
?var res=XMLHttpReq.responseXML.getElementIdByTagName(“res”);
?var sunMenu=””;
?for(var i=0;i<res.length;i++){
?submenu=subMenu+res[1].fistChild.data+”<br>”;
}
currentSort.innerHTML=submenu;
}
//創(chuàng)建級聯(lián)菜單
function showSubMenu(obj){
?? currentSort=document.getElementById(obj);
?? currentSort.parentNode.style.display=””;
?? sendRequest(“menu?sort=”+obj);
}
</script>
<b>EasyJF成員</b>
<a onClick=”onShowSubMenu(‘大峽’)”>大峽</a>
,
該頁面中提供了對應的菜單以供用戶選擇,用戶選擇菜單后,調(diào)用
showSubMenu(‘XXX’)
函數(shù),其中參數(shù)用于傳遞用戶所選菜單的標識信息到服務器以決定獲取服務器的哪個在菜單內(nèi)容,首先獲得菜單的識別信息,再提交給
Ajax,
這里用
innerHTML
屬性實現(xiàn)定位顯示!
在平時的實際項目中,為了減少往返提交數(shù)據(jù),可以一次編輯 GridView 的所有行,這樣大大提高用戶使用產(chǎn)品的體驗。下面就講解何一次編輯 GridView 的所有數(shù)據(jù)行。在下面的例子中,數(shù)據(jù)庫使用《ASP.NET 2.0應用開發(fā)技術》一書中Site\App_Data\下的 ASPNET20Book.mdb 數(shù)據(jù)庫。例子中的數(shù)據(jù)更新方式使用 SqlDataSource 數(shù)據(jù)源控件的UpdateCommand屬性,但此方法同樣適用于存儲過程,SQL 語句等。
C#?
<%
@?Page?Language
=
"
C#
"
?Debug
=
"
true
"
?
%>
<!
DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
script?
runat
="server"
>
string[]?ClassNameArray?
=
?{?
"
大學
"
,?
"
中學
"
,?
"
高中
"
?};
//
初始化原來的值
protected?
void
?GridView1_RowDataBound(object?sender,?GridViewRowEventArgs?e) {???? ??
if
?(e.Row.RowType?
==
?DataControlRowType.DataRow) ??{ ????System.Data.DataRowView?drv?
=
?(System.Data.DataRowView)e.Row.DataItem; ????RadioButtonList?rbl?
=
?(RadioButtonList)e.Row.FindControl(
"
txtGender
"
); ????
if
?(rbl?
!=
?
null
) ????{ ??????
if
?((bool)drv[
"
Gender
"
]) ??????{ ????????rbl.Items.FindByText(
"
男
"
).Selected?
=
?
true
; ??????} ??????
else
??????{ ????????rbl.Items.FindByText(
"
女
"
).Selected?
=
?
true
; ??????} ????}
????DropDownList?ddl?
=
?(DropDownList)e.Row.FindControl(
"
txtClassName
"
); ????
if
?(ddl?
!=
?
null
) ????{ ??????ddl.Items.FindByText(drv[
"
ClassName
"
].ToString()).Selected?
=
?
true
; ????} ??} }
//
?初始化項目列表
protected?
void
?GridView1_RowCreated(object?sender,?GridViewRowEventArgs?e) {???? ??
if
?(e.Row.RowType?
==
?DataControlRowType.DataRow) ??{ ????DropDownList?ddl?
=
?(DropDownList)e.Row.FindControl(
"
txtClassName
"
); ????ddl.DataSource?
=
?ClassNameArray; ????ddl.DataBind(); ??} }
//
處理提交的數(shù)據(jù)
protected?
void
?XianhuiMeng_Click(object?sender,?EventArgs?e) { ??foreach?(GridViewRow?gvr?
in
?GridView1.Rows) ??{ ????string?tGender?
=
?((RadioButtonList)gvr.FindControl(
"
txtGender
"
)).SelectedValue; ????SqlDataSource1.UpdateParameters.Add(
new
?Parameter(
"
@Title
"
,?TypeCode.String,?((TextBox)gvr.FindControl(
"
txtTitle
"
)).Text)); ????SqlDataSource1.UpdateParameters.Add(
new
?Parameter(
"
@Gender
"
,?TypeCode.Boolean,?(tGender?
==
?
"
男
"
?
?
?
"
True
"
?:?
"
False
"
))); ????SqlDataSource1.UpdateParameters.Add(
new
?Parameter(
"
@ClassName
"
,? ???????????????????TypeCode.String,?((DropDownList)gvr.FindControl(
"
txtClassName
"
)).SelectedValue)); ????SqlDataSource1.UpdateParameters.Add(
new
?Parameter(
"
@id
"
,?TypeCode.Int32,?GridView1.DataKeys[gvr.DataItemIndex].Value.ToString())); ????SqlDataSource1.Update(); ????SqlDataSource1.UpdateParameters.Clear(); ??} ??Response.Redirect(Request.UrlReferrer.ToString()); }
</
script
>
<
html?
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head?
id
="Head1"
?runat
="server"
>
<
title
>
同時編輯?GridView的?多行
</
title
>
</
head
>
<
body
>
<
form?
id
="form1"
?runat
="server"
>
??
<
asp:GridView?
ID
="GridView1"
?runat
="server"
?Font-Size
="12px"
?CellPadding
="3"
?AutoGenerateColumns
="False"
????DataKeyNames
="id"
?DataSourceID
="SqlDataSource1"
?OnRowDataBound
="GridView1_RowDataBound"
????OnRowCreated
="GridView1_RowCreated"
>
????
<
HeaderStyle?
BackColor
="#EDEDED"
?
/>
????
<
Columns
>
??????
<
asp:TemplateField?
HeaderText
="姓名"
>
????????
<
ItemTemplate
>
??????????
<
asp:TextBox?
ID
="txtTitle"
?runat
="server"
?Text
='<%#Eval("Title")?
%
>
'>
</
asp:TextBox
>
????????
</
ItemTemplate
>
??????
</
asp:TemplateField
>
??????
<
asp:TemplateField?
HeaderText
="性別"
>
????????
<
ItemTemplate
>
??????????
<
asp:RadioButtonList?
ID
="txtGender"
?runat
="server"
?RepeatColumns
="2"
>
????????????
<
asp:ListItem?
Text
='男'
></
asp:ListItem
>
????????????
<
asp:ListItem?
Text
='女'
></
asp:ListItem
>
??????????
</
asp:RadioButtonList
>
????????
</
ItemTemplate
>
??????
</
asp:TemplateField
>
??????
<
asp:TemplateField?
HeaderText
="學歷"
>
????????
<
ItemTemplate
>
??????????
<
asp:DropDownList?
ID
="txtClassName"
?runat
="server"
>
??????????
</
asp:DropDownList
>
????????
</
ItemTemplate
>
??????
</
asp:TemplateField
>
????
</
Columns
>
??
</
asp:GridView
>
??
<
asp:SqlDataSource?
ID
="SqlDataSource1"
?runat
="server"
? ????ConnectionString
="Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=|DataDirectory|ASPNET20Book.mdb;Persist?Security?Info=True"
????ProviderName
="System.Data.OleDb"
?SelectCommand
="SELECT?*?FROM?[Student]"
? ????UpdateCommand
="Update?[Student]?Set?Title?=?@Title,Gender?=?@Gender,ClassName=@ClassName?Where?id=@id"
>
??
</
asp:SqlDataSource
>
??
<
asp:Button?
ID
="Button1"
?runat
="server"
?Text
="保存所有修改"
?OnClick
="XianhuiMeng_Click"
?
/>
</
form
>
</
body
>
</
html
>
注意:對于SQL Server 數(shù)據(jù)庫,參數(shù)名字不加@。另外注意:不同數(shù)據(jù)庫類型和不同的數(shù)據(jù)源,UpdateCommand="Update [Student] Set Title = @Title,Gender = @Gender,ClassName=@ClassName Where id=@id"寫法和SqlDataSource1.UpdateParameters.Add(new Parameter("@Title", TypeCode.String, ((TextBox)gvr.FindControl("txtTitle")).Text)); 的寫法會有所區(qū)別。
摘要: 下面代碼實現(xiàn)將 GridView 導出到 Excel文件中。
值得注意的是VerifyRenderingInServerForm重載方法: MSDN上的 VerifyRenderingInServerForm 方法的描述: 必須位于 <form runat=server> 標記中的控件可以在呈現(xiàn)之前調(diào)用此方法,以便在控件被置于標記外時顯示錯誤信息。發(fā)送回或依賴于注冊的腳本塊的... 閱讀全文
facade模式:就是中關村攢機者的角色,他負責與多個零件供銷商交互并將這些零件組裝起來交給最終客戶。 struts與spring集成的兩種方式: 第一種是在struts的Action的execute方法中調(diào)用spring的applicationContext去獲得Manager對象,這里的Action自身沒有用到spring的思想,Action無法作為Spring中的JavaBean進行配置,相應代碼如下: ????? ServletContext application = this.getServlet().getServletContext(); ????? WebApplicationContext wapp = (WebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(application); ???? //WebApplicationContext wapp = (WebApplicationContext)application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); ???? StudentManager studentManager = (StudentManager)wapp.getBean("studentManager"); ? 另外,spring為集成struts而提供了一個ContextLoaderPlugIn插件,其作用在于:創(chuàng)建一個ApplicationContext對象,并將這個對象存儲在ServletContext中,以后在ActionSupport中就可以調(diào)用getWebApplicationContext來獲得這個ApplicationContext對象了,這并沒有讓我感覺有多大的好處,示例代碼如下: ??? Enumeration e = application.getAttributeNames(); ??? while(e.hasMoreElements()) ??? { ??????? String attributeName = (String)e.nextElement(); ??????? String objClassName = application.getAttribute(attributeName).getClass().getName(); ??????? try ??????? { ?????????? response.getWriter().println(attributeName + ":" + objClassName + "<br>"); ??????? }catch(Exception ex){} ??? } ??? StudentManager studentManager = this.getWebApplicationContext().getBean("studentManager"));
第二種是將struts的Action作為spring的一個JavaBean進行配置,在Action里面只需要定義一個Manager變量和相應的setter/getter方法,就可以通過spring為其注入一個Manager對象。這種方式太復雜,很牽強,個人覺得沒有什么實際意義,建議大家不必花費時間去學習。
spring與hibernate整合的原理: class MyController extend simpleformcontroller { ?public MyController() ?{ ??setCommandClass(Studnet.class); ?} } 首先回憶使用hibernate的步驟: 1.創(chuàng)建和配置Configuration對象,配置Congiuration對象可以通過hibernate.properties或hiberate.cfg.xml文件,也可以完全采用如下的編程方式: ????? configuration = new Configuration() ????? configuration.addClass(Student.class) ???????????? //.addFile("Student.hbm.xml") ???????????? //.addResource("/Student.hbm.xml") ???????????? .setProperty("show_sql","true"); 2.由configuration創(chuàng)建出SessionFactory對象:???????????? ??? SessionFactory sf = configuration.buildSessionFactory(); 3.通過SessionFactory獲得session對象,然后進行CRUD操作: ??? Session session = sf.openSession(); ??? session.save()\delete\update\load\ ??? session.close(); 使用spring集成hibernate的第一步就是借助spring的配置創(chuàng)建出SessionFactory對象 <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> ?<property name="mappingResource?> ??<list> ???<value>xx.hbm.xml</value> ???<value>yyy.hbm.xml</value> ??<list> ?</Property> ?<property name="hibernateProperties"> ??<props> ???<prop key="show_sql">true</prop> ??<props> ?</property> ?<property name="datasource"><ref locale=""/></property> </bean> 在LocalSessionFactoryBean內(nèi)部完全采用編程方式來配置configuration對象,而不再通過hibernate.properties或hiberate.cfg.xml配置文件,帶著大家查看了LocalSessionFactoryBean的源代碼。 有了SessionFactory,我們接著就可以通過spring將這個SessionFactory注入到DAO類中,配置如下: <bean id="studentDAO" class="cn.itcast.StudentDAO"> ?<property name="sessionFactory"> ??<ref bean="sessionFactory" /> ?</property> </bean> 相應的調(diào)用程序代碼示意如下: class StudentDAO { ?sessionFactroy; ?void setSessionFactory(sessionFactory) ?{ ??this.sessionFactory = sessionFactory; ?} ?insert(User user) ?{ ??sessionFactory.openSession();//累 ??session.save(user); ??session.close();//累 ?} } 我們還有自己openSession和getSession,這是不是很累呢?為此,spring又提供了一個配置類hibernateTemplate,它可以幫助我們?nèi)penSession和closeSession,這個配置類能夠openSession,顯然它一定要有SessionFactory的引用。 <bean id="hibernateTemplate" class="HibernateTemplate"> ?<property name="sessionFactory"> ??<ref bean="sessionFactory" /> ?</property> </bean> 遵循spring的IOC思想,我們接著還要將HibernateTemplate注入到DAO類中,配置如下: <bean id="studentDAO" class="cn.itcast.StudentDAO"> ?<property name="hibernateTemplate"> ??<ref bean="hibernateTemplate" /> ?</property> </bean>
class StudentDAO { ?hibernateTemplate; ?void sethibernateTemplate(hibernateTemplate) ?{ ??this.hibernateTemplate = hibernateTemplate; ?} ?insert(User user) ?{ ??hibernateTemplate.save(user);//我們不再openSession和closeSession,不是很爽嗎? ?} }
另外,spring還提供了一個HibernateSuport類,它可以通過注入的SessionFactory返回一個HibernateTemplate,我們的DAO類繼承這個HibernateSupport,也可以獲得HibernateTemplate對象進行CRUD操作,配置及示意代碼如下: <bean id="studentDAO" class="cn.itcast.StudentDAO"> ?<property name="sessionFactory"> ??<ref bean="sessionFactory" /> ?</property> </bean> class StudentDAO extends HibernateSuport { ?/*hibernateTemplate; ?void gethibernateTemplate() ?{ ??return hibernateTemplate; ?} ?sessionFactroy; ?void setSessionFactory(sessionFactory) ?{ ??this.sessionFactory = sessionFactory; ??hibernateTemplate = new HibernateTemplate(sessionFactory); ?}*/ ? ? ?insert(User user) ?{
??getHibernateTemplate().save(user); ?} }
?
最后由王澤佑提問引出spring mvc的注冊綁定的詳細講解: 在spring幫助文檔中搜索editor,可以看到有關注冊綁定的信息,CustomDateEditor是沒有被自動注冊的,需要用戶在initBinder方法內(nèi)部調(diào)用,示例代碼如下: protected void initBinder( ??????? HttpServletRequest request, ??????? ServletRequestDataBinder binder) ??????? throws ServletException { ??????? binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true)); ??? } 一種替換的方式及registerCustomEditor底層的實現(xiàn)內(nèi)幕: 調(diào)用BaseCommandController.setPropertyEditorRegistrars()方法,顯然這可以作為controller的屬性進行配置。 class MyPropertyEditorRegistrar { ?void registerCustomEditors(PropertyEditorRegistry registry) ?{ ??reguistry.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); ?} }
關于Date數(shù)據(jù)校驗的問題:通過在struts幫助文擋的Validator幫助頁面中搜索validate,是可以搜索到DateValidator的配置幫助的。
如果注冊了DateEditor,數(shù)據(jù)成功轉換成Date類型的字段,那么隨后的的Validator框架就沒必要對這個Date字段進行校驗了,因為Validator是對已裝配到Bean中的字符串類型的字段進行校驗,而數(shù)據(jù)都已經(jīng)成功裝配進了Date字段中,哪還有校驗的必要。框架把數(shù)據(jù)裝配到formbean里面,Validator從Formbean里面拿數(shù)據(jù)進行校驗。
<bean id="myController class="cn.itcast.MyController"> ?<property key="commandClass"> ??<value>cn.itcast.Student</value> ?</prperty> </bean> 上面的配置信息相當于下面的一段代碼,這其中就涉及到了屬性綁定的問題,要把字符串"cn.itcast.Student"轉換成Class對象。 Class clazz = Class.forName("cn.itcast.Student"); setCommandClass(clazz);
小記:通過spring配置文件中的<import>元素的resource屬性可以導入更多的配置文件,實現(xiàn)信息分散配置的目的。?
有網(wǎng)友在論壇上詢問:如何在使用GridView顯示主細表時添加刪除功能?其實,如果單個GridView能夠實現(xiàn)刪除對話,N個GridView嵌套也沒有任何問題。下面就將例子粘貼如下,望各位需要的朋友仔細揣摩一下,道理很簡單的:)
查看例子
aspx?:
<%
@?Page?Language
=
"
C#
"
?AutoEventWireup
=
"
true
"
?CodeFile
=
"
Default2.aspx.cs
"
?Inherits
=
"
Default2
"
?
%>
<!
DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html?
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head?
id
="Head1"
?runat
="server"
>
??
<
title
>
利用GridView顯示主細表并添加打開、關閉功能
</
title
>
??
<
style?
type
="text/css"
>
??td,div,a?
{
font-size
:
12px
}
??
</
style
>
??
<
script?
type
="text/javascript"
>
??
//
<![CDATA[
??
function
?ShowHidden(sid,ev) ??{ ????ev?
=
?ev?
||
?window.event; ????
var
?target?
=
?ev.target?
||
?ev.srcElement; ????
var
?oDiv?
=
?document.getElementById(
"
div
"
?
+
?sid); ????oDiv.style.display?
=
?oDiv.style.display?
==
?
"
none
"
?
"
block
"
:
"
none
"
; ????target.innerHTML?
=
?oDiv.style.display?
==
?
"
none
"
?
"
顯示
"
:
"
隱藏
"
; ??} ??
//
]]>
??
</
script
>
</
head
>
<
body
>
??
<
form?
id
="form1"
?runat
="server"
>
????
<
asp:GridView?
ID
="MasterGridView"
?runat
="server"
?AutoGenerateColumns
="false"
?Width
="780px"
??????BorderWidth
="1"
?OnRowDataBound
="MasterGridView_RowDataBound"
?DataKeyNames
="id"
??????ShowHeader
="false"
>
??????
<
Columns
>
????????
<
asp:TemplateField
>
??????????
<
ItemTemplate
>
????????????
<
div?
style
="width:?100%;?padding:?2px;?font-weight:?bold;?background-color:?#DEDEDE; ??????????????float:?left"
>
??????????????
<
span?
style
="float:?left"
>
欄目名稱:
<%
#
Eval
(
"
Title
"
)?
%>
</
span
><
span?
style
="float:?right; ????????????????color:?Red;?cursor:?pointer"
?onclick
="ShowHidden('<%#Eval("
id")?%
>
',event)">隱藏
</
span
></
div
>
????????????
<
div?
style
="background-color:?#FFF;?padding-left:?60px;?clear:?both"
?id
="div<%#Eval("
id")?%
>
"> ??????????????
<
asp:GridView?
ID
="DetailGridView"
?runat
="server"
?AutoGenerateColumns
="false"
?ShowHeader
="true"
????????????????HorizontalAlign
="left"
?DataKeyNames
="id"
?AutoGenerateDeleteButton
="true"
?OnRowCommand
="DetailGridView_RowCommand"
????????????????OnRowDeleting
="DetailGridView_RowDeleting"
?Width
="720px"
>
????????????????
<
HeaderStyle?
BackColor
="#9999FF"
?
/>
????????????????
<
Columns
>
??????????????????
<
asp:TemplateField?
HeaderText
="文章名稱"
?HeaderStyle-Width
="540px"
>
????????????????????
<
ItemTemplate
>
??????????????????????
<
a?
href
="/article/<%#Eval("
objectGuid")?%
>
/read.aspx"> ????????????????????????
<%
#
Eval
(
"
Title
"
)?
%>
??????????????????????
</
a
>
[
<%
#?
Eval
(
"
HitCount
"
)?
%>
] ????????????????????
</
ItemTemplate
>
??????????????????
</
asp:TemplateField
>
??????????????????
<
asp:BoundField?
HeaderText
="發(fā)布日期"
?DataField
="CreateDate"
?HtmlEncode
="false"
?DataFormatString
="{0:yyyy年MM月dd日}"
????????????????????HeaderStyle-Width
="100px"
?ItemStyle-HorizontalAlign
="Center"
?
/>
??????????????????
<
asp:CommandField?
DeleteText
="刪除"
?ShowDeleteButton
="true"
?ButtonType
="Button"
?HeaderStyle-Width
="40px"
?
/>
????????????????
</
Columns
>
??????????????
</
asp:GridView
>
????????????
</
div
>
??????????
</
ItemTemplate
>
????????
</
asp:TemplateField
>
??????
</
Columns
>
????
</
asp:GridView
>
??
</
form
>
</
body
>
</
html
>
?
C#:
using
?System;
using
?System.Data;
using
?System.Configuration;
using
?System.Collections;
using
?System.Web;
using
?System.Web.Security;
using
?System.Web.UI;
using
?System.Web.UI.WebControls;
using
?System.Data.OleDb;
using
?System.Web.UI.HtmlControls;
public
?
partial
?
class
?Default2?:?System.Web.UI.Page { ??
string
?ConnectionString?
=
?
@"
Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=|DataDirectory|aspxWeb.mdb;Persist?Security?Info=True
"
; ??OleDbConnection?cn1;
??
protected
?
void
?Page_Load(?
object
?sender,?EventArgs?e?) ??{ ????
if
?(
!
Page.IsPostBack) ????{ ??????OleDbConnection?cn?
=
?
new
?OleDbConnection(ConnectionString); ??????cn.Open(); ??????cn1?
=
?
new
?OleDbConnection(ConnectionString); ??????cn1.Open(); ??????OleDbCommand?cmd?
=
?
new
?OleDbCommand(
"
select?*?from?[Subject]
"
,?cn); ??????OleDbDataReader?dr?
=
?cmd.ExecuteReader(CommandBehavior.CloseConnection); ??????MasterGridView.DataSource?
=
?dr; ??????MasterGridView.DataBind(); ??????dr.Close(); ??????cmd.Dispose(); ??????cn.Dispose(); ??????cn1.Dispose(); ??????cn?
=
?cn1?
=
?
null
; ????} ??} ??
protected
?
void
?MasterGridView_RowDataBound(?
object
?sender,?GridViewRowEventArgs?e?) ??{ ????
if
?(e.Row.RowType?
==
?DataControlRowType.DataRow) ????{
??????GridView?oGridView?
=
?(GridView)e.Row.FindControl(
"
DetailGridView
"
); ??????
if
?(oGridView?
!=
?
null
) ??????{ ????????OleDbCommand?cmd?
=
?
new
?OleDbCommand(
"
select?top?10?*?from?Document?Where?pid?=?
"
?
+
?MasterGridView.DataKeys[e.Row.RowIndex].Value,?cn1); ????????OleDbDataReader?dr1?
=
?cmd.ExecuteReader(); ????????oGridView.DataSource?
=
?dr1; ????????oGridView.DataBind(); ????????dr1.Close(); ????????cmd.Dispose(); ??????} ????} ??}
??
protected
?
void
?DetailGridView_RowDeleting(?
object
?sender,?GridViewDeleteEventArgs?e?) ??{ ????GridView?a?
=
?(GridView)sender; ????Response.Write(
"
您要刪除的記錄是:<font?color='red'>
"
?
+
?a.DataKeys[e.RowIndex].Value.ToString()?
+
?
"
</font> TODO:執(zhí)行刪除動作
"
); ????
//
?TODO:執(zhí)行刪除動作
??} ??
protected
?
void
?DetailGridView_RowCommand(?
object
?sender,?GridViewCommandEventArgs?e?) ??{
??} }
注意:EnableViewState="true" 是必須的。
摘要: 在實際工作中,往往需要合并表格頭部的單元格,下面就是一個實現(xiàn)的例子。運行結果如圖:
C#
<%
...
@?Page?Language
=
"
C#
... 閱讀全文
今天看了兩個學生的博客,李曾鷹和邱新興的。很勤奮的兩個學生,也很努力。正走著十幾年前我曾走過的路,滿懷希望和憧憬。祝福他們!也想呼吁所有的學軟件的同學:熱愛技術吧,它也會愛你們的。 ?????????也許是因為身在學校的緣故吧,竟然讓我這個年逾三十已久的老人還能在這里談技術。這也許也算是我能安心在這個并不肥沃的土地上依然耕耘不輟的原因之一吧。象牙塔里的技術畢竟純粹,沒有雜質(zhì),滿足自己十幾年的虛榮,可以幼稚地以為還停留在那個“程序員英雄”的時代。 ?????????逝者如斯夫!
轉自:http://www.cnblogs.com/jplee/archive/2006/11/13/559612.html
說明: 這個手冊只是為初學者制作的環(huán)境配置和快速開發(fā)的一些基本方法,為的是廣大愛好者能快速上手,少走我走過的彎路,里面沒有任何關于java模式和其他相關的說明。本人水平有限,也沒有能力去討論關于模式設計和開發(fā)細節(jié)的一些內(nèi)容。
? ? 建議:
如果你還不清楚struts和hibernate的一些基本原理,希望能先去了解一下這方面的相關內(nèi)容。 推薦:
Hibernate中文手冊》作者認為要學Hibernate看這個就足夠了,里面幾乎包括了所有的細節(jié),不過可能不太適合快速入門。
地址:http://www.hibernate.org/hib_docs/v3/reference/zh- cn/html_single/
關于struts的資料就很多了,這里推薦一個可以下載一些入門教程的網(wǎng)站。
地址:http://www.wnetw.com/jclub/index.jsp
強烈建議入門的朋友先了解一下基本的原理!否則本文可能對你沒有任何幫助。 相關工具下載:(注意版本)
mysql5.0 http://www.mysql.org eclipse 3.1.1 http://www.eclipse.org myeclipse4.0.3 http://www.myeclipseide.com tomcat5.5 安裝:
關于tomcat和mysql的安裝就不多說了,需要注意的是最好保證你的 jdk是1.5的版本,并配置好你的環(huán)境變量,不然可能會遇到一些問題。
把eclipse解開,再去安裝剛下載的myeclipse,在安裝的時候需要把路徑指定到剛才解開的eclipse上,由于myeclipse是個收費軟件,所以需要注冊。不過一般按照Chinese的習慣,去google一個注冊碼就可以了:} 開發(fā)環(huán)境部署:
好了,現(xiàn)在保證你的mysql和tomcat服務能夠正常啟動,myeclipse能夠正常打開(如果不能,可以去找一下相關的說明或者給作者留言)。下面我們就要開始真正的開始部署一個傳說中的tomcat+struts+hibernate+mysql結構的工程了!(faint!前言就寫的我好累)
首先,在myeclipse里新建一個工程。在左邊的Package Exporler面版里點右鍵選擇new->project…
在跳出菜單里選擇MyEclipse->J2EE Projects->Web Project。
點擊next后進入如下畫面: 
工程名為:test
結束后點擊Finish。
好了,如果成功的話你就會在 Package Exporler里看到一個新的test工程!現(xiàn)在我們先配置一下數(shù)據(jù)庫方面的東西。首先在你的mysql 里建立一個數(shù)據(jù)庫webases,再在里面新建一個表admin,里面三個字段分別為id,name,password其中id為自動取值的主鍵(mysql具體的操作可以自己找資料,不是本文涉及范圍)。
再回到myeclipse ,選中window->Open Perspective->Other…
可以看到現(xiàn)在跳出一個名為Select Perspective的菜單,在里面選中MyEclipse Databases Exporler,可以看到現(xiàn)在到了下面的頁面。

按以上圖示輸入相關字段后點擊Finish便建立了一個數(shù)據(jù)庫連接,在新出現(xiàn)的JDBC for Mysql上點右鍵,選擇Open connection…,確認用戶名和密碼正確后點OK,如果一切順利的話你會看到下面的畫面: 
這說明你已經(jīng)和數(shù)據(jù)庫建立了正確的連接。現(xiàn)在我們再回到window->Open Perspective- >Other…里的MyEclipse,也就是我們剛進來的時候看到的畫面。
右鍵點擊你剛建立的工程 test并選擇MyEclipse->Add struts Capabilities…在跳出的菜單里按照如下輸入并確定: 
好了,現(xiàn)在你已經(jīng)為你的工程增加了struts,接下來和上面一樣在右鍵工程后選擇MyEclipse- >Add Hibernate Capabilities…一路確定下來為你的工程添加Hibernate。(為方便起見我們在選擇路徑時把HibernateSessionFactory.java放在了src/com下面,其實最好建立個單獨的目錄如 src/com/hibernate)
為了更好的演示我們不建立通常的登陸頁面而是建立個注冊頁面。選擇 src目錄下的hibernate.cfg.xml文件。照如下填寫并保存。這樣hibernate就為你建立了數(shù)據(jù)庫的連接池。 
下面我們再選擇WebRoot/WEB-INF/struts-config.xml文件,在畫面中點擊右鍵選擇new- >Form, Action and JSP。如下填寫 
再選擇JSP選項,如下 
最后選擇Finish。
再新建一個一個success.jsp的頁面,
在剛才struts- config.xml文件里右鍵選擇addAdmin選擇Properties,在菜單里選擇Forwords,再點add,如下圖填寫 
最后你的struts-config.xml就是下面這個樣子:

下面我們轉到hibernate。換到剛才我們建立數(shù)據(jù)庫的頁面,選擇你的admin的表點右鍵選擇Create Hibernate Mapping。選擇好打包路徑后選擇Finish。如圖:

在你剛才選擇的路徑下(我為方便是src/com/yourcompanyname/)下新建立的文件 AdminDAOFactory.java文件并輸入以下內(nèi)容: package com.yourcompanyname; import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.hibernate.SessionFactory; public class AdminDAOFactory { ?Session session; ?Transaction tx; ?public void add(Admin admin) throws HibernateException { ??/** ?? * Creation Date: 11-17-2005 ?? * TODO Add a new admin user. ?? * @param An object of Admin ?? * @return void ?? * @author Coder Guo ?? */ ??try { ???session = SessionFactory.currentSession(); ???tx = session.beginTransaction(); ???//Add a new admin ???session.save(admin); ???tx.commit (); ??}catch(HibernateException e){ ???throw e; ??}finally{ ???if (tx!=null) { ????tx.rollback(); ???} ???SessionFactory.closeSession(); ??} ?} } ?再打開com.yourcompany.struts.action下的AddAdminAction.java添加(其中如果有錯誤選中好按ctrl+shift+o自動添加包) public class AddAdminAction extends Action { ?// --------------------------------------------------------- Instance Variables ?// --------------------------------------------------------- Methods ?/** ? * Method execute ? * @param mapping ? * @param form ? * @param request ? * @param response ? * @return ActionForward ? * @author Coder Guo ? */ ?public ActionForward execute( ??ActionMapping mapping, ??ActionForm form, ??HttpServletRequest request, ??HttpServletResponse response) { ??AddAdminForm addAdminForm = (AddAdminForm) form; ?? ??// TODO Add a new admin ??Admin admin = new Admin(); ??admin.setName(addAdminForm.getName ()); ??admin.setPassword(addAdminForm.getPassword ()); ??AdminDAOFactory adminDAO = new AdminDAOFactory (); ??adminDAO.add(admin); ?? ??return mapping.findForward("success"); ?} } ?再打開com.yourcompanyname.struts.form下的AddAdminForm.java,修改(如果有錯誤按照上面說的方法導入包) ?public ActionErrors validate( ??ActionMapping mapping, ??HttpServletRequest request) { ??// TODO Auto-generated method stub ??ActionErrors errors = new ActionErrors(); ?? ??Session session = SessionFactory.currentSession(); ??Transaction tx = session.beginTransaction (); ??Query query = session.createQuery("select admin from Admin as admin where admin.name = '" + this.name + "'"); ??Iterator it = query.iterate (); ??if (it.hasNext()){ ???errors.add ("addAdmin.err.name",new ActionMessage("form.addAdmin.err.name")); ??} ??tx.commit(); ??SessionFactory.closeSession (); ??return errors; ?} ?public void reset(ActionMapping mapping, HttpServletRequest request) { ??// TODO Auto-generated method stub ??this.name=null; ??this.password=null; ?} 再打開com\yourcompanyname\struts下的ApplicationResource.properties在這里面添加錯誤信息:
Form.addAdmin.err.name=err 最后,(汗,好累啊-_-!)打開addAdmin.jsp修改成如下:
<%@ page contentType="text/html; charset=utf-8"%> <%@ page language="java"%> <%@ taglib uri="<%@ taglib uri="<script language = "javascript"> <!-- function check(){ ?if (loginForm.userName.value == "" || loginForm.password.value == ""){ ??alert("請輸入完整的信息!"); ??loginForm.userName.focus(); ??return false; ?} } //--> </script> ? ?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html> ? <head> ??? <html:base /> ??? ??? <title>login.jsp</title> ??? <link href="css/webcss.css" rel="stylesheet" type="text/css"> ??? ??? <meta http-equiv="pragma" content="no- cache"> ??? <meta http-equiv="cache-control" content="no- cache"> ??? <meta http-equiv="expires" content="0">??? ??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> ??? <meta http- equiv="description" content="This is my page"> ? </head> ? ? <body> ? <center> ? ?<p> </p> ? ?<p> </p> ? ?<table width="300" border="0" cellpadding="0" cellspacing="0"> ? ?<html:form action="/addAdmin" focus="name" method="GET"> ??? ?<tr align="center" valign="middle"> ????? ?<td colspan="2" class="typt_normal">新增管理員</td> ??? ?</tr> ??? ?<tr> ????? ?<td width="100" align="center" valign="middle" class="typt_normal">名稱: </td> ????? ?<td width="200" align="left"><html:text property="name" styleClass="text_s"/><html:errors property="addAdmin.err.name"/></td> ??? ?</tr> ??? ?<tr> ????? ?<td width="100" align="center" valign="middle" class="typt_normal">密碼: </td> ????? ?<td width="200" align="left"><html:password property="password" styleClass="text_s"/></td> ??? ?</tr> ??? ?<tr>? ????? ?<td colspan="2" align="center" valign="middle"><html:submit value="提交" onclick="return check ();"/><html:reset value="重置"></html:reset></td> ????? ?</tr> ?</html:form> ? ?</table> ?</center> ?</body> </html:html>
其中可以看到如何在struts的標簽中使用javascript的方法。
配置好myeclipse于tomcat的連接。在window->Preferences做如下設定: 
在項目文件點右鍵->“myeclipse”->“Add and remove project deployment”,如下圖: 
好了,我們的配置工作基本結束了,在myeclipse上開啟tomcat服務 
現(xiàn)在打開瀏覽器,輸入
http://127.0.0.1:8080/test/addAdmin.jsp就可以看到你的jsp頁面了! ?
相信大家都使用過MSN,QQ這樣的即時聊天類軟件,對于它們的好友上線提示功能并不陌生吧?從屏幕右下角彈出一個小界面,慢慢上升,最后消失。我們能不能在自已的程序中也做出相同的功能呢?能!筆者現(xiàn)用JAVA和eclipse的SWT用戶界面組件實現(xiàn)這個功能。
什么是SWT呢?
SWT原來是eclipse項目組為開發(fā)eclipse IDE所編寫的圖形界面API,運行時,其先判斷本機是否有相同的界面元素,如果有則直接調(diào)用顯示,如沒有才進行模擬顯示。其運行機制使速度比AWT,SWING快很多。
了解更多請看:http://www.eclipse.org/swt
編寫思路
先取得用戶屏幕大小,用屏幕高度減去popup界面的高度計算出popup界面在屏幕顯示的最高位置(當界面移動到此位置時就停止移動)。
Rectangle area = Display.getDefault().getClientArea(); int upPosition = area.height - 100;
|
用屏幕高度加上popup界面的高度就計算出popup界面的初始位置(初始化時不可見,然后慢慢上移到upPosition點后停止移動,再顯示若干秒后消失)。
int downPosition = area.height + 100;
|
移動位置我們用線程實現(xiàn),當初始化界面后,調(diào)用start()方法運行此線程,在線程中循環(huán)判斷downPosition的大小是否小于upPosition,如果小于的話說明還未到停止的時候,設置popup界面的邊框為downPosition,并暫停10毫秒,如果downPosition大于upPosition的,說明popup界面已移動到了最高位置。調(diào)用sleep()暫停5秒鐘后關閉界面并退出程序。就這么簡單,ok, Let's go! 下面給出整個程序代碼:
描述:
(Test為主界面,點擊上面的button后,調(diào)用Popup在右下角顯示像MSN和QQ一樣的popup界面。)
圖一為源代碼中的實現(xiàn),圖二為修改過后的界面(和QQ的有點像吧。)
圖一 圖二
|
源代碼:
// Test.java //主界面,其中只有一個button,當點擊時調(diào)用Popup在右下角顯示像MSN和QQ一樣的popup界面。
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell;
public class Test {
public static void main(String[] args) {
final Display display = new Display(); Shell shell = new Shell(); shell.setText("aaa"); shell.setSize(250, 150);
final Button button = new Button(shell, SWT.NONE); button.setBounds(50, 20, 100, 25); button.setText("button"); //監(jiān)聽button的事件,當用戶點擊時調(diào)用Popup類顯示popup界面。 button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { //實例化popup類,構造函數(shù)為popup界面中出現(xiàn)的提示信息。 Popup popup = new Popup("您的好友xxx上線了。"); popup.start(); } });
shell.open();
while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }
// Popup.java //實現(xiàn)像MSN,QQ一樣的好友上線通知popup
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text;
public class Popup extends Thread {
Shell shell;
protected int moveStep = 2; //每次移動的pixel protected int upPosition; //能移動到的最上面坐標 protected int downPosition; //當前popup的邊框坐標 protected int leftPosition; //popup左邊邊框坐標 public Popup(final String message) {
shell = new Shell(SWT.ON_TOP); Text text = new Text(shell, SWT.MULTI | SWT.WRAP); text.setBounds(10, 20, 180, 80); text.setBackground(shell.getBackground()); text.setText(message);
//取屏莫大小 Rectangle area = Display.getDefault().getClientArea();
upPosition = area.height - 100;//計算出popup界面在屏幕顯示的最高位置 downPosition = area.height + 100;//計算出popup界面的初始位置 leftPosition = area.width - 180;
shell.setSize(180, 100);
//初始化popup位置 shell.setLocation(leftPosition, downPosition);
shell.open();
}
public void run() {
Display display = shell.getDisplay(); while (true) { try { Thread.sleep(10);
//判斷當前位置是否小于能出現(xiàn)的最高位置,小于的話就說明還可以向上移動。 if ((downPosition - moveStep) > upPosition) { display.asyncExec(new Runnable() { public void run() { shell.setLocation(leftPosition, downPosition- moveStep); downPosition -= moveStep; } }); //此時已經(jīng)移動到了最高位置,顯示5秒鐘后,關閉窗口并退出。 } else { Thread.sleep(5000); display.asyncExec(new Runnable() { public void run() { shell.dispose(); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } } }
|
上面的源程序就完成了圖一中的功能,讀者可以自行修改,讓其界面,功能更強大。
|
|