2017-01-04 5 views
0

私は少し問題があります。私はいくつかのフィールドを持つオブジェクトを持っている場合は、フォームを介してこれらのフィールドを渡すことは簡単です:thymeleaf形式の文字列のみを渡す方法はありますか?

コントローラー:

@RequestMapping("/") 
public String hello(Model model) { 
    model.addAttribute("test", Test); 
    return "index"; 
} 

HTML:

<form th:action="@{/process}" method="post" th:object="${test}"> 
<input type="text" th:field="*{value}"/> 
<input type="submit" /> 
</form> 

しかし、私はしたくない場合はどのようなオブジェクトと渡すだけの文字列ですか?そのような何か:

コントローラー:

@RequestMapping("/") 
public String hello(Model model) { 
    model.addAttribute("test", "test string"); 
    return "index"; 
} 

は、HTML:

<form th:action="@{/process}" method="post"> 
<input type="text" th:field="${test}"/> 
<input type="submit" /> 
</form> 

は動作しません。 助けてくれてありがとう!

のindex.html:コメント欄に次の質問には

<form th:action="@{/process}" method="post"> 
<textarea th:text="${sourceText}"/> 
<input type="submit" /> 

ggg.html:

<textarea th:text="${sourceText}"/> 

コントローラ:

@RequestMapping("/") 
public String hello(Model model) { 
    model.addAttribute("sourceText", "asdas"); 
    return "index"; 
} 

@RequestMapping("/process") 
public String process(Model model, @ModelAttribute(value = "sourceText") String sourceText) { 
    return "ggg"; 
} 

答えて

0

Spring MVCが動作する方法によると、フォーム内のオブジェクトとして文字列を使用することはできません。フォーム構造がObjectで、それにリンクされているフィールドなので、その文字列をカプセル化するオブジェクトが必要です。

あなたのケースでは、私はこれらの一般的な状況のためのビューフォームオブジェクトを作成します。これは、formViewのようなString属性テキストを作成するものです。同様の状況で同じオブジェクトを使用することもできます。

この余分なオブジェクトを作成したくない場合は、AJAXでデータを送信し、データ配列を作成してjavascriptでコントローラに送信できます。

個人的に私は最初のオプションに行くだろう、より再利用可能です。

ご希望の場合はこちら

関連する問題