{"id":4114,"date":"2020-07-28T17:22:50","date_gmt":"2020-07-28T11:52:50","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4114"},"modified":"2020-12-16T15:23:36","modified_gmt":"2020-12-16T09:53:36","slug":"what-are-spring-tags","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-are-spring-tags\/","title":{"rendered":"What are Spring Tags?"},"content":{"rendered":"\n<p>Spring MVC Form Tags are the reusable building blocks of a web page. These Spring tags are used for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_binding\" rel=\"nofollow noopener\" target=\"_blank\">data binding<\/a> that automatically sets data to Java object\/bean and retrieves from it. It makes an application easy to develop, read, and maintain.<\/p>\n\n\n\n<p>There are various tags available in Spring MVC:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The form tag<\/li><li>The input tag<\/li><li>The checkbox tag<\/li><li>The checkboxes tag<\/li><li>The radiobutton tag<\/li><li>The radiobuttons tag<\/li><li>The password tag<\/li><li>The select tag<\/li><li>The option tag<\/li><li>The options tag<\/li><li>The textarea tag<\/li><li>The hidden tag<\/li><li>The errors tag<\/li><\/ul>\n\n\n\n<p>These tags come under spring-webmvc.jar. You need to add the following directive at the beginning of the JSP page:<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;%@ taglib prefix=\"form\" uri=\"http:\/\/www.springframework.org\/tags\/form\" %>  <\/code><\/pre><\/amp-fit-text>\n\n\n\n<p><strong>The form tag: <\/strong>It is a parent tag that contains all the other tags.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:form action=\"nextFormPath\" modelAttribute=?abc?>  <\/code><\/pre><\/amp-fit-text>\n\n\n\n<p>This will generate an HTML form tag and exposes a binding path to inner tags.<\/p>\n\n\n\n<p><strong>The input tag: <\/strong>It generates an HTML input tag.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:input path=\"name\" \/>  <\/code><\/pre><\/amp-fit-text>\n\n\n\n<p>It also provides other input types such as email, date, tel, etc.<\/p>\n\n\n\n<p>For email:<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:input type=?email? path=\"email\" \/> <\/code><\/pre><\/amp-fit-text>\n\n\n\n<p>For date:<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:input type=?date? path=\"date\" \/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<p><strong>The password tag: <\/strong>This tag generates an HTML tag with type=&#8217;password&#8217;.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:password path=\"password\" \/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<p><strong>The textarea tag: <\/strong>It generates an HTML textarea.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:textarea path=\"notes\" rows=\"3\" cols=\"20\"\/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<p>Number of rows and columns can be specified as required.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:checkbox path=\"receiveNewsletter\" \/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<p>The above is applicable for a single checkbox whose return type is Boolean. After setting the value as true, the checkbox will be checked by default.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">For Multiple Checkboxes:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Bird watching:<\/strong> &lt;form:checkbox path=\"hobbies\" value=\"Bird watching\"\/&gt;\n<strong>Astronomy:<\/strong> &lt;form:checkbox path=\"hobbies\" value=\"Astronomy\"\/&gt;\n<strong>Snowboarding:<\/strong> &lt;form:checkbox path=\"hobbies\" value=\"Snowboarding\"\/&gt;<\/pre>\n\n\n\n<p><strong>The radiobutton and radiobuttons tag: <\/strong>This will generate the HTML input tag with type=&#8217;radio&#8217;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">For Single Radiobutton:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Male:<\/strong> &lt;form:radiobutton path=\"sex\" value=\"M\"\/&gt;\n<strong>Female:<\/strong> &lt;form:radiobutton path=\"sex\" value=\"F\"\/&gt;\n<strong>For Multiple Radiobutton:<\/strong>\n&lt;form:radiobuttons path=\"sex\" items=\"${sexOptions}\" \/&gt;\n<\/pre>\n\n\n\n<p><strong>The select tag: <\/strong>This will generate the HTML select tag.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:select path=\"country\" items=\"${countryItems}\" \/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<p><strong>The hidden tag: <\/strong>This will generate the HTML input tag with type=&#8217;hidden&#8217;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;form:hidden path=\"id\" value=\"12345\" \/&gt;<\/pre>\n\n\n\n<p><strong>The option and options tag: <\/strong>This is used to select option from a drop-down option containing a list of elements.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>To generate single option tag:\n<\/strong>&lt;form:option value=\"abc\" label=\"xyz\"\/&gt;&nbsp;&nbsp;\n<strong>To generate multiple option tag:\n<\/strong>&lt;form:options items=\"${elementList}\" itemValue=\"abc\" itemLabel=\"xyz\"\/&gt;&nbsp;\n<\/pre>\n\n\n\n<p><strong>The errors tag: <\/strong>This is used to represent the HTML errors in the JSP page created by Validators.<\/p>\n\n\n\n<amp-fit-text layout=\"fixed-height\" min-font-size=\"6\" max-font-size=\"72\" height=\"80\"><pre class=\"wp-block-code\"><code>&lt;form:errors path=\"name\" cssClass=\"error\" \/><\/code><\/pre><\/amp-fit-text>\n\n\n\n<h4 class=\"wp-block-heading\">Example of Validator in error tag:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">public class UserValidator implements Validator {\n&nbsp;&nbsp;&nbsp;&nbsp;public boolean supports(Class candidate) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return User.class.isAssignableFrom(candidate);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;public void validate(Object obj, Errors errors) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ValidationUtils.rejectIfEmptyOrWhitespace(errors, \"firstName\", \"required\", \"Field is required.\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ValidationUtils.rejectIfEmptyOrWhitespace(errors, \"lastName\", \"required\", \"Field is required.\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong>Form.jsp <\/strong>will look like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;form:form&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;table&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;First Name:&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;\n&nbsp;&nb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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/table&gt;\n&lt;\/form:form&gt;\n<\/pre>\n\n\n\n<p>The HTML file will look like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;form method=\"POST\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;table&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;First Name:&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;input name=\"firstName\" type=\"text\" value=\"\" \/&gt;&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%-- Associated errors to firstName field displayed --%&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;span name=\"firstName.errors\"&gt;Field is required.&lt;\/span&gt;&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;Last Name:&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;input name=\"lastName\" type=\"text\" value=\"\" \/&gt;&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;%-- Associated errors to lastName field displayed --%&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;span name=\"lastName.errors\"&gt;Field is required.&lt;\/span&gt;&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td colspan=\"3\"&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=\"submit\" value=\"Save Changes\" \/&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/td&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/tr&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/table&gt;\n&lt;\/form&gt;\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring MVC Form Tags are the reusable building blocks of a web page. These Spring tags are used for data binding that automatically sets data to Java object\/bean and retrieves from it. It makes an application easy to develop, read, and maintain. There are various tags available in Spring MVC: The form tag The input [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4138,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[1147,1139,1146],"class_list":["post-4114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-boolean","tag-spring-mvc","tag-spring-tags"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4114"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4138"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}