Pages Navigation Menu

Coding is much easier than you think

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

ActionError & ActionMessage Example in Struts 2

Posted by in Struts 1, Struts 2 Tutorial, Struts-2 | 5 comments

 
In this tutorial we will learn about ActionError & ActionMessage class and its usage.
 

 
a) ActionError class is used to send error feedback message to user and it get rendered in jsp by using <s:actionerror/> tag.
 
b) ActionMessage class – is used to send information feedback message to user, and it get rendered in jsp using <s:actionmessage/> tag.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
In this tutorial we will use the previous tutorials example to implement the functionality of ActionError and ActionMessage class.
 
download
 
Here’s a simple login form, display the error message (actionerror) if the username is empty, Otherwise redirect to another page and display the a welcome message (actionmessage).
 

1. Folder Structure

 

 

Action Class

 
The action class, do a simple checking to make sure that the username is not empty, if the userName is not valid then the action class set the error message with addActionError() , if its valid then it set the successful message with addActionMessage().
 

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 6677091252031583948L;

	private String userName;

	public String execute() {

		return SUCCESS;
	}

	public String getUserName() {
		return userName;
	}

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

	public void validate() {
		if (userName.isEmpty()) {
		addActionError("Username can't be blanked");
		} else {
		addActionMessage("Welcome " + userName + ", You have been Successfully Logged in");
		}
	}
}

 
You might be interested to read:

 

JSP

 
Two simple JSP pages with css style to customize the error message.
 
login.jsp
 

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<h3>ActionError & ActionMessage Example</h3>
	<s:actionerror />
	<s:form action="loginUser">
		<s:textfield name="userName" placeholder="Username" label="Username" />
		<s:submit value="Submit" />
	</s:form>
</body>
</html>

 
success.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h3>ActionError & ActionMessage Example</h3>
	<s:actionmessage />
</body>
</html>

 

struts.xml

 
The mapping in struts.xml should be as below

<struts>
	<package name="default" extends="struts-default">
		<action name="loginUser" class="com.action.LoginAction">
			<result name="success">success.jsp</result>
			<result name="input">login.jsp</result>
		</action>
	</package>
</struts>

 

Run it

 
http://localhost:8089/ActionErrorMessage/


 
When Username is invalid, display error message with <s:actionerror/>
 

 
When Username is valid, display welcome message <s:actionmessage/>
 

 

Note:

 
In struts 2 when you use <s:actionerror /> tag, it displays the errors with bullets, read the article on Change default style of s:actionerror / s:actionmessage tag in order to remove this
 

Read More

Different types of form bean in struts 1.X

Posted by in Struts 1

ActionForm Action form for action class.

DynaActionForm – No form bean is created. No Validations are done here. Form bean properties are specified in struts-config.xml file

ValidatorForm Uses validation framework for validating the form bean.

DynaValidatorForm – Similar to the ValidatorForm but form bean is not created and form bean properties are specified in the struts-config.xml file. Validations here are done according to the to the form bean name.

ValidatorActionForm – Validations are done according to the action class name and not according to the form bean name as in ValidatorForm. Form bean is still created.

DynaValidatorActionForm –> Similar to the ValidatorActionForm but form bean is not created and form bean properties are specified in the struts-config.xml file. Validations are done according to the Action class name.

Read More