1

spring-mvcを使用してthymeleafテンプレートをth:pattern次のように日付入力フォーム フィールドに使用しようとしていますが、運がありません。他の誰かが同様のことを経験し、洞察や代替手段を持っていますか?

1を試しました。パターンのハードコーディング

<input type="text" th:pattern="MM/dd/yyyy" th:field="*{classDate}"/>

受信エラー:

Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "MM/dd/yyyy" 

そして2。使用するテンプレートの Java コードでパターンを設定する

<input type="date" th:pattern="${classdate_format}" th:field="*{classDate}"/>

受信エラー:

Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring3.processor.attr.SpringInputGeneralFieldAttrProcessor'
4

1 に答える 1

2

pattern is html5 attribute of input tag.

pattern validates an input value using regex. So that value, which you inserts into pattern attribute should be correct regex pattern.

If you are using Thymeleaf's th: prefix, template processor trying to find appropriate variable in Spiring's model and insert it as a value of attribute. Thymeleaf is using Spring EL for it's templates.

So your first approach is incorrect because of using invalid SpringEL expression.

The second solution looks better, type="date" gives you exactly what you want, but works not for all browsers. ${classdate_format} looks like correct expression. To understand what causes the second error more code needed.

Anyway is there any reason to use th: prefix for pattern attribute? It needed only if you want to create regex pattern dynamically at server side. But in this case regex pattern is pretty straightforward, so you can use attribute without th:. To write correct regex for your case please refer to this answer.

于 2013-06-15T21:30:35.863 に答える