You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot整合JSP出现No mapping for GET请求错误的解决求助

Spring Boot Web应用“No mapping for GET”错误解决指南

问题描述

开发Spring Boot Web应用时出现“No mapping for GET”错误,错误界面如下:
错误截图1
错误截图2

相关代码与配置

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.RequestParam;
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
    }
}

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.EnableWebMvc;

@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();
    }
}

应用结构

应用结构截图

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

解决方法

1. 修正静态资源路径配置

spring.mvc.static-path-pattern是用来指定CSS、JS等静态资源的访问路径,你现在把它设成了视图文件所在的目录,会导致Spring无法正确解析控制器返回的视图。修改application.properties:

# 删除或注释掉错误的静态路径配置,若需要静态资源,可设置为/static/**
# spring.mvc.static-path-pattern=/WEB-INF/views/
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

2. 移除冗余的@EnableWebMvc注解

@EnableWebMvc会关闭Spring Boot的自动Web配置,你在控制器和主类都加了这个注解,大概率导致视图解析器失效。直接删掉EmployeeController上的@EnableWebMvc,主类上的也建议移除,除非你有自定义MVC配置的需求。

3. 简化组件扫描配置

@SpringBootApplication已经默认扫描当前包及其子包,不需要额外写@ComponentScan,而且你排除了Configuration类,可能导致必要的配置无法加载。修改主类:

@SpringBootApplication
public class ExamenJspSpringApplication extends SpringBootServletInitializer{
    // 原有main方法和SecurityFilterChain Bean保留
}

4. 确认访问路径正确

控制器根路径是/views,showIndex方法映射了/和/index,所以正确访问路径是http://localhost:8082/views/index或http://localhost:8082/views/,不要直接访问http://localhost:8082/,这个路径没有对应的控制器映射。

5. 检查JSP文件匹配度

确保WEB-INF/views下的JSP文件名和控制器返回的视图名完全一致,比如返回index就对应index.jsp,注意大小写敏感。

6. 排查JDBC工具类异常

虽然当前是映射错误,但如果jdbcConnect中的方法抛出未捕获异常,可能导致控制器无法正常返回视图。建议在控制器方法中加强异常处理,或者确保JDBC工具类的逻辑不会阻断请求流程。

内容的提问来源于stack exchange,提问作者Balta Cristian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:35:53