2013年4月6日 星期六

struts2 - Chapter 3 Action、ActionSupport、validate


Strut2其中一個重要的環節就是Action,首先我們來看Action介面:
public interface Action {
    public static final String SUCCESS = "success";
    public static final String NONE = "none";
    public static final String ERROR = "error";
    public static final String INPUT = "input";
    public static final String LOGIN = "login";
    public String execute() throws Exception;
}

除了定義五個常用的String外,還有一個execute()方法。

這是作為之後繼承Action的預設方法,像上個範例的method="login"如果沒有這行的話,預設就是呼叫execute,回傳SUCCESS,而<result>如果沒有宣告name的話,預定也是接收"success",接下來我們看最常用的ActionSupport


public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable

除了實作Action外,還有常用的驗證、錯誤消息等等,ActionSupport實現了validate()方法,我們可以將想驗證的邏輯覆寫validate(),在UserLoginAction覆寫validate()如下:

@Override
 public void validate() {
  if(getAccount().length()==0){
   addFieldError("account", "請輸入account");
  }
  if(getPassword().length()==0){
   addFieldError("password","請輸入password");
  }
 }


    當有驗證到錯誤發生時,會預設傳回return INPUT,接著我們在剛剛的index.jsp中將account和password不輸入值後送出,會得到以下畫面:




    會根據在struts.xml中
<result name="input">/index.jsp</result>
跳回index.jsp,以上簡單的驗證就完成了,當然,除了把錯誤訊息放在code中,我們也可以放在一個屬性文件.properties中,以便日後的維護和管理方便。
建立一個UserLoginAction.properties,並放置在user.action包下,並設定內容:

account.required=請輸入account
password.required=請輸入password

並將
addFieldError("account", "請輸入account");
修改成
addFieldError("account", getText("account.required"));
就能讀取到了。
若是你使用eclipse,可以去下載一個插件Properties Editor,這樣就可以在eclipse編輯properties時使用中文訊息。
除了addFieldError外還有addActionError()和addActioMessage(),你可以在validate()中加入這兩個方法,並在頁面上使用<s:actionerror/>和<s:actionmessage/>這兩個方法的參數都只有一個String,你可以輸入你要顯示的訊息或者是讀取properties,若是你使用ActionError,就會如同FieldError一樣回傳input,而ActionMessage則是保存一個訊息後,繼續執行流程到結果頁。

2013年4月3日 星期三

Struts2 - Chapter 2 Start Struts2

使用的版本為Struts2-2.3.8,可到Struts2官網下載
 
配置的目錄及lib如下:














 
 
 

Struts2的配置文件struts.xml基本上置於classes下
 
以下為一個簡單的配置:
 
struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

 <constant name="struts.devMode" value="true" />
 
 <package name="default" namespace="/" extends="struts-default">

  <default-action-ref name="index" />

  <global-results>
   <result name="error">/error.jsp</result>
  </global-results>

  <global-exception-mappings>
   <exception-mapping exception="java.lang.Exception"
    result="error" />
  </global-exception-mappings>

  <action name="loginAction" class="user.action.UserLoginAction" method="login">
   <result name="success">/success.jsp</result>
   <result name="input">/index.jsp</result>
  </action>

 </package>

</struts>

從上到下依序介紹,

struts.devMode 讓你debug時可以隨時重新deploy

struts-default  是一個struts2已經設定好的攔截器及流程,通常都是繼承

default-action-ref 預設的action名稱

global-results 所有的result傳回error時,都會進入/error.jsp

global-exception-mappings 同上,所有出現的Exception都會轉到這

action name 頁面呼叫的名稱

action class 該Action的位置

result name 執行完回傳的字串,並跳轉入該頁面

下面用一個簡單的登入範例顯示:

index.jsp

<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
 <s:form name="login" action="loginAction" >
  <s:textfield name="account" label="account" />
  <s:password name="password" label="password" />
  <tr>
   <td>&nbsp;</td>
   <td><s:submit value="login" theme="simple" />
    <s:reset value="reset" theme="simple" /></td>
  </tr>
 </s:form>
</body>
</html>

success.jsp

<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 <s:form >
  <tr>
   <th>account</th>
   <th>password</th>
  </tr>
  <tr>
   <td><s:property value="account" /></td>
   <td><s:property value="password" /></td>
  </tr>
 </s:form>
</body>
</html>

UserLoginAction.java

package user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport{
 private String account;
 private String password;
 
 public String getAccount() {
  return account;
 }

 public void setAccount(String account) {
  this.account = account;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String login(){
  return "success";
 }
}


 

Struts2 - Chapter 1 Introduction


前言

        之前自己在學習Strut2時,發現台灣介紹Struts2的網站跟資料相當少,只好自己去K原文書跟大陸的文章,我想會的人一定相當多,但我相信一定有許多像我這樣非本行的人跨領域進入這個產業,在學習了一段時間後,開始嘗試把學習Strut2的過程編寫成一些教學的文章,希望可以幫助自學而苦無管道的新手。

 

簡介:

        Struts2是一套基於MVC(Model-View-Control)的框架,將模型、視圖、控制三方面分層管理,另外加入了ValueStackOGNL及許多的UI Tags,將從以下的各單元分別介紹起。

 

Struts2流程:

        Struts2 in action中有一張重要的圖如下:
 
 



從最上頭開始,你從頁面送出Request之後,經過許多攔截器,送到Action中執行,接著到Result,再通過攔截器,傳回結果頁面。

再通過攔截器時,會將部分資訊送到ValueStack,而Result的結果也會傳送到ValueStack儲存,最下方的ActionContext是一個Action執行時的容器,會將Action執行時的所有東西,包含(Request,Session,Application,ValueStack…)都存放在裡面,而結果頁面需要呈現時,也是從這裡面讀取,在下一篇將會介紹Struts2的配置文件struts.xml,以及一個簡單的登入範例。

2013年4月2日 星期二

POI-Excel 2007實作

此篇介紹使用POI套件建立excel檔案

使用的版本為poi-3.8-20120326

將樣式建立一個lib可供擴展及取用

我把建立時會用到的部分都詳列註解在上面

希望可以協助在使用上有問題的user

如果有需要提示下載的部分

可參考另一篇檔案下載

code如下:

2013年4月1日 星期一

日期差距、時間差距、兩日期相差多久

最近處理到ASP的日期函數DateDiff,

發現JAVA竟沒有如此好用的API,

只好由自己模擬一個來使用,

我將日期差距和時間差分作兩個function,

請注意日期的部分,

若是2012/12/20和2013/01/01相減的話,

日期加減計算

最近在Coding的過程經常使用到日期運算,

網路上的日期計算也有不少範例,

在此分享一個自己寫的method,

有需要的人就參考看看吧!~