form tag - form

form : It is used to create a form to accept data from user. All the input tag must into this tag. It is frequently used in the web application. It has two important attributes - action (target action name, which will be configured in struts.xml) and method (type of HTTP method, generally get or post is used). If you do not specify method, get will be the form default method type.

<s:form action="ActionName" method="post">

form tag - textfield

textfield : It is used to create HTML form input control. It has three important attributes - name (name of the control), label (display label before input control) and size (size of control).

<s:form action="ActionName" method="post">
<s:textfield name="name" label="Enter Name" size="30" />

Lets demostracte form and textfiled tag. In this example, I am creating a form, a textfiled and a button. While submitting form to an action, it will be redirected to Success.jsp page.

Follow the steps to create this project :

Step 1: Create a Dynamic Web Project in eclipse - "Struts2FormTextfield".

Step 2: The follwing jar files required in WEB-INF\lib folder in your project. Please read Readme.txt file in the attcahed source code for more details.

Step 3: Create a View page index.jsp.
<%@taglib uri="/struts-tags" prefix="s"%>
<title>Form Tags</title>
<h1>Struts2 textfield example</h1>
<s:form action="TextFieldAction" method="post">
<s:textfield name="name" label="Enter Name" size="30" />
<s:submit value="Submit" align="center" />

Step 4: Create action class
package techmyguru.formtags;
import com.opensymphony.xwork2.ActionSupport;
public class TextFieldAction extends ActionSupport {
private String name;
// getter and setter method
public String execute() throws Exception {
return SUCCESS;

Step 5: Configure Action in struts.xml file.
struts.xml :
<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<constant name="devMode" value="true" />
<package name="default" extends="struts-default">
<action name="TextFieldAction" class="techmyguru.formtags.TextFieldAction">

Step 6: Create target View page Success.jsp.
<%@ taglib uri="/struts-tags" prefix="s"%>
<title>Textfield example</title>
<h1>Struts2 textfield example<br>
Hi, <s:property value="name" /> </h1>
<a href="index.jsp">Try Again</a>

Step 7: Verify the directory structure.
Struts2 textfiled form tag

Step 8: Run the project and get output.
Struts2 textfiled form tag

Enter name , click Submit button and get output.
Struts2 textfiled tag
Previous Download Source Code Next - password tag

Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :