2011年11月9日 星期三

WSDL2Java 用法


1. 首先必須先下載地三方套件axis.zip,可到下面網址載
   http://www.giveawayoftheday.com/download+axis.zip/
   將 axis.zip 解壓縮至個人電腦中的某一位置 (如 C:\)
   解壓縮後在 C:\ 會出現一個 axis 目錄


2. 新增環境變數 (我的電腦按右鍵 --> 內容 --> 進階 --> 環境變數)
   a.
   AXIS_HOME=C:\axis  (即剛才解壓縮之目錄)

   b.
   AXIS_CLASSPATH=%AXIS_HOME%\WEB-INF\classes;%AXIS_HOME%\WEB-INF\lib\axis.jar;%AXIS_HOME%\WEB-INF\lib\axis-ant.jar;%AXIS_HOME%\WEB-INF\lib\commons-discovery-0.2.jar;%AXIS_HOME%\WEB-INF\lib\commons-logging-1.0.4.jar;%AXIS_HOME%\WEB-INF\lib\jaxrpc.jar;%AXIS_HOME%\WEB-INF\lib\log4j-1.2.8.jar;%AXIS_HOME%\WEB-INF\lib\saaj.jar;%AXIS_HOME%\WEB-INF\lib\wsdl4j-1.5.1.jar;%AXIS_HOME%\WEB-INF\lib\activation.jar;%AXIS_HOME%\WEB-INF\lib\mail.jar;%AXIS_HOME%\WEB-INF\lib\xmlsec-1.4.4.jar

   c.
   CLASSPATH=.;%AXIS_CLASSPATH%


3. 執行方法
   java org.apache.axis.wsdl.WSDL2Java wsdl檔案全名 -o 相對目錄名稱
   
   例如:
   C:\>java org.apache.axis.wsdl.WSDL2Java outbound.wsdl -o wsdl_to_java_temp
   即代表會把 outbound.wsdl 轉換完成的檔案放在 C:\wsdl_to_java_temp 底下

JSTL 標籤

<c:out> 標籤
  <b>Hello, <c:out value="${user}" default="guest"/>.</b>
  可指定default值,避免 EL 表示式 user 之值為 null

  <b>Hello, <%=user%>.</b>
  <b>Hello, ${user}.</b>
  若 user 為 null 以上兩種方式,則 output 為 Hello, ______.

<c:forEach> 標籤
  以下兩個寫法結果都一樣,但使用 <c:forEach> 標籤卻精簡多了
  1.
  <table>
    <%
    String[] items = (String[]) request.getAttribute("movieList");
    String var = null;
    for (int i = 0; i < items.length; i++) {
      var = items[i];
    }
    %>
    <tr>
      <td><%= var %></td>
    </tr>
  </table>

  2.
  <table>
    <c:forEach var="movie" items="${movieList}">
      <tr>
        <td>${movie}</td>
      </tr>
    </c:forEach>
  </table>

  可用巢狀迴圈,範例如下:
  Servlet 程式碼
    String[] ary1 = {"a", "b", "c", "d"};
    String[] ary1 = {"e", "f", "g", "h"};
    java.util.List arrayList = new java.util.ArrayList();
    arrayList.add(ary1);
    arrayList.add(ary2);
    request.setAttribute("arrays", arrayList);

  JSP 程式碼
    <table>
      <c:forEach var="arrayEl" items="arrays">
        <c:forEach var="item" items="arrayEl">
          <tr>
            <td>${item}</td>
          </tr>
        </c:forEach>
      </c:forEach>
    </table>

<c:if> 標籤
  <c:if test="${user == 'admin'}">
    <jsp:include page="test.jsp">
  </c:if>
  上面的敘述代表若 user 變數值為 "admin"
  則 include test.jsp 頁面到網頁中
  但此標籤無法做到 else 的敘述,下一組標籤可以做到此需求

<c:choose>, <c:when>, and <c:otherwise> 標籤
  用法範例如下
    <c:choose>
        <c:when test="${a == 'test1'}">
            output string1
        </c:when>
       
        <c:when test="${a == 'test2'}">
            output string2
        </c:when>
       
        <c:otherwise>
            output string2
        </c:otherwise>
    </c:choose>

  以上被 <c:choose> 包住的判斷,包括<c:otherwise>,只會有一個被執行

2011年10月30日 星期日

JSP Include

要在 JSP 裡插入固定頁面有兩種方式
1. JSP 指令
    <%@ include file=”Header.jsp”%>
2. JSP 標準動作
    <jsp:include page=”Header.jsp” />


