2013年7月30日 星期二

Menu 淺談

在Android 中的Menu可以使用XML來開發,在Menu.xml 中可以新增Item1,Item2 





<menu 
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item android:id="@+id/item1" android:title="item1"></item>
    <item android:id="@+id/item2" android:title="item2"></item>


</menu>


在模擬器呈現出來的結果如下。


在Activity 中要使用的方法:



其中item.getItemid()會傳出一組序號來辨識所選擇的是哪一個item,可以用R.id.item1 來辨視鎖選擇的item



2013年7月29日 星期一

Menu 的事件觀察


  1. 先Create基本的<LinearLayout> ,



基本配置如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

回到Activity 觀察:
onCreateOptionsMenu 事件中,寫了一個Log.i 觀察
Log.i("Jacky","onCreateOptionsMenu");

public boolean onCreateOptionsMenu(Menu menu) 
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
Log.i("Jacky","onCreateOptionsMenu");
return true;

} 

onMenuItemSelected 事件中,寫了一個Log.i 觀察

public boolean onMenuItemSelected(int featureId, MenuItem item)
 {
// TODO Auto-generated method stub
Log.i("Jacky","onMenuItemSelected");
return super.onMenuItemSelected(featureId, item);
}

onOptionsItemSelected事件中,寫了一個Log.i 觀察
public boolean onOptionsItemSelected(MenuItem item) 
{
// TODO Auto-generated method stub
Log.i("Jacky","onOptionsItemSelected");
return super.onOptionsItemSelected(item);

}

執行程式,按下模擬器的Menu


在Log 中,Text 呈現onCreateOptionMenu ()

按下Setting 鍵後


2013年7月14日 星期日

Servlet 的部署描述檔

在Tomcat 伺服器下對應到根目錄"/" 的預設環境是servlet_root/webapps/ROOT,這表示在servlet_root/webapps/ROOT/WEB_INF/classes目錄下的servlet 都可以用類似servlet/HelloWorld
來存取。

在WEB_INF目錄下的web.xml 稱為部署描述檔,這是一個具有標準DTD的XML檔。其中也包含了控制servlet 控制的行為。


  
擷取web.xml 中相關設定來看

<servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>Servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/hello.html</url-pattern>
    </servlet-mapping>


我們可以透過執行http://localhost:8084/FirstServlet/hello.html 來啟動HelloServlet,藉由設定servlet name 的關係,找到對應的servlet

2013年7月13日 星期六

JSTL 概述

JSTL 是JSP Standard Tag Library 的縮寫。所謂的標籤是指JSP 作為Java EE 分層程式中顯示層的一種顯示方案。 Model 1 (模式一):早期的Java Web 應用程式是不分階層的,也因為業務沒有分層,程式碼顯得凌亂也不易維護。 Model 2 (模式二):將Web 分為幾個層,每個層處理不同事情,比較經典的分層結構為MVC (Model -View-Control)。在MVC 架構中Model 處理業務相關,View 處理顯示相關,而Control 協調Model層與View 層。Control 層中一般為Servlet,View 層為JSP 。 在JSP中可以使用Scriptlet 指令稿來完成資料顯示;但是Scriptlet 如果與JSP 中的HTML混在一起,可讀性與可維護性將變差,如果要實現複雜的顯示功能,Scriptlet 也不方便。而JSP標籤與HTML 相似,遵循XML與法,將使JSP 顯得簡潔,乾淨。 從JAVA EE 5開始,JAVA EE 內建對JSTL 的支援,JSP中可直接使用,無須再設定web.xml。 Core標籤函式庫是JSTL 的核心標籤函式庫。實踐了基本功能。 標籤 JSTL 中以out 標籤來顯示資料,例如。

2013年5月29日 星期三

JSP 網頁下,提供USER下SQL指令

利用Mysql 建立 一個資料庫,利用JDBC 連上資料庫
使用者可於Textarea 下使用SQL 語法


<%@page import="com.mysql.jdbc.Driver"%>
<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"
    import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=uft-8">
<title>教育訓練管理系統</title>
</head>
<body>

<table width="2" ><tr > <td></td>
<form action=SQL.jsp>

    <textarea cols=200 rows=15 name="sqltext" width=1
    text=<%=request.getParameter("sqlText")%>>
   
    </textarea>
    <input type="submit" value="ok"/>
</form>
</tr>
</table>

<%
try
{
String sqlText=request.getParameter("sqltext");
out.write(sqlText);
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver is loaded");
Connection c =DriverManager.getConnection("jdbc:mysql://localhost:3306/db","user","pwd");
Statement s = c.createStatement();
//sqlText="Select idEducation id from education";
        ResultSet rs = s.executeQuery(sqlText);
        ResultSetMetaData rsmeta = rs.getMetaData();

        //out.print("rs.getStatement()="+rs.getStatement());
        //out.print("rs.getArray(0)"+rs.getArray(0));
       
        int cols = rsmeta.getColumnCount();
        //out.print(""+cols);
        out.print("<Table Border=1>");
    out.print("<tr>");
   
    for(int i=1; i<=cols; i++)
        {
          out.print("<th>");
          out.print(rsmeta.getColumnLabel(i));
          out.print("</th>");
        }
    out.print("</tr>");
    //rs.first();
        while(rs.next()) // Retrieve data from ResultSet
       {          
        out.print("<tr>");
        for (int i=1;i<=cols;i++)
        out.print("<th border=1>"+rs.getString(i)+"</th>");      
           
        out.print("</tr>");
        }
        out.print("</Table>");
}catch(Exception e)
{
System.out.println("Exception : " +e);
}
%>

</body>
</html>

2013年4月20日 星期六

Linklist 範例




LinkedList 使用範例


package dispatch;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

public class Dispatch {

/**
* @param args
*/
String L[]={"40","39","38","37","26","25","12","11","4","3","10","9","24","23","60","59",
"7","8","22","21","58","57","19","20","53","54","55","56"};
 
String R[]={"41","42","43","44","27","28","29","30","13","14","5","6","15","16","31","32","45","46"
  , "2","1","18","17","33","34","47","48","36","35","52","51","50","49"  
};

LinkedList <String> maps;
public void  process()
{
maps=new LinkedList<>();
HashMap map=new HashMap<>();
for(int i=0;i<L.length;i++)
{
maps.add(L[i]);
}

while (!maps.isEmpty())
{System.out.print(maps.remove());
System.out.print("\t");
}


}


public static void main(String[] args) {
// TODO Auto-generated method stub
new Dispatch().process();

}

}


2013年4月16日 星期二

List 介面使用


import java.util.LinkedList;
import java.util.List;


public class ListExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List list1= new LinkedList<>();
list1.add("abc");
list1.add(new Integer(3));
list1.add(new Boolean(true));
list1.add(new Integer(3));
System.out.print("LinkedList:"+list1);

}

}