Skip to content

Instantly share code, notes, and snippets.

@vernetto
Created November 23, 2023 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernetto/a7c2030aa066939876afcd4e80e0b5bb to your computer and use it in GitHub Desktop.
Save vernetto/a7c2030aa066939876afcd4e80e0b5bb to your computer and use it in GitHub Desktop.
basic webapp
using spring boot and java 17, what is the simplest way to create a basic web application, with a main page where I enter a search parameter and, with a click of a button, I execute a Java function and return a result web page?
ChatGPT
Creating a basic web application with Spring Boot and Java 17 that includes a main page with a search function is quite straightforward. Here's a simple guide on how to set it up:
1. Set Up Your Project
Use Spring Initializr: Go to Spring Initializr to generate a basic Spring Boot project. Choose Maven/Gradle as your build tool, Java as the language, and specify Java 17 as the version.
Dependencies: Add spring-boot-starter-web for web application development.
Generate and Open: Generate the project and open it in your preferred IDE (like IntelliJ IDEA or Eclipse).
2. Create a Controller Class
Controller Class: Create a new Java class annotated with @Controller. This class will handle web requests.
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
@Controller
public class MyController {
@GetMapping("/")
public String home() {
return "index"; // points to index.html
}
@PostMapping("/search")
public String search(@RequestParam String query, Model model) {
// Process the search here
String result = processSearch(query);
model.addAttribute("result", result);
return "result"; // points to result.html
}
private String processSearch(String query) {
// Implement your search logic here
return "Result for: " + query;
}
}
3. Create HTML Pages
Index Page (index.html): In src/main/resources/templates, create index.html with a form for input.
html
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<form action="/search" method="post">
<input type="text" name="query">
<button type="submit">Search</button>
</form>
</body>
</html>
Result Page (result.html): Create result.html to display the search results.
html
<!DOCTYPE html>
<html>
<head>
<title>Search Result</title>
</head>
<body>
<p>Search result: ${result}</p>
</body>
</html>
4. Run Your Application
Run your Spring Boot application from the IDE or using the command line (mvn spring-boot:run for Maven or gradle bootRun for Gradle).
Access http://localhost:8080 in your web browser.
5. Optional Enhancements
Add Error Handling: Implement error handling for your search function.
Styling: Use CSS to style your HTML pages.
Logging: Add logging for debugging and monitoring.
Advanced Search Logic: Enhance the processSearch method to perform more complex searches.
This setup gives you a basic web application where you can enter a search parameter on the main page, submit it, execute a Java function, and display the result on a new page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment