I am dealing with a problem
Here is my EmployeeController:
EmployeeController
package com.examenjsp.jsp.controllers;
import java.sql.ResultSet;
import java.sql.SQLException;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.examenjsp.jsp.jdbcConnect;
@Controller
@EnableWebMvc
@RequestMapping("/views") // Applies to all routes within this controller
public class EmployeeController {
@GetMapping({"/", "/index"})
public String showIndex(Model model) {
String query = "SELECT * FROM employees";
String url = "jdbc:mysql://localhost:3306/employeesdb";
String name = "root";
String password = "rootadmin223";
String table = jdbcConnect.GetTable(query, url, name, password);
model.addAttribute("employeeTable", table);
return "index"; //
}
@PostMapping("/action_page")
public String addEmployee(
@RequestParam("first_name") String firstName,
@RequestParam("last_name") String lastName,
@RequestParam("salary_pm") float salaryPm,
@RequestParam("hire_date") String hireDate,
@RequestParam("company") String company,
@RequestParam("department") String department) {
jdbcConnect.addValue(firstName, lastName, salaryPm, hireDate, company, department);
return "redirect:/views/index"; // Redirects back to index after adding
}
@PostMapping("/edit_employee")
public String editEmployee(@RequestParam("employee_id") int employeeId, Model model) {
try {
ResultSet rs = jdbcConnect.getValue(employeeId);
if (rs != null && rs.next()) {
model.addAttribute("employeeId", employeeId);
model.addAttribute("firstName", rs.getString("first_name"));
model.addAttribute("lastName", rs.getString("last_name"));
model.addAttribute("salaryPm", rs.getFloat("salary_pm"));
model.addAttribute("hireDate", rs.getString("hire_date"));
model.addAttribute("company", rs.getString("company"));
model.addAttribute("department", rs.getString("department"));
return "edit_employee"; // Shows edit_employee.jsp
} else {
model.addAttribute("error", "Employee not found");
return "error"; // Shows an error page if employee isn't found
}
} catch (SQLException e) {
e.printStackTrace();
model.addAttribute("error", "Error occurred while fetching employee details: " + e.getMessage());
return "error"; // Shows an error page on SQL exceptions
}
}
// PostMapping to update an employee's details
@PostMapping("/update_employee")
public String updateEmployee(
@RequestParam("employee_id") int employeeId,
@RequestParam("first_name") String firstName,
@RequestParam("last_name") String lastName,
@RequestParam("salary_pm") float salaryPm,
@RequestParam("hire_date") String hireDate,
@RequestParam("company") String company,
@RequestParam("department") String department) {
jdbcConnect.setValue(employeeId, firstName, lastName, salaryPm, hireDate, company, department);
return "redirect:/views/index"; // Redirects back to index after updating
}
// PostMapping to delete an employee
@PostMapping("/delete_employee")
public String deleteEmployee(@RequestParam("employee_id") int employeeId) {
jdbcConnect.dropValue(employeeId);
return "redirect:/views/index"; // Redirects back to index after deleting
}
}
and here is my ExamenJspSpringApplication:
ExamenJspSpringApplication
package com.examenjsp.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@SpringBootApplication()
@ComponentScan(basePackages = { "com.examenjsp.jsp", "com.examenjsp.jsp.configuration","com.examenjsp.jsp.controllers"}, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) })
public class ExamenJspSpringApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(ExamenJspSpringApplication.class, args);
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest()
.permitAll())
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}
}
and here is my app structure:
The dependencies in my pom seem to be in place and my application.properties looks like this:
application.properties
spring.mvc.static-path-pattern=/WEB-INF/views/
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
server.port=8082
spring.datasource.url=jdbc:mysql://localhost:3306/employeesdb
spring.datasource.username=root
spring.datasource.password=rootadmin223
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
What can I try to solve this?
6