Pages Navigation Menu

Coding is much easier than you think

How to use a JavaBean class as a property in struts 2 action class.

Posted by in Struts-2

 
In this tutorial we will learn how to use a JavaBean class as a property in struts 2 action.
 
Consider a scenario, in which you have written an action class, which in turn has a JavaBean class as its property as shown below.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Action class

 
HelloAction.java
 

package com.simplecode.action;

import com.opensymphony.xwork2.Action;
import com.simplecode.bo.User;

public class HelloAction implements Action
{
	private User user;
	
	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	public String execute()
	{
		if (getUser().getUserName().isEmpty())
		     return ERROR;
		else
		     return SUCCESS;
	}
}

 
Transferring the JavaBeans property values to Action class
 

In Struts 2 transferring of data to the Java bean object is automatically done by the params interceptor, all we have to do is to create a JavaBeans object and its corresponding getter and setter methods in the action class.
 
Retrieving the JavaBeans property from action class to Jsp
 
To refer the attributes of User class, we need to first get the User object and then access its properties. For example to access the User’s, userName attribute in the Action class, the following syntax is used.
 

getUser().getUserName();

 

JavaBean class

 
User.java
 

package com.simplecode.bo;

public class User
{
	private String userName;

	public String getUserName()
	{
		return userName;
	}

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

 

JSP

 
In jsp page the user attributes cannot be directly referenced. Since the attributes we refer in the jsp page belongs to the User object (JavaBean object) we need to go one level deeper to reference the attributes. To refer the userName attribute of User class, the value of the name attribute in the s:property tag should be
 

"user.userName"

 
login.jsp
 

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
	<h3>Struts 2 JavaBeans as property</h3>

	<s:form action="hello">
		<s:textfield name="user.userName"  label="User name"/>
		<s:submit name="submit" />
	</s:form>
</body>
</html>

 
success.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
	Welcome, <s:property value="user.userName" />
</body>
</html>

 
error.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Error Page</title>
</head>
<body>
Login failed! Please enter a valid user name
</body>
</html>

 

Demo

 

http://localhost:8089/JavaBean/

login
 
On entering the details and clicking the Submit button the following page will be dispalyed.
 
javabean op
 
In struts 2 there is a concept call Model objects, using which we can refer to the JavaBean objects attributes directly, instead of doing a deeper reference as shown in this example above. In our next article we will learn about struts 2 Model Objects which simplifies the amount of code written above via ModelDriven interface
 

dwd2
Zip file –JavaBean.zip

  Read More

Displaytag export option is not working- Solution

Posted by in Java, Struts 1, Struts-2 | 3 comments

 
In this article we will learn to resolve the issue which occurs while exporting the file using display tag,

 
In order to enable export functionality in display tag, configure display tag in JSP as shown below.
 

<display:table name="studentList" pagesize="5" export ="true" requestURI="/displaytag.jsp">
<display:column property="rollNo" title="Roll No"/>
<display:column property="studentName" title="Student Name"/>
<display:column property="department" title="Department"/>
<display:column property="rank" title="Rank"/>
</display:table>

 
Here export=”true” option enable export functionality in displaytag.
 

Displaytag Export Issue

 

After enabling displaytag export functionality, though it was working in test program, export option on display tag for CSV, Excel, XML and PDF was not working on actual project.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
My Application throws the following exception and error message, when user clicks on export button :
org.displaytag.exception.BaseNestableJspTagException Exception: [.TableTag] Unable to reset response before returning exported data. You are not using an export filter. . . . configure the export filter.
 

Cause of Displaytag export problem:

 
This issue occurs when you have declared filter in web.xml
 

Solution :

 
To fix displaytag export issue the following steps are needed to be followed:
 
Add ResponseOverrideFilter filter as shown below.
 

<filter>
        <filter-name>ResponseOverrideFilter</filter-name>
        <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>ResponseOverrideFilter</filter-name>
       <url-pattern>*.*</url-pattern>
</filter-mapping>

 
Make sure that display tag’s ResponseOverrideFilter placed as first filter in web.xml
 
Related Questions:

How to make Struts 2 tag work inside Display tag?
How to get checkbox values from displaytag using struts2
 

Read More

Model 1 and Model 2 (MVC) Architecture

Posted by in Java, Servlet, Struts 1, Struts-2 | 2 comments

 

Misconceptions

 
Student: Master,  what is the difference between MVC 1 and MVC 2 ?
Master: Grasshopper, there is no such thing as MVC 1 and MVC 2, there’s just MVC. if you meant to ask about the difference between Model 1 and Model 2 with respect to web applications, then this article will help you out.
 
In Java there are two types of programming models

  1. Model 1 Architecture
  2. Model 2 (MVC) Architecture

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

Model 1 Architecture

 
model-1-architecture
 

Flow of the Model 1 architecture.

  1. Browser sends request for the JSP page
  2. JSP accesses Business service Bean class and invokes business logic
  3. Business service Bean class connects to the database to store/retrieve data
  4. Response generated by JSP is sent to the browser

 
Disadvantage
Navigation control is decentralized
 

Model 2 (MVC) Architecture

 
model-2-architecture
 

Model 2 is based on the MVC (Model View Controller) design pattern.

  • Model         – Represents the data and business logic of the application.
  • View          – Represents the presentation.
  • Controller   – The controller module acts as an interface between view and model.

It intercepts all the requests i.e. receives input and commands to Model / View to change  accordingly.
 
Advantage of Model 2 (MVC) Architecture

  • Navigation control is centralized (Controller only has the control to determine the next page)
  • Easy to maintain, extend and test.

Disadvantage of Model 2 (MVC) Architecture
If we change the controller code, we need to recompile the class and redeploy the application.
 

Read More

How to Override Default Theme in Struts 2 ?

Posted by in Struts 2 Tutorial, Struts-2 | 3 comments

theme
 
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

<s:submit name="clear" action="ClearFormAction" value="Clear" theme="simple" />

 
2. Page Level

a) Static value

<s:set name="theme" value="'simple'" scope="page"/>

 
b) Dynamic property

<s:set name="theme" value="%{myTheme}" scope="page"/>

 

3. Application Level

<constant name="struts.ui.theme" value="simple"/>

 
Popular Post in Struts 2 :

 

Read More

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

Posted by 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
 

<ul>
<li>error 1</li>
<li>error 2</li>
</ul>

 
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
 

<s:if test="hasActionErrors()">
<s:iterator value="actionErrors">
<span class="msg"><s:property escape="false" />
</span>
</s:iterator>
</s:if>

 
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 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.

 

<display:table name="productList" pagesize="10" requestURI="productAction" id="row">

<display:column title="Delete">
<s:checkbox name="productList[%{#attr.row_rowNum - 1}].chkBox"
  id="check%{#attr.row_rowNum - 1}" value="%{#attr.row.chkBox}"/>
</display:column>

<display:column property="product" title="Product Name"/>
<display:column property="price" title="Price"/>

</display:table>
<s:submit action="removeProduct" value="Delete"
  onClick="return confirm('Do you want to delete these items?');"/>

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

Read More
Page 3 of 1012345...10...Last»