Spring MVC + jQuery | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link



Download Code and Jars : Link

File : Country.java

package com.codeFactory.bean;

public class Country {

	private int countryId;
	private String countryName;

	// Getters and setters.

}

File : Action.java

package com.codeFactory.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.codeFactory.bean.Country;

@Controller
public class Action {

	List<Country> allCountryList = new ArrayList<Country>();

	public List<Country> getAllCountryList() {
		return allCountryList;
	}

	public void setAllCountryList(List<Country> allCountryList) {
		this.allCountryList = allCountryList;
	}

	@RequestMapping(value = "/getCountryList", method = RequestMethod.GET)
	public @ResponseBody List<Country> getCountryList() {

		Country country;

		for (int i = 1; i <= 5; i++) {
			System.out.println(i);
			country = new Country();
			country.setCountryId(i);
			country.setCountryName("country" + i);
			allCountryList.add(country);
		}
		return allCountryList;
	}
}

File : index.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
	$(document).ready(function() {
		$(function(event) {$.getJSON('getCountryList', {}, function(allCountryList) {
				var select = $('#countryId');
				select.find('option').remove();
				$.each(allCountryList, function() {
					$('<option>').val(this.countryId).text(
					this.countryName).appendTo(select);
				});
			});
		});
	});
</script>
</head>
<body>
	<div class="form-group">
		<label class="control-label col-sm-4" for="sel1">Select country:</label>
		<div class="col-sm-8">
			<select class="form-control" id="countryId" name="countryId">
			</select>
		</div>
	</div>
</body>
</html>

File : web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
        <display-name>SpringJquery</display-name>
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

File : spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:p="http://www.springframework.org/schema/p" 
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		http: //www.springframework.org/schema/mvc/spring-mvc.xsd
		http: //www.springframework.org/schema/beans 
		http: //www.springframework.org/schema/beans/spring-beans.xsd
		http: //www.springframework.org/schema/context 
		http: //www.springframework.org/schema/context/spring-context.xsd
		http: //www.springframework.org/schema/tx
		http: //www.springframework.org/schema/tx/spring-tx.xsd">
	
	<context:component-scan base-package="com.codeFactory.controller"></context:component-scan>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean> 
	
	<mvc:annotation-driven>

      <mvc:message-converters>
          <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
          <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
      </mvc:message-converters> 
	</mvc:annotation-driven>
</beans>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s