很多時候這兩種方式的結果是完全相同的,但是內部 Container 的處理方式是有點不一樣的。


「指令」處理 include 頁面的時機是第一次被讀取到的時候(Translation Time)


「標準動作」處理 include 頁面的時機是每次被讀取到的時候(Run Time)


此外,被 include 之 JSP 檔案中必須注意不可有 <html> 或 <body>,否則會造成母頁面重覆出現 <html> 及 <body> 導致網頁讀取不正確。

2011年10月24日 星期一

EL 隱含物件

對應各種作用域屬性之 Map
    pageScope
    requestScope
    sessionScope
    applicationScope

對應於屬性請球參數之 Map
    param
    paramValues (參數有多個值時可利用陣列方式存取)
       ${paramValues.food[0]}
       ${paramValues.food[1]}

對應於請求標頭 Map
    header
    headerValues (參數有多個值時可利用陣列方式存取)

cookie 請求
    cookie

Context 之初始參數 Map
    initParam

指向 pageContext 之實際參考
    pageContext

2011年10月23日 星期日

當 Form 之 action 指向 JSP 而不是 Servlet


<html>
<body>
<form action="Test.jsp">
  name: <input type="text" name="userName">
  ID#: <input type="text" name="userID">
  <input type="submit">
</form>
</body>
</html>

Test.jsp 以標準動作接收之方式為:
<jsp:useBean name="person" type="foo.Person" class="foo.Employee">
    <jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>

此外,若 html 指定之 name 與 bean 之屬性同名,則可以不用指定 param 參數
<jsp:useBean name="person" type="foo.Person" class="foo.Employee">
    <jsp:setProperty name="person" property="name" />
</jsp:useBean>

若所有的特性皆與 bean 之屬性相同,則可用 * 代替
<jsp:useBean name="person" type="foo.Person" class="foo.Employee">

    <jsp:setProperty name="person" property="*"/>

</jsp:useBean>

若要展示的屬性不是基本資料型別或 String 時,如 Pearson 中的 Dog 屬性之 name 屬性,假設
  Person class 具有 String 型態之 "name" attribute
  Person class 具有 Dog 型態之 "dog" attribute
  Dog class 具有 String 型態之 "name" attribute


不使用標準動作 : (可正常運作)
<%= ((foo.Person) request.getAttribute("person")).getDog().getName%>

使用標準動作 : (會印出dog類別之toString()回傳值)
<jsp:useBean id="person" class="foo.Person" scope="request" />
Dog name is : <jsp:getProperty name="person" property="dog" />

但若要符合不使用 scripting 原則又要能正確印出結果,可利用 EL 完成 :
Dog name is : ${person.dog.name}

2011年10月18日 星期二

EL & Scripting 評算整理表格

EL 表示式評算
DD組態
<el-ignored>
page指令
isELIgnored
評算 忽略
未指定 未指定
false 未指定
true 未指定
false false
false true
true false

Scripting 元素評算
DD組態
<scripting-invalid>
評算 錯誤
未指定
true
false

JSP 元素種類

1. Scriptlet : <% .... %>
     <% 與 %> 之間可以插入一般的 Java 程式碼
     例如
     <%
          int a = 100; // 區域變數
          for (int i = 0; i < a; i++) {
                .........
          }
     %>

2. Directive : <%@ ... %>
    共分為三種
    <%@ page ....%>
      例如
        <%@ page import = "foo.*" %>

    <%@ taglib ....%>
      定義 JSP 使用的標籤函式庫
      例如
      <%@ taglib tagdir="/WEB-INF/rags/cool" prefix="cool" %>

    <%@ include ....%>
      可在 JSP 頁面固定載入一段文字或程式碼,建立一個可重複使用的區塊
      例如
      <%@ include file="xxxxxx.html" %>
   
3. Expression : <%= ... %>
    一般運算式,結尾不加分號
    例如
    <%=Math.random()%>
    <%=count%>
    <%= 27 %>
    <%= "27"%>

4. Declaration : <%! ... %>
    宣告 JSP 轉換出來的 Servlet 類別中的成員,成員即包括變數及方法。
    與 scriptlet 中宣告的區域變數不同。
    例如
    <%!
        int a = 0; //類別變數
    %>
    <!%
        int test(int a) {
             int b = a + 1;
             return b;
        }
    %>

5. Action : <xxx: ... >
    ● Standard Action
      <jsp:include page="test.jsp" />
      <jsp:useBean id="person" class="foo.Employee" scope="request"/>
      <jsp:getProperty name="person" property="name">
      <jsp:setProperty name="person" property="name" value="Fred">

    ● Others
      <c:set var="rate" value="32" />