Struts 2 <s:textfield> textbox example
In Struts 2 , you can use the <s:textfield> to create a HTML input textbox. For example, you can declared the “s:textfield” with a key attribute or label and name attribute.
<s:textfield key="username" /> //or <s:textfield label="Username" name="username" />
Both are generate the same HTML output (in default xhtml theme).
<td class="tdLabel"> <label for="registerUser_username" class="label">Username:</label> </td> <td> <input type="text" name="username" value="" id="registerUser_username"/> </td>
Struts 2 <s:textfield> example
Quick guide to create a textbox input field in Struts 2.
1. Properties file
Two properties files to store the message.
global.properties
#Global messages
username = Username
submit = SubmitRegisterAction.properties
#error message
username.required = Username is required2. Action
A simple Action class with a validation to make sure the username is not empty, otherwise return an error message.
RegisterAction.java
package com.mkyong.user.action; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport{ private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } //business logic public String execute() { return "SUCCESS"; } //simple validation public void validate(){ if("".equals(getUsername())){ addFieldError("username", getText("username.required")); } } }
3. View page
Result page to use Struts 2 “s:textfield” to create a HTML textbox input field.
register.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 - textbox example</h1> <s:form action="registerUser" namespace="/user"> <s:textfield key="username" /> <s:submit key="submit" name="submit" /> </s:form> </body> </html>
welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 - textbox example</h1> <h4><s:property value="username"/></h4> or <h4><s:property value="%{username}"/></h4> </body> </html>
4. struts.xml
Link all together ~
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <action name="register"> <result>pages/register.jsp</result> </action> <action name="registerUser" class="com.mkyong.user.action.RegisterAction"> <result name="SUCCESS">pages/welcome.jsp</result> <result name="input">pages/register.jsp</result> </action> </package> </struts>
5. Demo
http://localhost:8080/Struts2Example/user/register.action









Your article is very good butcher. I have to know more.
Thank you to me.
How to display values fetched from action class in a text box?
My scenario is: When a user wants to modify his details, he clicks on the modify button which will redirect him to a jsp which should populate his details from DB into textboxes so that he can modify the required details.
I just want the part which will auto populate the data… how can this be done?
i think you should to write getter setter method of parameter in action class after that you can write update query in action class for getting the values which is enterde into text field you can get it by calling it like name=’”+getname()”‘
if you want more information then call me iwill send you all code related to that no-9922095969
thanks