Sir, I have to make a project in which a data from the database is to be displayed in combo box and depending on the choice made in combo box the data pertaining to it from the database is to be extracted and shown on the same page. Kindly provide me with the code to do the following task.
Hi Use the jquery ajax() to achiever this requirement.
Technologies: jsp,spring and jquery.
1) develop the jsp like: palce the jquery-1.10.2.min.js inside webcontent
<%@ 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> <script type="text/javascript" language="javascript" src="jquery-1.10.2.min.js"></script> <!-- <script type="text/javascript" src="json2.js"></script> --> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring Jquery Ajax Demo</title> <style> Table.GridOne { padding: 3px; margin: 0; background: lightyellow; border-collapse: collapse; width:35%; } Table.GridOne Td { padding:2px; //border: 1px solid #ff9900; border-collapse: collapse; } </style> <script type="text/javascript"> function madeAjaxCall(){ $.ajax({ type: "post", url: "./employee/add.htm", cache: false, data:"firstName=" + $("#firstName").val() + "&lastName=" + $("#lastName").val() + "&email=" + $("#email").val(), success: function(response){ $("#fNames").empty(); for(var i=0;response.length;i++) { //var res=response[i].firstName+" "+response[i].lastName+" "+response[i].email; //$("p").append(res); var res="<option value='"+response[i].firstName+"'>"+response[i].firstName+"</option>"; $("#fNames").append(res); } //for(var i=0;response.length;i++) { // var res=response[i]; // $("p").append(res); //} }, error: function(){ alert('Error while request..'); } }); } </script> </head> <body> <form name="employeeForm" method="post"> <table cellpadding="0" cellspacing="0" border="1" class="GridOne"> <tr> <td>First Name</td> <td><input type="text" name="firstName" id="firstName" value=""></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastName" id="lastName" value=""></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" id="email" value=""></td> </tr> <tr> <td colspan="2" align="center"><input type="button" value="Ajax Submit" onclick="madeAjaxCall();"></td> </tr> <tr> <td>FirstNames:</td> <td> <select id="fNames" name="fNames" > </select> </td> </tr> </table> </form> <h1>Spring Framework Jquery Ajax Demo</h1> <p></p> </body> </html>
2) develop the controlelr by using spring.
import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.technicalkeeda.bean.Employee; @Controller @RequestMapping("/employee/add.htm") public class EmployeeController { @RequestMapping(method = RequestMethod.POST) public @ResponseBody List<Employee> add(HttpServletRequest request, HttpServletResponse response) throws Exception { List<Employee> list=new ArrayList<Employee>(); Employee employee = new Employee(); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String email = request.getParameter("email"); employee.setEmail(email); employee.setFirstName(firstName); employee.setLastName(lastName); list.add(employee); System.out.println("fristname="+firstName); Employee employee1 = new Employee(); employee1.setEmail("[email protected]"); employee1.setFirstName("Sarala"); employee1.setLastName("A"); list.add(employee1); /*List<String> list=new ArrayList<String>(); list.add("Basha"); list.add("Sarala"); */ System.out.println("EmployeeController add methos was called="+list); //return employee; return list; } } 3) develop the Employee bean class package com.technicalkeeda.bean; public class Employee { private String firstName; private String lastName; private String email; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return firstName+" "+lastName+" "+email; } }
4) configure this controller inside spring configuration file.
Ads