anishcana / jovial

SpringAjaxModule
0 stars 0 forks source link

Input Field #3

Open anishcana opened 7 months ago

anishcana commented 7 months ago

<!DOCTYPE html>

Search Page

Search Page

anishcana commented 7 months ago
<h2>Search Page</h2>
<input type="text" id="query" name="query" placeholder="Type your search query...">

<div id="searchResults">
    <!-- Search results will be displayed here -->
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $('#query').on('input', function() {
            var query = $(this).val();
            search(query);
        });
    });

    function search(query) {
        $.ajax({
            url: '/performSearch?query=' + query,
            type: 'GET',
            success: function(data) {
                displaySearchResults(data);
            }
        });
    }

    function displaySearchResults(results) {
        $('#searchResults').empty();
        $.each(results, function(index, result) {
            $('#searchResults').append('<div>' + result.title + '</div>');
        });
    }
<
anishcana commented 7 months ago
<div id="searchResults">
    <!-- Search results will be displayed here -->
</div>
anishcana commented 7 months ago

<!DOCTYPE html>

Search Page

Search Page

anishcana commented 7 months ago

Sample application.

Search Page

Search Page

anishcana commented 7 months ago

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.io.ClassPathResource;

@Controller public class DataController {

@GetMapping("/performSearch")
@ResponseBody
public List<SearchResult> performSearch(@RequestParam("query") String query) throws IOException {
    List<SearchResult> results = new ArrayList<>();

    // Load data from JSON file
    ObjectMapper objectMapper = new ObjectMapper();
    InputStream inputStream = new ClassPathResource("data.json").getInputStream();
    List<User> users = objectMapper.readValue(inputStream, new TypeReference<List<User>>() {});

    // Perform search based on query
    for (User user : users) {
        if (user.getName().toLowerCase().contains(query.toLowerCase())) {
            results.add(new SearchResult(user.getName()));
        }
    }

    return results;
}

// Inner class representing the structure of search results
public static class SearchResult {
    private String title;

    // Constructor, getters, and setters
    public SearchResult(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

}

anishcana commented 7 months ago

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.io.ClassPathResource;

@Controller public class DataController {

@GetMapping("/performSearch")
@ResponseBody
public List<SearchResult> performSearch(@RequestParam("query") String query) throws IOException {
    List<SearchResult> results = new ArrayList<>();

    // Load data from JSON file
    ObjectMapper objectMapper = new ObjectMapper();
    InputStream inputStream = new ClassPathResource("data.json").getInputStream();
    List<User> users = objectMapper.readValue(inputStream, new TypeReference<List<User>>() {});

    // Perform search based on query
    for (User user : users) {
        if (user.getName().toLowerCase().contains(query.toLowerCase())) {
            results.add(new SearchResult(user.getName()));
        }
    }

    return results;
}

// Inner class representing the structure of search results
public static class SearchResult {
    private String title;

    // Constructor, getters, and setters
    public SearchResult(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

}

anishcana commented 7 months ago

[ {"id": 1, "name": "John", "age": 30, "city": "New York"}, {"id": 2, "name": "Jane", "age": 25, "city": "Los Angeles"}, {"id": 3, "name": "Alice", "age": 35, "city": "Chicago"} ]

anishcana commented 7 months ago

@GetMapping("/performSearch") @ResponseBody public List performSearch(@RequestParam("query") String query) throws IOException { List results = new ArrayList<>();

// Load data from JSON file
ObjectMapper objectMapper = new ObjectMapper();
InputStream inputStream = new ClassPathResource("data.json").getInputStream();
List<User> users = objectMapper.readValue(inputStream, new TypeReference<List<User>>() {});

// Perform search based on query
for (User user : users) {
    if (user.getName().toLowerCase().contains(query.toLowerCase()) ||
        String.valueOf(user.getAge()).contains(query) ||
        user.getCity().toLowerCase().contains(query.toLowerCase())) {
        results.add(user);
    }
}

return results;

}

anishcana commented 7 months ago

public class DataObject { private int id; private String name; private int age; private String city;

// Constructor
public DataObject(int id, String name, int age, String city) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.city = city;
}

// Getters and setters
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

anishcana commented 7 months ago

[ { "id": 1, "name": "John", "age": 25, "city": "New York" }, { "id": 2, "name": "Alice", "age": 30, "city": "Los Angeles" }, { "id": 3, "name": "Bob", "age": 28, "city": "Chicago" } ]

anishcana commented 7 months ago

public class User { private int id; private String name; private int age; private String city;

public User(int id, String name, int age, String city) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.city = city;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

anishcana commented 7 months ago

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>

User Search

User Search

anishcana commented 7 months ago

User Search

<input type="text" id="searchInput" oninput="performSearch()" placeholder="Search by name or city">
<div id="searchResults"></div>
anishcana commented 7 months ago

User Search

<input type="text" id="searchInput" oninput="performSearch()" placeholder="Search by name or city">
<div id="searchResults"></div>
anishcana commented 7 months ago

User Search

<input type="text" id="searchInput" oninput="performSearch()" placeholder="Search by name or city">
<div id="searchResults"></div>
anishcana commented 7 months ago

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>

User Search

User Search

anishcana commented 7 months ago

import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;

@Controller public class SearchController {

@RequestMapping("/searchPage")
public String searchPage() {
    return "search"; // Assuming "search.jsp" is in the "WEB-INF/views" directory
}

@GetMapping("/performSearch")
public ModelAndView performSearch(HttpServletRequest request) {
    // Perform search logic here
    // Example:
    String query = request.getParameter("q");
    List<User> searchResults = searchService.performSearch(query);

    // Pass search results to the search.jsp page
    ModelAndView modelAndView = new ModelAndView("search");
    modelAndView.addObject("searchResults", searchResults);
    return modelAndView;
}

}

anishcana commented 7 months ago

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

public class SearchServiceImpl implements SearchService { private List userList;

public SearchServiceImpl(List<User> userList) {
    this.userList = userList;
}

@Override
public List<User> performSearch(String query) {
    List<User> searchResults = new ArrayList<>();
    for (User user : userList) {
        if (user.getName().toLowerCase().contains(query.toLowerCase()) ||
            user.getCity().toLowerCase().contains(query.toLowerCase())) {
            searchResults.add(user);
        }
    }
    return searchResults;
}

}

anishcana commented 7 months ago

import java.util.List;

public interface SearchService { List performSearch(String query); }

anishcana commented 7 months ago

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.ModelAndView;

@Controller public class SearchController {

@GetMapping("/search")
public ModelAndView search(@RequestParam("query") String query) {
    // Perform search logic here

    ModelAndView modelAndView = new ModelAndView("search"); // Assuming "search.jsp" is your view
    modelAndView.addObject("query", query);
    // Add any other objects you need to pass to the view

    return modelAndView;
}

}