Sunday, July 12, 2009
Monday, September 22, 2008
HelloWorld in Struts 2.0
IDE: Eclipse (Version: 3.4.0)
Struts2: Version 2.0.11.2
Web Server: Apache Tomcat 6.0.18
Step 1) Create a new "Dynamic Web Project" using File -> New -> Dynamic Web Project.
Project Name: Struts2Demo
Context Root: Struts2Demo
Content Directory: WebContent
Java Source Directory: src
Step 2) Copy the following jars from Struts' lib[C:\struts-2.0.11.2\lib] to the Web-Project's lib directory (WebContent/WEB_INF/lib) [Minimum required set]
commons-logging-.jar
freemarker-.jar
ognl-.jar
struts2-core-.jar
xwork-.jar
Step 3) Create a new JSP file (HelloWorld.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a test!</h1>
<h2><s:property value="message" /></h2>
</body>
</html>
Step 4) Create the action class (HelloWorld):
package com.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts is up and running ...";
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
}
Step 5) Modify web.xml:
<?xml version="1.0"?>
<!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>Struts2Demo</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>HelloWorld.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 6) Create struts.xml in classpath (here in java source "src" folder):
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2demo" extends="struts-default">
<action name="HelloWorld" class="com.struts2demo.action.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
Step 7) Deploy the application to tomcat and start the server:
http://localhost:8080/Struts2Demo/ : you can see HelloWorld.jsp (with no message from server)
Now try,
http://localhost:8080/Struts2Demo/HelloWorld.action
you can see message "Struts is up and running ..." which was set in HelloWorld.java
This article has moved now to new my new BLOG.
Struts2: Version 2.0.11.2
Web Server: Apache Tomcat 6.0.18
Step 1) Create a new "Dynamic Web Project" using File -> New -> Dynamic Web Project.
Project Name: Struts2Demo
Context Root: Struts2Demo
Content Directory: WebContent
Java Source Directory: src
Step 2) Copy the following jars from Struts' lib[C:\struts-2.0.11.2\lib] to the Web-Project's lib directory (WebContent/WEB_INF/lib) [Minimum required set]
commons-logging-
freemarker-
ognl-
struts2-core-
xwork-
Step 3) Create a new JSP file (HelloWorld.jsp):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a test!</h1>
<h2><s:property value="message" /></h2>
</body>
</html>
Step 4) Create the action class (HelloWorld):
package com.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts is up and running ...";
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
}
Step 5) Modify web.xml:
<?xml version="1.0"?>
<!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>Struts2Demo</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>HelloWorld.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 6) Create struts.xml in classpath (here in java source "src" folder):
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2demo" extends="struts-default">
<action name="HelloWorld" class="com.struts2demo.action.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
Step 7) Deploy the application to tomcat and start the server:
http://localhost:8080/Struts2Demo/ : you can see HelloWorld.jsp (with no message from server)
Now try,
http://localhost:8080/Struts2Demo/HelloWorld.action
you can see message "Struts is up and running ..." which was set in HelloWorld.java
This article has moved now to new my new BLOG.
Labels: action class, Apache Tomcat, Eclipse, JSP, struts.xml, Struts2, Struts2 Demo, web.xml
Thursday, September 11, 2008
MyEclipse Blue vs IBM RAD
I was exploring whether we can replace IBM RAD with MyEclipse Blue.
To see the presentation click here.
This article has now moved to my new BLOG.
To see the presentation click here.
This article has now moved to my new BLOG.
Labels: IBM RAD, My Eclipse Blue, MyEclipse, Powerpoint Presentation, Presentation, Slideshow
Wednesday, August 20, 2008
Clustered Index Vs Non-clustered Index
A database index is a data structure that is used for faster retrieval of data from a database table.
Clustered Indexing: Indexing is done on physical storage. Therefore you can have only one clustered index per table. Generally clustered index is built using Primary Key.
Non-clustered Indexing: Indexing is done logically. Therefore you can have multiple non-clustered indexes per table.
Since, a table can have just one Clustered Index, indexing need to be done carefully. In general scenario apply clustered index unique (PK), most queried column.
Sometime, you may need to work with other non-sorted columns such as Name, Description ( especially , varchar/char typed columns ). Then your physical data store cannot be ordered physically. In this case, you should use Non-Clustered Index for these columns. Because Non-Clustered Indexes can have logical sorting order.
This article has now moved to my new BLOG.
Clustered Indexing: Indexing is done on physical storage. Therefore you can have only one clustered index per table. Generally clustered index is built using Primary Key.
Non-clustered Indexing: Indexing is done logically. Therefore you can have multiple non-clustered indexes per table.
Since, a table can have just one Clustered Index, indexing need to be done carefully. In general scenario apply clustered index unique (PK), most queried column.
Sometime, you may need to work with other non-sorted columns such as Name, Description ( especially , varchar/char typed columns ). Then your physical data store cannot be ordered physically. In this case, you should use Non-Clustered Index for these columns. Because Non-Clustered Indexes can have logical sorting order.
This article has now moved to my new BLOG.
Labels: Clustered Indexes Non-clustered Indexes Indexing Database
Monday, April 07, 2008
How to convert Java String to DOM document Object?
It's a simple thing to do.
Just provide String Object as an InputSource or
InputStream to DocumentBuilder.
Example:
String xmlString;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document =
builder.parse(new InputSource(new StringReader(xmlString)));
This article has moved to my new BLOG.
Labels: Document, DocumentBuilder, DocumentBuilderFactory, W3C, XML
Tuesday, March 04, 2008
Wednesday, December 19, 2007
Struts 2.0 Explained in Brief!
HighLights of Struts 2.0:
Architecture Explained:
Struts 2 is based on standard technologies such as Java filters, JavaBeans, ResourceBundles, Locales and XML, as well as few open source packages such as OGNL(Object Graph Navigation Language) and XWork.
How Struts 2.0 Framework works (in brief):
Major Differences from Struts 1:
This article has now moved to my new BLOG.
- Action classes are simplified POJOs (Plain Old Java Objects). Any Java class with execute() method can be used as an Action class.
- Struts 2 doesn't supports the ActionForm.
- Most of the configuration elements in the Struts 2 configuration have default values, so there is no need to set values unless you need to change the default value. This helps reducing the configuration requirement.
- Interceptors: Struts 2 introduces a new concept in the framework i.e. interceptors. Interceptors can be executed before and after an Action class is executed. Interceptors are configured to apply common functionality to a request like validation, checking session, etc. All the request pass through a set of interceptors before they are sent to an Action class and after the execution of Action class request passes through the interceptors again in the reverse order.
- Struts 2 tags provides support for AJAX.
- Framework automatically tracks checkboxes; if a checkbox is missing, the default value "false" is assumed.
- Plug-in: Struts 2 extensions can be installed by just dropping the jar files into /WEB-INF/lib directory. No manual configuration is required.
Architecture Explained:
Struts 2 is based on standard technologies such as Java filters, JavaBeans, ResourceBundles, Locales and XML, as well as few open source packages such as OGNL(Object Graph Navigation Language) and XWork.
How Struts 2.0 Framework works (in brief):
- An initial request goes to the servlet container where it passes though a chain of filters.
- Request passes through an optional ActionContextCleanUp filter (useful if integrating with other technologies).
- Then the FilterDispatcher (required) is called, which uses the ActionMapper to determine whether there is a need to invoke an action for this request. If yes, it delegates the control to the ActionProxy.
- ActionProxy utilizes the framework configuration files manager, which is initialized from the struts.xml file. ActionProxy creates an ActionInvocation, which is responsible for implementing the command pattern.
- The ActionInvocation process invokes the required interceptors and then invokes an Action.
- Once the Action is executed, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.
- Result is then executed, which, most of the time, renders a JSP or template written using FreeMarker or Velocity.
- Interceptors are executed again after completing the Action in the reverse order.
- If the ActionContextCleanUp filter is configured, the FilterDispatcher will not clean up the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not configured, the FilterDispathcer will clean up all the ThreadLocals present.
Major Differences from Struts 1:
- Filters have replaced servlets in Struts 2. In web.xml the tags and tags to map the controller, ActionServlet has been replaced by and tags to map the dispatcher, FilterDispatcher.
- ActionForms are obsolete now. Properties are part of the Action class.
- The default name of configuration file is struts.xml (this file should reside on the classpath of the webapp, generally /WEB_INF/classes).
- Struts 1 Actions are singletons and hence need to be thread-safe, since only one instance of the class will be available to handle all request for that Action. Struts 2 Actions are not singletons; they are instantiated for each request, and hence they need not be thread-safe.
- Struts 1 Actions use the Servlet API and hence are HTTP dependent. Struts 2 Actions are not coupled to a container and hence are HTTP independent.
- Unit Testing: In Struts 1 Actions can't be tested separately. Struts 2 Actions can be tested easily with the support of dependency injection. Actions can be instantiated with the properties set and the method invoked.
- Struts 2 supports Java 5 annotations, which can be used as an alternate to XML files.
This article has now moved to my new BLOG.
Labels: J2EE, Struts, Struts 1, Struts2, Struts2.0