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.


Hi Mkyong,
Thanks for sharing this example which i think is good for my study on STRUT. When i try to execute access the log in page, i have the following error:-
org.apache.jasper.JasperException: The absolute uri: http://struts.apache.org/tags-bean cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:114)
org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:316)
org.apache.jasper.compiler.TagLibraryInfoImpl.(TagLibraryInfoImpl.java:147)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:423)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1552)
org.apache.jasper.compiler.Parser.parse(Parser.java:126)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:146)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
I follow this step by step solution provided in your site — http://www.mkyong.com/struts/the-absolute-uri-httpstruts-apache-orgtags-bean-cannot-be-resolved-in-either-web-xml-or-the-jar-files-deployed-with-this-application/
still i get the same error. Kindly advice, thanks!
Thank’s you for share, it is excellent.
But, i don’t know, when i click button submit then it is show messages error:cannot be cast to org.apache.struts.action.Action.
I very hope help from you.
Thank’s verry much.
Oh, I see and fix success. :)
Thank’s for share.It is excellent
but, I don’t know, when i click submit then it show messages error: cannot be cast to org.apache.struts.action.Action.
I hope get help from you.
Thank’s you very much.
Nice tutorial…:) Thanx…!
Reset is not working .
Useful tutorial. Thanks.
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…
good tutorial for beginners..
Thank you.
very nice tutorial :)