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

在Spring Maven项目中整合Full Calendar插件遇PHP/JSP交互问题

Hey there, let's break down your problem step by step — the core issue here is that you're mixing two entirely separate server-side tech stacks (Spring MVC/Java and PHP) which don't play nicely together out of the box. Let's unpack each part and fix it:

Why Your Current Setup Isn't Working

1. PHP code in JSP files won't execute

JSP files are parsed and run by Java application servers like Tomcat or Jetty. These servers have no idea how to handle PHP code — they'll just treat it as plain text and output it directly to the browser, not execute it. PHP needs its own runtime environment (like Apache + PHP-FPM or Nginx + PHP-FPM) to process the code, which your Spring server doesn't provide.

2. JavaScript fails when accessed via Spring Controller

This is almost always one of two issues:

  • Incorrect static resource paths: FullCalendar relies on CSS and JS files, and if you're referencing them with relative paths (like ./fullcalendar.min.js), Spring's MVC routing might be messing up the file location. You need to configure Spring to serve static resources properly and use absolute paths based on your app's context root.
  • Timing issues with JS execution: If you initialize FullCalendar before the DOM is fully loaded, the calendar element won't exist yet, so the JS code will fail.

3. Switching to PHP files doesn't help

Spring Controllers are designed to handle Java-based requests and render JSP/Thymeleaf templates. If you try to return a PHP file via a Spring Controller, Tomcat will just serve the raw PHP code as text — it won't pass it to a PHP runtime to execute. PHP files need to live in a PHP-enabled server's document root, not your Spring project.

Practical Solutions

The cleanest fix is to unify your tech stack. FullCalendar can pull event data directly from a Spring Controller via AJAX, no PHP needed. Here's how:

Step 1: Add a Spring API endpoint for events

@RestController
@RequestMapping("/api/calendar")
public class CalendarController {

    @GetMapping("/events")
    public List<CalendarEvent> getCalendarEvents() {
        // Replace this with your actual data fetch logic (DB, etc.)
        List<CalendarEvent> events = new ArrayList<>();
        events.add(new CalendarEvent(
            "Team Sync",
            LocalDateTime.now(),
            LocalDateTime.now().plusHours(1)
        ));
        return events;
    }

    // Simple POJO for event data
    public static class CalendarEvent {
        private String title;
        private LocalDateTime start;
        private LocalDateTime end;

        // Constructor, getters, setters
        public CalendarEvent(String title, LocalDateTime start, LocalDateTime end) {
            this.title = title;
            this.start = start;
            this.end = end;
        }

        // Getters for JSON serialization
        public String getTitle() { return title; }
        public LocalDateTime getStart() { return start; }
        public LocalDateTime getEnd() { return end; }
    }
}

Step 2: Fix your JSP's FullCalendar initialization

Make sure you reference static resources correctly and initialize the calendar after the DOM loads:

<!-- First, include FullCalendar dependencies with absolute paths -->
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/fullcalendar/main.min.css">
<script src="${pageContext.request.contextPath}/resources/fullcalendar/main.min.js"></script>

<div id="calendar"></div>

<script>
// Wait for the DOM to be fully loaded before initializing
document.addEventListener('DOMContentLoaded', function() {
    const calendarEl = document.getElementById('calendar');
    const calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'dayGridMonth',
        // Fetch events from your Spring API
        events: '${pageContext.request.contextPath}/api/calendar/events'
    });
    calendar.render();
});
</script>

Step 3: Configure Spring to serve static resources

Add this to your Spring MVC configuration (e.g., spring-servlet.xml or a @Configuration class):

<!-- For XML config -->
<mvc:resources mapping="/resources/**" location="/resources/"/>

Or for Java config:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

Option 2: Separate Spring and PHP services (If you must use PHP)

If you absolutely need to use PHP for data processing, you'll need to deploy your Spring app and PHP app as separate services (e.g., Spring on localhost:8080, PHP on localhost:80). Then:

  1. Write a PHP endpoint that returns event data as JSON.
  2. Update your JSP's JS to fetch data from the PHP endpoint, and handle cross-origin requests (add Access-Control-Allow-Origin headers in PHP or configure Spring CORS support).
Final Notes

Mixing Java and PHP server-side runtimes in the same deployment is almost never a good idea — it creates unnecessary complexity and environment conflicts. Sticking to one server-side tech stack will make your setup far more maintainable.

内容的提问来源于stack exchange,提问作者Brian Walzer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:16:27