0
0

More than 3 years have passed since last update.

Java Spring Controllerで使用したもの

Last updated at Posted at 2021-05-19

Java Spring Controllerで使用したものを書き連ねていきます。

こだわりのディスクリプションを書く

(例)[({要素1})、({要素2})、({要素3})]{標準discription}
要素を最大3つまで表示させる場合

1.標準discriptionはymlファイルに記載

2.要素を生成(最大3つまで)

java
StringBuilder sbHoge = new StringBuilder();
int count = 0;
for(HogeEntity hogeEntity : hogehoge.getHoges()) {
    if(count <= 2) {
        sbHoge.append("、");
        sbHoge.append(hogeEntity.getName());

    }
    count ++;
}
if(sbHoge.length()!=0) {
    sbHoge.deleteCharAt(0);
    sbHoge.insert(0, "[");
    sbHoge.append("]");
}
//Stringに変換
String pageHoges = sbHoge.toString();

3.連結

java
StringBuilder sbDscription = new StringBuilder();
//Stringに変換した連結された要素(最大3つ)
sbDscription.append(pageHoges);
//ymlファイルから取り出した標準discription
sbDscription.append(messageSource.getMessage("yml.dscription.", null, localeResolver.resolveLocale(request)));
//Stringに変換
String siteDscription = sbDscription.toString();

int → String

java
int val = 1;
String valStr = String.valueOf(val);

int val = 1;
String valStr = Integer.toString(val);

String → int

java
String valStr = "1";
int val = Integer.parseInt(valStr);

String valStr = "1";
int val = Integer.valueOf(valStr);

StringBuilder
https://java-reference.com/java_string_stringbuilder.html

java
StringBuilder sb = new StringBuilder("A");

sb.append("B");//末尾に加える
sb.insert(123, "***");//インデックス番号1(2文字目)に挿入
sb.delete(0,2);//インデックス番号0~2(1~2文字目)を削除
sb.deleteCharAt(0);//インデックス番号0(1文字目)を削除
sb.deleteCharAt(sb.length()-1);//末尾から1文字だけ削除
String siteUrl = sb.toString();

クエリパラメータ取得する方法

java
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String search(
@RequestParam Map<String,String> allRequestParams, ModelMap model) {
   return "paramName";
}

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0