Wednesday 2 December 2015

Ajax example using spring mvc

Write in jsp page

http://www.opensource-techblog.com/2015/07/aui-ajax-in-liferay-portlet.html

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />

<portlet:resourceURL id="findState" var="findState" ></portlet:resourceURL>

<script type="text/javascript">
$(document).ready(function(){

$( "#country" ).change(function() {
 $.ajax({
       url: "${findState}" ,
       type: 'POST',
       datatype:'json',
       data: {
 countryName: $("#country").val()
  },
           success: function(data){
           var content= JSON.parse(data);
           $('#state').html('');// to clear the previous option
           $.each(content, function(i, state) {
               $('#state').append($('<option>').text(state.name).attr('value', state.stateId));
           });
       }
   });
  });
});
</script>

<b>Change the Country State Change By Ajax</b> <br><br>
Country:
<select id="country" name="country">
<option value="select">Select Country</option>
<option value="india">India</option>
<option value="usa">USA</option>
</select>

<br><br>
State:
<select id="state" name="state">
</select>


SController.java

package com.test;

import java.io.IOException;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;


@Controller(value = "AjaxController")
@RequestMapping("VIEW")
public class AjaxController {
@RenderMapping
public String handleRenderRequest(RenderRequest request,RenderResponse response, Model model) {
return "view";
}

@ResourceMapping(value="findState")
    public void findStateForCountry(ResourceRequest request, ResourceResponse response) throws IOException  {
String countryName = ParamUtil.getString(request, "countryName");

JSONArray stateArray = JSONFactoryUtil.createJSONArray();
JSONObject stateObject,stateObject2;
if(countryName.equalsIgnoreCase("india"))
{
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "1");
stateObject.put("name", "Delhi");

stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "2");
stateObject2.put("name", "Gujrat");


}
else{

stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("stateId", "21");
stateObject.put("name", "LA");

stateObject2 = JSONFactoryUtil.createJSONObject();
stateObject2.put("stateId", "22");
stateObject2.put("name", "California");
}
   stateArray.put(stateObject);
   stateArray.put(stateObject2);
   response.getWriter().println(stateArray);
}



}


No comments:

Post a Comment