org.springframework.beans.propertyeditors.PropertiesEditorの使い方

1.IntegerEditorクラス
import org.springframework.beans.propertyeditors.PropertiesEditor;
public class IntegerEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals(“”)) {
text = “0”;
}
setValue(Integer.parseInt(text));
}

@Override
public String getAsText() {
return getValue().toString();
}
}

2.FloatEditorクラス
import org.springframework.beans.propertyeditors.PropertiesEditor;

public class FloatEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals(“”)) {
text = “0”;
}
setValue(Float.parseFloat(text));
}

@Override
public String getAsText() {
return getValue().toString();
}
}

3.LongEditorクラス
import org.springframework.beans.propertyeditors.PropertiesEditor;
public class LongEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.equals(“”)) {
text = “0”;
}
setValue(Long.parseLong(text));
}

@Override
public String getAsText() {
return getValue().toString();
}
}

「Spring MVC」@PathVariableアノテーションを使用するサンプル

1.Javaコード
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PathVariableExampleController {

/**
* @PathVariable Example:
*
*/
@RequestMapping(“/cft/lijia/{sitePrefix}/{language}/document/{id}/{naturalText}”)
public String documentView(Model model,
@PathVariable(value = “sitePrefix”) String sitePrefix,
@PathVariable(value = “language”) String language,
@PathVariable(value = “id”) Long id,
@PathVariable(value = “naturalText”) String naturalText) {

model.addAttribute(“sitePrefix”, sitePrefix);
model.addAttribute(“language”, language);
model.addAttribute(“id”, id);
model.addAttribute(“naturalText”, naturalText);

String documentName = “Java tutorial for Studyers”;
if(id == 8108) {
documentName = “Spring MVC for Studyers”;
}

model.addAttribute(“documentName”, documentName);

return “documentView”;
}
}

2.JSPコード
/WEB-INF/pages/documentView.jsp
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>${documentName}</title>
</head>
<body>
<h3>${documentName}</h3>
Site Prefix: ${sitePrefix}
<br> Language: ${language}
<br> ID: ${id}
<br> Natural Text: ${naturalText}
<br>

</body>
</html>

URLの確認
http://localhost:8080/HelloSpringMVC/cft/lijia/default/en/document/8108/spring-mvc-for-beginners

「Spring MVC」InitBinderでDate型パラメータを設定するコード

Javaコード
//the parameter was converted in initBinder
@RequestMapping(“/date”)
public String date(Date date){
System.out.println(date);
return “hello”;
}

//At the time of initialization,convert the type “String” to type “date”
@InitBinder
public void initBinder(ServletRequestDataBinder binder){
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(“yyyy-MM-dd”),
true));
}

「PoStgreSQL入門」substr()で文字列の一部を取得する

SQL構文
select substr(‘1234’,0,3) as cft

postgreSQLの場合、結果:12
Oracleの場合、結果:12
MySQLの場合、結果:空白

「Spring MVC」@RequestParamアノテーションを使用するサンプル

1.Javaコード
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RequestParamExampleController {

@RequestMapping(“/user”)
public String userInfo(Model model,
@RequestParam(value = “name”, defaultValue = “Guest”) String name) { model.addAttribute(“name”, name);
if(“demo”.equals(name)) {
model.addAttribute(“email”, “demo@sample.com”);
} else{
model.addAttribute(“email”, “Not set”);
}
return “userInfo”;
}

}
2.JSPコード
/WEB-INF/pages/userInfo.jsp
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<title>User Info</title>
</head>
<body>
<h2>${name}</h2>
Email: ${email}
<br>
</body>
</html>
URLの確認
http://localhost:8080/HelloSpringMVC/user?name=demo

「Spring MVC」redirectでリダイレクト先ページに遷移する

Javaコード:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RedirectExampleController {

@RequestMapping(value = “/redirect”, method = RequestMethod.GET)
public String authorInfo(Model model) {

// Write some code

return “redirect:/hello”;
}
}
URLの確認
http://localhost:8080/HelloSpringMVC/redirect

「Javacript」ファイルを読み込む方法

//ファイルを読み込む
function readFile(filename){
var fso = new ActiveXObject(“Scripting.FileSystemObject”);
var f = fso.OpenTextFile(filename,1);
var s = “”;
while (!f.AtEndOfStream)
s += f.ReadLine()+”/n”;
f.Close();
return s;
}

//ファイルを書き込み
function writeFile(filename,filecontent){
var fso, f, s ;
fso = new ActiveXObject(“Scripting.FileSystemObject”);
f = fso.OpenTextFile(filename,8,true);
f.WriteLine(filecontent);
f.Close();
alert(‘ok’);
}

xxx is not in the sudoers file.This incident will be reportedの解決方法

エラーメッセージ:
yamada_user is not in the sudoers file. This incident will be reported.

解決方法
#chmod u+w /etc/sudoers
#vi /etc/sudoers

## Allow root to run any commands anywhere
root ALL=(ALL) ALL
追記
yamada_user ALL=(ALL) ALL

「javaScript」正規表現で前後の空白を取り除く方法

1.全ての空白を取り除く
str = str.replace(/\s+/g,””);

2.両端の空白を取り除く
str = str.replace(/^\s+|\s+$/g,””);

3.左側の空白を取り除く
str=str.replace( /^\s*/, ”);

4.右側の空白を取り除く
str=str.replace(/(\s*$)/g, “”);

「Java入門」Listから配列へ変換するサンプル

Javaコード:
ArrayList<Integer> cft = new ArrayList<Integer>();
cft.add(1);
cft.add(2);
cft.add(3);

int size = cft.size();
Integer[] pp = cft.toArray(new Integer[size]);

String str = “”;
for(int i = 0; i < size; i++) {
str += pp[i] + “, “;
}
System.out.println(str);