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">
........................
.......................
</s:form>


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" />
</s:form>


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.
index.jsp:
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Form Tags</title>
</head>
<body>
<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" />
</s:form>
</body>
</html>

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

Step 5: Configure Action in struts.xml file.
struts.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="devMode" value="true" />
<package name="default" extends="struts-default">
<action name="TextFieldAction" class="techmyguru.formtags.TextFieldAction">
<result>/Success.jsp</result>
</action>
</package>
</struts>


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


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


Write your Comment

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