Struts 2 <s:textfield> textbox example

Struts 2 <s:textfield> textbox example

Previous Home Next

 

The Struts 2.0 framework provide text input element through the<s:textfield></s:textfield> tag. 
This tag provide the facility to get input through the user .

The Struts 2 textfield component provide the following textfield tag:

<s:textfield label="UserName" name="username" />

 
Directory Structure of <s:textfield> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<s:form action="text">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield name="address" label="Address"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="text" class="org.r4r.textBoxAction">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>

textBoxAction.java

package org.r4r;

public class textBoxAction {
	private String name;
	private String address;
	
	public String execute(){
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	

}

hello.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<h3>
Name:-<s:property value="name"/>
<br/>Address:-<s:property value="address"/>
</h3>

Output






Previous Home Next