Closed anishcana closed 4 months ago
Import statement for controller
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; 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;
[ {"name": "John"}, {"name": "Alice"}, {"name": "Bob"}, {"name": "Emma"}, {"name": "Michael"} ]
<!DOCTYPE html>
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.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 {
ObjectMapper objectMapper = new ObjectMapper();
InputStream inputStream = new ClassPathResource("data.json").getInputStream();
List<SearchResult> results = objectMapper.readValue(inputStream, new TypeReference<List<SearchResult>>() {});
// Perform filtering based on the query
// This is just a dummy implementation
return filterResults(results, query);
}
private List<SearchResult> filterResults(List<SearchResult> results, String query) {
// Implement your filtering logic here
// For example, filter results based on title containing the query
// This is just a dummy implementation
return results.stream()
.filter(result -> result.getTitle().toLowerCase().contains(query.toLowerCase()))
.toList();
}
}
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.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 {
ObjectMapper objectMapper = new ObjectMapper();
InputStream inputStream = new ClassPathResource("data.json").getInputStream();
List<SearchResult> results = objectMapper.readValue(inputStream, new TypeReference<List<SearchResult>>() {});
// Perform filtering based on the query
// This is just a dummy implementation
return filterResults(results, query);
}
private List<SearchResult> filterResults(List<SearchResult> results, String query) {
// Implement your filtering logic here
// For example, filter results based on title containing the query
// This is just a dummy implementation
return results.stream()
.filter(result -> result.getTitle().toLowerCase().contains(query.toLowerCase()))
.toList();
}
// 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;
}
}
}
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
Input field added
<!DOCTYPE html>
<!DOCTYPE html>
1. Controller class:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths;
@Controller public class JsonController {
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/getData")
@ResponseBody
public String getData() {
try {
// Read JSON file and return its content
return new String(Files.readAllBytes(Paths.get("data.json")));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
2. HTML file (index.html) with AJAX:
<!DOCTYPE html>
3. JSON file (data.json) with sample data:
{ "name": "John Doe", "age": 30, "email": "john.doe@example.com", "city": "New York", "country": "USA" }
AJAX:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
AJAX:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
Validation
<!DOCTYPE html>
Json format
<!DOCTYPE html>
<!DOCTYPE html>
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // Sample content String content = "This is a sample text to demonstrate search highlighting in JSP.";
// Get the search keyword from the request
String searchText = request.getParameter("searchText");
// Highlight the search keyword in the content
if (searchText != null && !searchText.isEmpty()) {
content = content.replaceAll("(?i)(" + searchText + ")", "<span style='background-color: yellow;'>$1</span>");
}
// Send back the updated content
out.print(content);
%>
// Highlight the search keyword in the content if (searchText != null && !searchText.isEmpty()) { content = content.replaceAll("(?i)(" + searchText + ")", "$1"); }
@Controller public class SearchController {
}
public class DataObject { private String name; // Add other fields as needed // Include getters and setters }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
Search Results
spring.mvc.view.prefix=/templates/ spring.mvc.view.suffix=.jsp