顯示具有 Struts 標籤的文章。 顯示所有文章
顯示具有 Struts 標籤的文章。 顯示所有文章

2008年12月2日 星期二

struts標籤使用舉例--logic篇

1. logic:empty

該標籤是用來判斷是否為空的。如果為空,該標籤體中嵌入的內容就會被處理。該標籤用於以下情況:

1)當Java對象為null時;

2)當String對象為""時;

3)當java.util.Collection對象中的isEmpty()返回true時;

4)當java.util.Map對象中的isEmpty()返回true時。
eg.

...

該句等同於:
if (userList.isEmpty()) {
...
}
2. logic:notEmpty
該標籤的應用正好和logic:empty標籤相反,略。
3. logic:equal
該標籤為等於比較符。
eg1. 比較用戶的狀態屬性是否1,若為1,輸出"啟用";

啟用

eg2. 如果上例中的value值是動態獲得的,例如需要通過bean:write輸出,因struts不支持標籤嵌套,可採用EL來解決該問題。

......

4. logic:notEqual
該標籤意義與logic:equal相反,使用方法類似,略。
5. logic:forward
該標籤用於實現頁面導向,查找配置文件的全局forward。
eg.
6. logic:greaterEqual
為大於等於比較符。
eg. 當某學生的成績大於等於90時,輸出「優秀」:

優秀

7. logic:greaterThan
此為大於比較符,使用方法同logic:greaterEqual,略;
8. logic:lessEqual
此為小於等於比較符,使用方法同logic:greaterEqual,略;
9. logic:lessThan
此為小於比較符,使用方法同logic:greaterEqual,略;
10. logic:match
此標籤比較對象是否相等;
eg1. 檢查在request範圍內的name屬性是否包含"amigo"串:

中有一個「amigo」串。

eg2. 檢查在request範圍內的name屬性是否已「amigo」作為起始字符串:

以「amigo」作為起始字符串。

eg3.

你運行的是Windows系統

11. logic:notMatch

此標籤用於比較對象是否不相同,與logic:match意義相反,使用方法類似,略。
12. logic:messagePresent
該標籤用於判斷ActionMessages/ActionErrors對象是否存在;
eg. 如果存在error信息,將其全部輸出:





13. logic:messagesNotPresent
該標籤用於判斷ActionMessages/ActionErrors對象是否不存在,使用方法與logic:messagePresent類似,略
14. logic:present
此標籤用於判斷request對象傳遞參數是否存在。
eg1. user對象和它的name屬性在request中都存在時,輸出相應字符串:

user對象和該對象的name屬性都存在

eg2. 若有一個名字為「user」的JavaBean,輸出對應字符串:

有一個名字為「user」的JavaBean。

eg3.

we got a user-agent header.

15. logic:notPresent
此標籤用於判斷request對象傳遞參數是否不存在,意義與了logic:present相反,使用方法類似,略。
16. logic:redirect
該標籤用於實現頁面轉向,可傳遞參數。
eg1.

17. logic:iterator
用於顯示列表為collection的值(List ,ArrayList,HashMap等)。
eg1. 逐一輸出用戶列表(userlList)中用戶的姓名:




eg2. 從用戶列表中輸出從1開始的兩個用戶的姓名

.


eg3. logic:iterator標籤的嵌套舉例

.





2008年7月15日 星期二

Try Statement Format

1. set the conn = DatabaseManager.getConnection(); in the begginning and once it one time only.
2. set the conn.rollback(); when the error happen and set the SQL will not keep running.
3. call function by following command and dun open the connection again.
ContractorDAO conDAO = new ContractorDAO();
conDAO.setConnection(conn);


try {

conn = DatabaseManager.getConnection();

log.info("TRY : JobSvc.APP_ID...................." + JobSvc.APP_ID);
log.info("TRY : JobSvc.RCW_VIEW...................." + JobSvc.RCW_VIEW);
log.info("TRY : userId................." + userId);
log.info("TRY : theForm.getCurrentUserIdInt()................." + theForm.getCurrentUserIdInt());
log.info("TRY : Check siteCode !!! ......................." + siteCode );
log.info("check isEmpty() --------------------------> : " + errors.isEmpty());

// check conn is null
if (conn == null || conn.isClosed()) {
conn = null;
log.error("conn == null || conn.isClosed()");
errors.add("SystemError", new ActionMessage("hrms.error.systemError"));
forwardmapping = "result";
return mapping.findForward(forwardmapping);
}

// check conn ok and then check access right
//if (!ValidateAccess.canAccessBySiteCodeServiceIdUserId(conn, siteCode, JobSvc.RCW_CONTRACTOR_REPORT, userId)){

/******* Access right ***********************
if (!ValidateAccess.canAccessBySiteCodeServiceIdUserId(conn, siteCode, JobSvc.RCW_VIEW, userId)){
errors.add("accessrights",new ActionMessage("error.access.norights"));
saveErrors(request,errors);
if (conn == null && !conn.isClosed()) {
conn.close();
conn = null;
}
return mapping.findForward("Error");
}
******* Access right ***********************/

}

// set the contractor list
ContractorDAO conDAO = new ContractorDAO();
conDAO.setConnection(conn);

catch (ExceedLimitException e){
try {if (conn!=null && !conn.isClosed()) {
conn.rollback();}
} catch (SQLException ec){}
log.error("RcwOutputAction Exception" + e);
errors.add("loadExceed" ,new ActionMessage("error.rcw.result.exceed1000"));
} catch (Exception ex) {
try {if (conn!=null && !conn.isClosed()) {
conn.rollback();}
} catch (SQLException ec){}
log.error("RcwOutputAction Exception" + ex);
ex.printStackTrace();
errors.add("load" ,new ActionMessage("error.loadError"));
} finally{
request.setAttribute("totalRecord",totalRecord); // send the totalRecord to the Result JSP page
try {
if (conn != null) {
conn.close();
conn = null;
}
}
catch (Exception e) {}
}

if (errors.isEmpty()) {
action = RcwOutputFileForm.ACTION_LIST;
} else {
action = RcwOutputFileForm.ACTION_SEARCH;
}

}//end if : action_list
log.info("check isEmpty() --------------------------> : " + errors.isEmpty());

if (action != null && action.equals(RcwOutputFileForm.ACTION_SEARCH)){
log.info("ACTION_SEARCH : " + action);
forwardmapping = RcwOutputFileForm.ACTION_SEARCH;
}

if (!errors.isEmpty()){
saveErrors(request,errors);
log.error("error is not empty!");
}

log.info("forwardmapping: " + forwardmapping);
return mapping.findForward(forwardmapping);

Declare New Form In Struts

struts-config-jobRequest.xml - rcwOutputFile.jsp

< name="RcwOutputFileForm" type="sem.jobRequest.form.RcwOutputFileForm">
< /form-bean >


< action name="RcwOutputFileForm" path="/rcwOutputFile" scope="request"

type="sem.jobRequest.action.RcwOutputFileAction" unknown="false" validate="false">
< name="search" path="/rcwOutputFile.jsp" redirect="false">
< name="edit" path="/rcwOutputFileEdit.jsp" redirect="false">
< name="list" path="/rcwOutputFileResult.jsp" redirect="false">
< /action >