code_prettify

2013年8月30日 星期五

synchronized method無效的假象

系統有個 table紀錄人員編號, entity為
public Class Foo {
  private String prefix1;
  private String prefix2;
  private int serial;
}
有個取號程式會依(prefix1, prefix2)去取得目前流水號,並將 tablek的 serial加1.
為了避免多個 thread搶序號問題, 取號的 method(select and update table)加上了 synchronized.
但卻發現依然出現 serial重複的問題.
測了半天才發現是因為我們用 hibernate.
第二個 thread load entity時 return的是第一個 thread load後的 cache.
加上 session.clear()就解決了.
害我一直很擔心如果真的是 synchronized失效,不知道要加多少班解決這個問題了.

2013年8月29日 星期四

Class org.apache.struts.chain.commands.util.ClassUtils can not access a member of class with modifiers ""

手殘把 public Class XXX的 public刪掉,成了 Class XXX就出現上述錯誤了

2013年8月16日 星期五

eclipse的 Internal validation error java.lang.NullPointerException line 0 JavaScript Problem

看來這個 bug一直無解.
但是退而求其次,不想看到這些 warning,設定起來也是很騙人布
一直忘記該取消哪些
寫下來省個搜尋的工
1.project>properties>Builders-> uncheck Javascript Validator.
2.Window>Preferences>JavaScript>Validator>Errors/Warnings-> uncheck Enable JavaScript semantic validation.

2013年8月13日 星期二

struts 1的 ActionForm, default scope是 session

在 struts-config中, action tag如果沒指定 scope, 預設 scope為 session.
一般都會把 scope設為 request
不過那個爛系統,竟然有一堆都是 default scope
於是就造成了我的悲劇:
public class TheForm extends ActionForm {
private int[] aField;
........
}
jsp
<form name="theForm">
<table>
<tbody id="讓 user新增資料處">
</tbody>
</table>
<input type="button" onclick="在atbody新增一列sample row"/>
</form>
<table id="已經被儲存的新增資料">
<tr><td>
<input name="oldAField" value="1"/>
</tr></td>
<tr><td>
<input name="oldAField" value="2"/>
</tr></td>
<tr><td>
<input name="oldAField" value="3"/>
</tr></td>
</table>
sample row
<tr><td>
<input name="aField" value=""/>
</tr></td>
只准新增,沒有刪除修改.form submit的時候把"讓 user新增資料處"的內容存到 DB.
scenario:
user新增了一筆資料, session還在的時候,同個頁面修改別的地方後儲存.
由於 theForm放在 session裡,上次 submit的新增資料還在theForm裡
但是畫面上沒有新增 aField, 所以 submit出去的 aField是 null, struts不覆蓋 theForm.aFiled.
於是乎,上次的新增資料再次被儲存.

2013年8月8日 星期四

跨 frame資料存取,不考慮 cross-domain

pseudo code
<html>
......
<iframe id="frame1">
<iframe id="frame2">
</html>

iframe1:
<html>
<html>

iframe2
<html>
<input type="text" name="isOk" value="0"
<html>
需求是在 iframe1裡面需要 iframe2的 isOk的值作判斷
直覺的 javascript(in iframe1):
var isOk = parent.document.getElementById('iframe2').document.getElementById('isOk');
.....
但是測了半天才發現 parent.document.getElementById('iframe2')根本沒拿到 iframe2而是拿到 parent.
印出 parent.document.getElementById('iframe2').document.body.innerHTML才發現根本是 parent的內容.

解決方法:
改用 parent.frames['iframe2']

觀念錯誤處:尚未找出.還是不知道為什麼 parent.document.getElementById('iframe2')拿不到 iframe2

收穫:搜尋過程中找到幾個
Web technology for developers
window.frames解說
each item in the window.frames pseudo-array represents the window object corresponding to the given <frame>'s or <iframe>'s content, not the (i)frame DOM element (i.e. window.frames[ 0 ] is the same thing as document.getElementsByTagName( "iframe" )[ 0 ].contentWindow)
iframe scripting
From the DOM iframe element, scripts can get access to the window object of the included HTML page via the contentWindow property. ThecontentDocument property refers to the document element inside the iframe (this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.