Struts <html:text> textbox example
In this Struts example, you will learn how to create a HTML text box input field with Struts <html:text> tag, validate the text box with ActionForm, display the error message with ActionErrors, and also message resource in JSP page.
1. Folder Structure
This is the final project structure created by Maven. Please create the corresponding folders.

2. Action class
Create an Action class, do nothing but forward the request.
UserAction.java
package com.mkyong.common.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class UserAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { return mapping.findForward("success"); } }
3. Properties file
Create a properties file, and declare the error and label messages.
Common.properties
#common module error message error.common.name.required = Name is required. #common module label message label.common.name = UserName label.common.button.submit = Submit label.common.button.reset = Reset
4. ActionForm
Create a ActionForm, accept a username and validate it in validate() method. If the username is empty, get a error message from the above properties file, and add it as key “common.name.err” in ActionErrors, later will use this name “common.name.err” to display the error message in JSP.
UserForm.java
package com.mkyong.common.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class UserForm extends ActionForm{ String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if( getUsername() == null || ("".equals(getUsername()))) { errors.add("common.name.err", new ActionMessage("error.common.name.required")); } return errors; } @Override public void reset(ActionMapping mapping, HttpServletRequest request) { // reset properties username = ""; } }
5. JSP Page
Use the Struts’s HTML tag <html:text> to create a HTML text box input field. The label message is declared in above properties file, you can get it via <bean:message key=”label_name” /> directly.
The error message can display in two ways :
- Display all error messages
<html:errors/>
- Display specified error message by specify the error message key which declared in ActionErrors.
<html:messages id="err_name" property="common.name.err"> <bean:write name="err_name" /> </html:messages>
login.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
</head>
<body>
<h1>Struts html:text example</h1>
<html:form action="/Login">
<html:messages id="err_name" property="common.name.err">
<div style="color:red">
<bean:write name="err_name" />
</div>
</html:messages>
<div style="padding:16px">
<div style="float:left;padding-right:8px;">
<bean:message key="label.common.name" /> :
</div>
<html:text property="username" size="20" maxlength="20"/>
</div>
<div style="padding:16px">
<div style="float:left;padding-right:8px;">
<html:submit>
<bean:message key="label.common.button.submit" />
</html:submit>
</div>
<html:reset>
<bean:message key="label.common.button.reset" />
</html:reset>
</div>
</html:form>
</body>
</html>Display the text box input.
welcome.jsp
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> </head> <body> <h1> Welcome <bean:write name="userForm" property="username" /> </h1> </body> </html>
6. struts-config.xml
Create a Struts configuration file and link all together.
In action tag attributes :
- path = web path you access
- type = your action class
- name = your action form
- validate = true will cause ActionForm to validate() method for the form validation
- input = if the validation false, where to forward?
You have to include your properties file in struts-config.xml file to make Struts aware of your custom resource message.
<message-resources parameter="com.mkyong.common.properties.Common" />
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="userForm" type="com.mkyong.common.form.UserForm"/> </form-beans> <action-mappings> <action path="/LoginPage" type="org.apache.struts.actions.ForwardAction" parameter="/pages/login.jsp"/> <action path="/Login" type="com.mkyong.common.action.UserAction" name="userForm" validate="true" input="/pages/login.jsp" > <forward name="success" path="/pages/welcome.jsp"/> </action> </action-mappings> <message-resources parameter="com.mkyong.common.properties.Common" /> </struts-config>
7. web.xml
Final step, create a web.xml for the Strut framework integration.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Access it
http://localhost:8080/StrutsExample/LoginPage.do

If user didn’t fill in the text box and press the submit button, it will display a error message.

If user fill in the text box and press the submit button.

It will pass the text box input value to the welcome page and display it.








Does this do encoding bydefault?
html:text tag encode string?
how to genarate calendar component in jsp page by usig struts
Struts 1 don’t have calender component, try JavaScript calender component instead.
good…
[...] Struts textbox example [...]
good tutorial for beginners..
Thank you.
[...] Struts <html:text> textbox example will be refactor to use the “DynaActionForm” instead of normal [...]
[...] Struts <html:text> textbox example | struts [...]