Pages Navigation Menu

Coding is much easier than you think

FieldError in Struts 2 Example

Posted by on Aug 14, 2014 in Struts 2 Tutorial, Struts-2 | 6 comments

Field error in Struts 2
In our previous tutorial we learnt about actionError and actionMessage in struts 2, in this tutorial, we are going to describe the fielderror tags. The fielderror tag is a UI tag that renders field errors if they exist.


** UPDATE: Struts 2 Complete tutorial now available here.

Folder Structure

Struts 2 Field Error

Action Class

Develop an action class using addFieldError(String fieldName, String errorMessage)method. This method adds an error message for a given field to the corresponding jsp page.

Here the error messages is added to each field individually using the addFieldError() method. The error messages can either be specified in a separate property file or can be added directly in the Action class itself.

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 6677091252031583948L;

	private String userName;
	private String password;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;

	public String execute() {
		return SUCCESS;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;

	public void validate() {

		if (userName.isEmpty()) {
			addFieldError("userName", "Username can't be blank");
		if (password.isEmpty()) {
			addFieldError("password", "Password Can't be blank");
		else {
		addActionMessage("Welcome " + userName + ", You have been Successfully Logged in");

Here when either of userName or password is empty then fieldError is added this action via addFiedError method, so the execute method does not get invoked, and interceptors take case altering the flow of response by sending “error” string.
If userName and password are valid then a success message is added to actionMessage, the execute method get invoked, the jsp page get displayed based on execute methods returned value.
Recommended Article



File: Login.jsp
Create a jsp page that will display your field error messages (when fails to logged-in) using <s:fielderror/> tag as shown:

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


FieldError in Struts 2

File : Success.jsp

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

Welcome Page

ActionError & ActionMessage Example


Note :

We have done the coding such that If the username or password is empty , then the user will be forwarded to login.jsp where the field error added using addFieldError will get displayed.

You might be interested to read:



Make sure that the struts.xml has the following mapping


The web.xml configuration is same as given in hello world programe.


On running the application
FieldError in Struts 2
When Username or password is empty, display error message set by <s:fielderror/>
FieldError in Struts 2 Error
When Username and password is non empty, then application executes successfully and redirects to success.jsp and displays the ActionMessage setted in Validate method via <s:actionmessage/> tag.
FieldError in Struts 2 Action Message


FieldError Apache

Read More

How to Override Default Theme in Struts 2 ?

Posted by on Jan 13, 2014 in Struts 2 Tutorial, Struts-2 | 3 comments

Struts 2 have theme generation functionality due to which it automatically generates Table based HTML code for its tags. This is due the default theme (x_html)
** UPDATE: Struts 2 Complete tutorial now available here.
Other themes are

  • simple
  • css_xhtml
  • ajax

You can change this theme setting to any other theme on tag level, page level or application level as shown below.

1. Tag Level

2. Page Level

a) Static value

b) Dynamic property


3. Application Level

Popular Post in Struts 2 :


Read More

Changing default style of s:actionerror / s:actionmessage tag

Posted by on Jan 4, 2014 in Struts 2 Tutorial, Struts-2 | 1 comment

default style
In struts 2 when you use <s:actionerror /> tag, it displays the errors with bullets, On viewing the page source of the jsp page, we can see that error message is displayed using

  • error 1
  • error 2

But suppose if our requirement is to display the error message without bullet in the action errors or action messages
** UPDATE: Struts 2 Complete tutorial now available here.
There are 2 ways to solve this problem
1. Customized the code in jsp, such that you can specify your own css for displaying the message

2. Change the file in the “/template/simple/actionerror.ftl” and put it in the /web-directory/struts/simple if you are using simple theme

Recommended reading

Similar kind of approach is followed for <s:actionmessage /> tag.

Read More

How to get checkbox values from struts2 checkbox in displaytag to action class

Posted by on Dec 25, 2013 in Display Tag, Struts 2 Tutorial, Struts-2 | 6 comments

Consider a scenario, such that we have a list of items and each item can be selected by checking its checkbox. If a submit button is clicked after selecting all necessary checkboxes. So now, in our Action class, we could get the values of checkboxes which had been selected, by which we can implement delete functionality.
** UPDATE: Struts 2 Complete tutorial now available here.

The following snippet of code is used to retrieve the value from a checkbox used inside displaytag.


In our Action class, we can get the values of checkbox by creating getter & setter for the field “chkBox”.

Read More

Struts 2 tag not working in Display tag – Solution

Posted by on Dec 24, 2013 in Display Tag, Struts 2 Tutorial, Struts-2 | 1 comment

Consider a scenario, such that you want to display a value of a column based on certain condition.
For example:
if a student’s marks is greater than 50 display as ‘pass’, if its less than 50 then display as ‘fail’. For this scenario, in a display tag, most of us result in the following code snippet.



But here unfortunately this code does not works, because the variable “mark”is not reachable inside the display tag, since it is not associated with current display table.
** UPDATE: Struts 2 Complete tutorial now available here.
Solution :
So the correct way of coding is:



Generalized way to access the struts 2 tag via display tag is :- #attr.tableIdName.tableField. So the only way to access a struts 2 tag inside displaytag, is to follow the above syntax.
Example : To access a property tag inside display tag :

Related Questions:

How to get checkbox values from displaytag using struts2
Displaytag export option is not working?

Read More

Concept of Servlets Vs Concept of Struts 2

Posted by on Dec 22, 2013 in Java, Servlet, Struts 2 Tutorial, Struts-2 | 4 comments

In our previous article we have created a hello world program in struts2, in which I have passed parameter from request and setted in action class via a member variable, which may led you in some confusion, since in case of servlets the member variable in it are shared between all request and only those variable inside doget and dopost methods remains unique. So inorder to know why a member variable in struts 2 remains unique per request, we have look into the conceptual difference between Servlets and Struts 2. This article deals with the same.
Servlet vs Struts 2
** UPDATE: Struts 2 Complete tutorial now available here.
In case of Servlet when we have N number of request then only one object is created out of which N number of Threads are created , in which a request object is created. So the objects inside the thread are safe and not shared with other thread objects.
But suppose if the Servlet has Member variable, then those variable are not thread safe, as they are shared between all the threads created for each request.

The above concept is depicted pictographically below.

Recommended Article


Note: Servlet & Struts 1 follows same concept.


In case of struts 2 for N number of request N number of object is created. So in this case, even the member variable for these objects are not shared between objects of other request, that the reason Struts 2 is said to be thread safe.

The concept of Struts 2 is depicted pictographically as below.
In our next article we shall learn about the roles of Action class in struts 2.

Read More