Saturday, September 22, 2012
Dependable drop downs in Struts 2 using database
JSP PAGE IS :-
<%@ include file="../others/includes.jsp"%>
<script type="text/javascript">
function populateCountries(){
var selectedRegion = $('#register_region').val();
$("#register_country").empty().append('<option value="0">Select your country</option');
$("#register_state").empty().append('<option value="0">Select your state</option');
$("#register_city").empty().append('<option value="0">Select your city</option');
$.ajax({
url: "../views/getCountries",
type: "POST",
async: true,
data: {regionId: selectedRegion},
cache: false,
dataType: "json",
success: function(result){
if (result.length == 0){
$('#register_country').append('<option value="0">No data found</option>');
}
else{
for (var key in result) {
$('#register_country').append('<option value="'+result[key].countryId+'">'+result[key].countryName+'</option>');
}
}
},
error: function(xhr, ajaxOptions, thrownError){
alert("An error occured: " + thrownError +" "+ajaxOptions+" "+xhr.status + " " + xhr.statusText);
}
});
}
function populateStates(){
var selectedCountry = $('#register_country').val();
$("#register_state").empty().append('<option value="0">Select your state</option');
$("#register_city").empty().append('<option value="0">Select your city</option');
$.ajax({
url: "../views/getStates",
type: "POST",
async: true,
data: {countryId: selectedCountry},
cache: false,
dataType: "json",
success: function(result){
if (result.length == 0){
$('#register_state').append('<option value="0">No data found</option>');
}
else{
for (var key in result) {
$('#register_state').append('<option value="'+result[key].stateId+'">'+result[key].stateName+'</option>');
}
}
},
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
}
function populateCities(){
var selectedState = $('#register_state').val();
$("#register_city").empty().append('<option value="0">Select your city</option');
$.ajax({
url: "../views/getCities",
type: "POST",
async: true,
data: {stateId: selectedState},
cache: false,
dataType: "json",
success: function(result){
if (result.length == 0){
$('#register_city').append('<option value="0">No data found</option>');
}
else{
for (var key in result) {
$('#register_city').append('<option value="'+result[key].cityId+'">'+result[key].cityName+'</option>');
}
}
},
error: function(xhr){
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
}
</script>
<body id="main" onload="document.formname.reset();">
<s:form action="getRegions" name="formname">
<s:select cssStyle="WIDTH:300px" list="region" listKey="regionId" listValue="regionName" label="Select your region" headerKey="0" headerValue="Select your region" name="register_region" id="register_region" onchange="populateCountries();"></s:select>
<s:select cssStyle="WIDTH:300px" list="country" listKey="countryId" listValue="countryName" label="Select your country" headerKey="0" headerValue="Select your country" name="register_country" id="register_country" onchange="populateStates();" ></s:select>
<s:select cssStyle="WIDTH:300px" list="state" listKey="stateId" listValue="stateName" label="Select your state" headerKey="0" headerValue="Select your state" name="register_state" id="register_state" onchange="populateCities();"></s:select>
<s:select cssStyle="WIDTH:300px" list="city" listKey="cityId" listValue="cityName" label="Select your city" headerKey="0" headerValue="Select your city" name="register_city" id="register_city" onchange=""></s:select>
</s:form>
</body>
ACTION CLASS IS :-
package com.aih.projects.actions.sd;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import net.sf.json.JSONArray;
import com.aih.projects.beans.City;
import com.aih.projects.beans.Country;
import com.aih.projects.beans.Region;
import com.aih.projects.beans.State;
import com.aih.projects.beans.sd.Food;
import com.aih.projects.commons.sd.Constants;
import com.aih.projects.commons.sd.Parent;
import com.aih.projects.dao.sd.DBInteractionForFood;
import com.aih.projects.dao.sd.DBInteractionForMember;
import com.aih.projects.dao.sd.DBInteractionForWorldLocations;
import com.aih.projects.utility.SendEmail;
import com.aih.projects.utility.UtilityFunctions;
public class DatabaseAction extends Parent implements SessionAware{
private static final long serialVersionUID = 1L;
private JSONArray jArray;
private String dbHitCounter;
private String noOfRecords;
private String email;
private String password;
private String zip;
private InputStream inputStream;
private Map<String, Object> session;
private String text;
private ArrayList<Region> region;
private ArrayList<Country> country;
private ArrayList<State> state;
private ArrayList<City> city;
private String regionId;
private String countryId;
private String stateId;
private String cityId;
private JSONArray jArrayRegions;
private JSONArray jArrayCountries;
private JSONArray jArrayStates;
private JSONArray jArrayCities;
public JSONArray getJArrayRegions() {
return jArrayRegions;
}
public void setJArrayRegions(JSONArray arrayRegions) {
jArrayRegions = arrayRegions;
}
public JSONArray getJArrayCountries() {
return jArrayCountries;
}
public void setJArrayCountries(JSONArray arrayCountries) {
jArrayCountries = arrayCountries;
}
public JSONArray getJArrayStates() {
return jArrayStates;
}
public void setJArrayStates(JSONArray arrayStates) {
jArrayStates = arrayStates;
}
public JSONArray getJArrayCities() {
return jArrayCities;
}
public void setJArrayCities(JSONArray arrayCities) {
jArrayCities = arrayCities;
}
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
}
public String getStateId() {
return stateId;
}
public void setStateId(String stateId) {
this.stateId = stateId;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getRegionId() {
return regionId;
}
public void setRegionId(String regionId) {
this.regionId = regionId;
}
public ArrayList<Region> getRegion() {
return region;
}
public void setRegion(ArrayList<Region> region) {
System.out.println("Here");
this.region = region;
}
public ArrayList<Country> getCountry() {
return country;
}
public void setCountry(ArrayList<Country> country) {
this.country = country;
}
public ArrayList<State> getState() {
return state;
}
public void setState(ArrayList<State> state) {
this.state = state;
}
public ArrayList<City> getCity() {
return city;
}
public void setCity(ArrayList<City> city) {
this.city = city;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public void setJArray(JSONArray jArray) {
this.jArray = jArray;
}
public JSONArray getJArray() {
return jArray;
}
public String getNoOfRecords() {
return noOfRecords;
}
public void setNoOfRecords(String noOfRecords) {
this.noOfRecords = noOfRecords;
}
public String getDbHitCounter() {
return dbHitCounter;
}
public void setDbHitCounter(String dbHitCounter) {
this.dbHitCounter = dbHitCounter;
}
public String populateFoods(){
ArrayList<Food> foods = null;
String dbQuery = null;
try{
foods = DBInteractionForFood.getAllAvailableFoods(Integer.parseInt(dbHitCounter), Integer.parseInt(noOfRecords));
}catch (Exception e) {
e.printStackTrace();
SendEmail.postMail(new String[]{Constants.errorMail} , "Error from "+Constants.projectName, "Query is "+dbQuery+" and Method name is "+Thread.currentThread().getStackTrace()[2].getMethodName()+"\n\n\nError is" +UtilityFunctions.stackTraceToString(e));
}
jArray = UtilityFunctions.getListAsJsonArray(foods);
//System.out.println(jArray);
return SUCCESS;
}
public String registerMember(){
inputStream = DBInteractionForMember.registerMember(email, password, zip);
return SUCCESS;
}
public String logIn(){
inputStream = DBInteractionForMember.logIn(email, password);
session.put("anshul", inputStream);
return SUCCESS;
}
public String activateMember(){
inputStream = DBInteractionForMember.activateMember(email);
return SUCCESS;
}
public String getRegions(){
region = DBInteractionForWorldLocations.getRegions();
country = DBInteractionForWorldLocations.getCountries("0");
state = DBInteractionForWorldLocations.getStates("0");
city = DBInteractionForWorldLocations.getCities("0");
jArrayRegions = UtilityFunctions.getListAsJsonArray(region);
return SUCCESS;
}
public String getCountries(){
country = DBInteractionForWorldLocations.getCountries(regionId);
jArrayCountries = UtilityFunctions.getListAsJsonArray(country);
return SUCCESS;
}
public String getStates(){
state = DBInteractionForWorldLocations.getStates(countryId);
jArrayStates = UtilityFunctions.getListAsJsonArray(state);
return SUCCESS;
}
public String getCities(){
city = DBInteractionForWorldLocations.getCities(stateId);
jArrayCities = UtilityFunctions.getListAsJsonArray(city);
return SUCCESS;
}
public static void main(String args[]){
DatabaseAction a = new DatabaseAction();
System.out.println(a.getRegions());
}
}
STRUTS.XML is:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="test" namespace="/views" extends="struts-default, json-default">
<action name="getRegions" class="com.aih.projects.actions.sd.DatabaseAction" method="getRegions">
<result name="SUCCESS">test1.jsp</result>
</action>
<action name="getCountries" class="com.aih.projects.actions.sd.DatabaseAction" method="getCountries">
<result type="json" name= "SUCCESS"><param name="root">jArrayCountries</param></result>
</action>
<action name="getStates" class="com.aih.projects.actions.sd.DatabaseAction" method="getStates">
<result type="json" name= "SUCCESS"><param name="root">jArrayStates</param></result>
</action>
<action name="getCities" class="com.aih.projects.actions.sd.DatabaseAction" method="getCities">
<result type="json" name= "SUCCESS"><param name="root">jArrayCities</param></result>
</action>
</package>
</struts>
Subscribe to:
Post Comments (Atom)
SpringBoot Application Event Listeners
When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...
-
1. Create a tld file inside WEB-INF folder having declaration of the custom function as below <?xml version="1.0" encoding=...
-
JOIN : Return rows when there is at least one match in both tables LEFT JOIN : Return all rows from the left table, even if there are no m...
can you please make it understandable as i can't understand the code and how the database values are coming.????
ReplyDeleteam i need json plugin only?what need for ajax ?thanks.
ReplyDelete