Showing posts with label Struts 2 Example. Show all posts
Showing posts with label Struts 2 Example. Show all posts

Struts 2 AJAX Autocomplete dropdown example

I started playing with struts 2.0.14 showcase application and thought of extracting a simple AJAX Autocomplete dropdown example from it.
In this example I am trying to create a autocomplete textbox and when you select one value from this autocomplete drop down the other will populate accordingly.

This post is for developers who are new to Struts 2.

This example uses

- Struts 2.0.14 - Download Struts
- apache-tomcat-6.0.18 - Download Tomcat
- java version "1.6.0_10"
- freemarker template (2.3.8) for the second dropdown.

You can use this example as is or play around with it based on your need.

index.jsp :

This page redirects the request to the demo page where autocomplete text box is diplayed.


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



autocomplete.jsp

This page contains the core of this example, the Struts tags for Autocomplete.

  
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>



AutoComplete




Link two autocompleter elements. When the selected value in 'Autocompleter 1' changes,
the available values in 'Autocompleter 2' will change also.



Autocompleter 1



Autocompleter 2





options.ftl

This is Freemarker template to render the second drop down, which is populated based on selection in first autocomplete drop down.

  
[
<#list options as option>
["${option}"],

]


ListingAction.java

This action class is to populate values in the first autocomplete drop down.

  

package ajaxdemo.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ListingAction extends ActionSupport {
private List selectList = null;

public String execute() throws Exception {
selectList = new ArrayList();
selectList.add("Fruits");
selectList.add("Colors");
return SUCCESS;
}

public List getSelectList() {
return selectList;
}

public void setSelectList(List selectList) {
this.selectList = selectList;
}

}


DetailAction.java

This is a action class to populate the second autocomplete drop down based on selection value in first drop down.

  
package ajaxdemo.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class DetailAction extends ActionSupport {
private String select;
private List options = new ArrayList();

private static final long serialVersionUID = -8481638176160014396L;

public String execute() throws Exception {
if ("Fruits".equalsIgnoreCase(select)) {
options.add("apple");
options.add("banana");
options.add("grape");
options.add("pear");
} else if ("Colors".equalsIgnoreCase(select)) {
options.add("red");
options.add("green");
options.add("blue");
}
return SUCCESS;
}

public String getSelect() {
return select;
}

public void setSelect(String select) {
this.select = select;
}

public List getOptions() {
return options;
}
}


struts.xml

Struts config file for action mappings.

  



"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




/autocomplete.jsp


/options.ftl






web.xml

The application deployment descriptor.

  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

struts2
org.apache.struts2.dispatcher.FilterDispatcher



