Struts - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Struts 2 Form Tags

Struts 2 Form Tags

shape Description

Struts 2 Form Tags, While developing on the projects, developer needs to to write the code, then some tags will help to reduce the code where each and every tag will have its own functionality but all the tags functionality is same but their usage may be different in technologies. Struts 2 have some set of tags. Using these tags, user can easily execute the flow of page execution. Struts 2 has the great feature that can integrate with other frameworks such as hibernate, tiles, spring, tiles, Ajax. Following are the different tags available in Struts 2 will help to reduce the programming length.

Form Tags

shape Description

Struts 2 Form Tags are defined in different places. Struts 2 Form Tags are the subset of Struts UI tags. In Struts 2 UI tags are defined in different ways.Following are the different types UI tags.

Simple UI tags

Simple UI tags are used to display the text in the browser or user have to create a text fields like text area, label, forms, submit.Following is the sample example code. [java]<s:div>Login Form</s:div> <s:text name=”Enter all required fields:” /> <s:form action=”login” enctype=”multipart/formdata”> <s:textfield key=”login.form” name=”username”/> <s:password key=”login.password” name=”password”/> <s:label for=”attached resume” value=”Upload resume”/> <s:file name=”attached resume” accept =”text/html, text/plain”/> <s:token/> <s:submit key=”submit”/> </s:form>[/java]

Group UI tags

The group UI tags are used to create radio button and the checkbox. For example, let’s design one login page and that login page contain different fields. [java]<%@ page contenttype=”text/html”; charset=UTF-8″%> <%@ taglib prefix=”s” uri=”/struts-tags”%> <s:html> <head> Itoolsinfo Login page</head> <body> <s:form action=”login” method=”post”> <s:radio label=”Gender” name=”gender” list=”{‘male’,’female’}”/> <s:checkboxlist label=”Java Types” name=”Java types” list=3. Selected UI tags.”] [/java] In Struts 2, select tags are used to select the multiple options as follows. [java]<%@ page contenttype=”text/html”; charset=UTF-8″%> <%@ taglib prefix=”s” uri=”/struts-tags”%> <s:html> <head> Itoolsinfo Login page</head> <body> <s:form action=”login” method=”post”> <s:select name=”username” label=”Username” list=”{‘John’, ‘Joshaf’, ‘Smith’}”/> <s:select name=”languages” label=”Languages” list=”{‘Java’, ‘.Net’, ‘Oracle’,’Testing’}”/> <s:doubleselect label=”Occupation” name=”occupation” list=”{‘Techinical’,’Other’}” doubleName=”occupations2″ doubleList=”top==’Techincal’?{‘I.T’, ‘Hardware’}:{‘Accounting’,’H.R’}”/> </s:form> </s:body> </s:html> [/java] In this example, select the username, language and occupation. First select single user from multiple users and select the tags like Jhon, Joshaf, Smith. Now, select any one language in multiple languages like Java, .Net, Oracle. Then, select the user category like Technical or other. If the user is from Technical, then select the user position like I.T or Hardware, else select the Non-technical and the user position like Accounting or H.R.

Selected UI tags

While developing the web applications developer supposed to insert select tag to select the single element from the multiple elements and this type of elements regularly used in html. Following is an example which describes more selected tag. [java] <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Hello World</title> <s:head /> </head> <body> <s:form action="login.action"> <s:select name="username" label="Username" list="{'Mike','John','Smith'}" /> <s:select label="Company Office" name="mySelection" value="%{'America'}" list="%{#{'America':'America'}}"> <s:optgroup label="Asia" list="%{#{'India':'India','China':'China'}}" /> <s:optgroup label="Europe" list="%{#{'UK':'UK','Sweden':'Sweden','Italy':'Italy'}}" /> </s:select> <s:combobox label="My Sign" name="mySign" list="#{'aries':'aries','capricorn':'capricorn'}" headerKey="-1" headerValue="--- Please Select ---" emptyOption="true" value="capricorn" /> <s:doubleselect label="Occupation" name="occupation" list="{'Technical','Other'}" doubleName="occupations2" doubleList="top == 'Technical' ? {'I.T', 'Hardware'} : {'Accounting', 'H.R'}" /> </s:form> </body> </html> [/java] Following is the Best form example to understand the concept.

Summary

shape Key Points

  • Struts 2 Form Tags - Form tags general using tags in every technology.
  • Struts 2 Form Tags - Declaration of tags will be varied in different technologies.