struts2
/*


index.jsp





Output

Go the URL http://localhost:8080/ajaxdemo/ in your favorite web browser. The page should look like this.




Web application directory Structure
This is how the application war directory structure should look like.



  
ajaxdemo/
ajaxdemo/index.jsp
ajaxdemo/autocomplete.jsp
ajaxdemo/options.ftl
ajaxdemo/WEB-INF/web.xml
ajaxdemo/WEB-INF/classes/struts.xml
ajaxdemo/WEB-INF/classes/ajaxdemo/action/DetailAction.class
ajaxdemo/WEB-INF/classes/ajaxdemo/action/ListingAction.class
ajaxdemo/WEB-INF/lib/*.jar



Here are the list of jar files you need to have in your application WEB-INF/lib dir.

  

antlr-2.7.2.jar
aopalliance-1.0.jar
classworlds-1.1.jar
commons-beanutils-1.7.0.jar
commons-chain-1.1.jar
commons-codec-1.3.jar
commons-collections-2.1.jar
commons-collections-3.1.jar
commons-digester-1.6.jar
commons-digester-1.8.jar
commons-el-1.0.jar
commons-fileupload-1.1.1.jar
commons-io-1.1.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
commons-validator-1.3.0.jar
dwr-1.1-beta-3.jar
freemarker-2.3.8.jar
jstl-1.1.0.jar
log4j-1.2.9.jar
myfaces-api-1.1.2.jar
myfaces-impl-1.1.2.jar
ognl-2.6.11.jar
oro-2.0.8.jar
plexus-container-default-1.0-alpha-10.jar
plexus-utils-1.2.jar
sitemesh-2.2.1.jar
spring-beans-2.0.5.jar
spring-context-2.0.5.jar
spring-core-2.0.5.jar
spring-web-2.0.5.jar
struts-core-1.3.5.jar
struts2-codebehind-plugin-2.0.14.jar
struts2-config-browser-plugin-2.0.14.jar
struts2-core-2.0.14.jar
struts2-jsf-plugin-2.0.14.jar
struts2-sitemesh-plugin-2.0.14.jar
struts2-struts1-plugin-2.0.14.jar
struts2-tiles-plugin-2.0.14.jar
tiles-api-2.0.4.jar
tiles-core-2.0.4.jar
tiles-jsp-2.0.4.jar
velocity-1.4.jar
velocity-dep-1.4.jar
velocity-tools-1.1.jar
xml-apis-1.0.b2.jar
xwork-2.0.7.jar




Download ajaxdemo.war here


struts form, struts 2 ajax autocomplete drop down code example, Struts2 ajax autocomplete tutorial with sample code, struts 2 ajax auto complete drop down code download, struts training, apache struts training online

Backporting Struts 2 to JDK1.4 or JDK 1.3 using Retrotranslator

Struts 2 framework and its dependencies available currently are compiled using JDK 1.5 and if you want to use same framework on JDK1.4 then you will require to backport these jars. Struts 2 framework core jars and its plugin jars can be translated to JDK 1.4 or 1.3 using Retrotranslator utility.

Retrotranslator is a open source project which does this translation.

You can read more about this on http://retrotranslator.sourceforge.net/

Here are the steps to create the backport jars for JDK 1.4 or JDK 1.3

1. Download Struts 2 - Alternative Java 4 JARs: from http://struts.apache.org.
This also contains the retrotanslator jar files

2. Unzip the downloaded file, this should create backport folder containing core jars, and retrotranslation jar files.

3. Now copy all your Struts 2 Jars (including dependancies) to a folder say (Struts2-1.5)

4. Create another folder for converted jar files say (Struts2-1.4)

5. Run below command for each jar in the Struts2-1.5 folder

  
java -jar retrotranslator-transformer-.jar -advanced -srcjar Struts2-1.5/struts2--.jar -destjar ./Struts2-1.4/struts2--.jar


Note: Replace the & tokens with appropriate jar values.
Here are commands for struts 2.0.11.2 backporting


java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-core-2.0.11.2.jar -destjar Struts2-1.4/struts2-core-j4-2.0.11.2.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/xwork-2.0.5.jar -destjar Struts2-1.4/xwork-j4-2.0.5.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-codebehind.jar -destjar ./Struts2-1.4/struts2-codebehind.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-core.jar -destjar ./Struts2-1.4/struts2-core-1.4.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-spring.jar -destjar ./Struts2-1.4/struts2-spring.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-pell.jar -destjar ./Struts2-1.4/struts2-pell.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-plexus.jar -destjar ./Struts2-1.4/struts2-plexus.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-struts1.jar -destjar ./Struts2-1.4/struts2-struts1.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-jsf.jar -destjar ./Struts2-1.4/struts2-jsf.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-sitegraph.jar -destjar ./Struts2-1.4/struts2-sitegraph.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-sitemesh.jar -destjar ./Struts2-1.4/struts2-sitemesh.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-tiles.jar -destjar ./Struts2-1.4/struts2-tiles.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/struts2-config-browser.jar -destjar ./Struts2-1.4/struts2-config-browser.jar

java -jar retrotranslator-transformer-1.2.2.jar -advanced -srcjar Struts2-1.5/xwork-2.0.5.jar -destjar ./Struts2-1.4/xwork-2.0.5.jar




6. This should create all your jars which are required for JDK 1.4.

7. Make sure you delete all JDK 1.5 compiled jars from your application lib and replace it with translated jars for JDK 1.4. Keeping both may create unknown issues.


Retrotranslator is not limited to backporting the Struts 2 framework. Its a utility which can be utilized for backporting any JDK 1.5 compiled code to JDK 1.4.

Backporting Struts 2 to JDK 1.4, Back porting Struts 2 to JDK 1.3, Back Porting example, Backporting using retrotranslator, Backporting Struts 2 using Retrotranslator, retrotranslator for struts 2, backporting all struts 2 plugin, struts 2 back port example

How can you set devMode to dynamically load struts.xml file in Struts 2?

The constant element has a name attribute and a value attribute. The struts.devMode setting determines whether or not the Struts application is in development mode. By default, the value is false, meaning the application is not in development mode.

The following constant element sets struts.devMode to true.

<struts>
struts.devMode" value="true"/>

...
struts>


Once you set this attribute to true Struts will check the config file for every request. This will save lot of development time as you need not restart server for struts.xml changes.

Second way of setting this attribute is struts.properties file which can overrides the value of default.properties file as below.
struts.devMode = true

Third way to do it is as a init-param to filter tag in deployment descriptor (web.xml) file as shown below


struts

org.apache.struts2.dispatcher.FilterDispatcher


struts.devMode
true




 



struts 2 development mode settings, struts 2 dev mode, loading struts.xml dynamically, dynamic configuration file loading struts 2, developer mode configuration setting

Struts 2 - AJAX Drop down Example


Struts2 AJAX Drop down Example, struts2 ajax auto populate drop down, struts2 ajax auto select drop down, Struts2 AJAX Example, Struts2 AJAX Drop down sample Code, Struts 2, beginners example for struts2, struts2 simple application,struts2 with ajax

Struts 2 is a powerful web application development framework. Its built on top of WebWork framework and rebranded as Struts 2. Due to excessive popularity of Struts 1 its difficult to search for much relevant documentation on web about struts2. Slowly the search engines are improving on that.

In this example when you select from one drop down the other will populate accordingly. You can use it as is or play around with it based on your need

Index.jsp

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



Listing.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
   
   
   
    Listing
   
   
   
   
   
   
   
   
   
   
    onchange="javascript:show_details();return false;" >
   
    id="details" href="%{d_url}" theme="ajax"
    listenTopics="show_detail" formId="frm_demo">
   

   

   
   


Detail.jsp

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






DetailAction.java

package ajaxdemo.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class DetailAction extends ActionSupport {
private String lst;
private List lstList = null;
private List lstList2 = null;

public String execute() throws Exception {

if (getLst() != null && !getLst().equals("")) {
populateDetail(getLst());
return SUCCESS;
} else {
return SUCCESS;
}
}

private void populateDetail(String id) {
lstList = new ArrayList();
if (id.equalsIgnoreCase("Fruits")) {
lstList.add("Apple");
lstList.add("PineApple");
lstList.add("Mango");
lstList.add("Banana");
lstList.add("Grapes");
} else if (id.equalsIgnoreCase("Places")) {
lstList.add("New York");
lstList.add("Sydney");
lstList.add("California");
lstList.add("Switzerland");
lstList.add("Paris");
} else {
lstList.add("Other 1");
lstList.add("Other 2");
lstList.add("Other 3");
lstList.add("Other 4");
lstList.add("Other 5");
}
}

public List getLstList() {
return lstList;
}

public void setLstList(List lstList) {
this.lstList = lstList;
}

public String getLst() {
return lst;
}

public void setLst(String lst) {
this.lst= lst;
}
}


DetailListing.java


package ajaxdemo.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

public class ListingAction extends ActionSupport {
private List lstList1 = null;

public String execute() throws Exception {
populateDetail();
return SUCCESS;
}

private void populateDetail() {
lstList1 = new ArrayList();
lstList1.add("Fruits");
lstList1.add("Places");
lstList1.add("Others");

}

public List getLstList1() {
return lstList1;
}

public void setLstList1(List lstList1) {
this.lstList1 = lstList1;
}
}


Struts.xml


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



/listing.jsp


/detail.jsp





web.xml




struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*


index.jsp