gpt4 book ai didi

java - index.jsp 不起作用,仅在浏览器上显示源代码

转载 作者:行者123 更新时间:2023-12-01 11:45:23 26 4
gpt4 key购买 nike

在 Spring MVC 应用程序中,我尝试将列表传递到 JSP 页面并将其显示在表格上,但我的 index.jsp 渲染得不好,只显示源代码在浏览器上。

这是我的 Controller :

package com.orantaj.controllers;

import com.orantaj.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class IndexController {

@Autowired
EventService eventService;

@RequestMapping(value = "/")
public void setEvents(HttpServletRequest request, HttpServletResponse response) {

try {
request.setAttribute("basketballEvents", eventService.getBasketballEvents());
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}

这是 JSP:

<%@ page import="com.orantaj.model.BasketballEvent" %>
<%@ page import="java.util.List" %>
<html>
<head>
<title></title>
</head>
<body>

<table>

<tr>

<th>Maç Kodu</th>
<th>Lig</th>
<th>Maç Saati</th>
<th>Takımlar</th>
</tr>

 

<%List<BasketballEvent> basketballEvents = (List<BasketballEvent>) request.getAttribute("basketballEvents");%>

 <%if (basketballEvents != null && basketballEvents.size() > 0) {%>

 

<%for (BasketballEvent event : basketballEvents) {%>
 


<tr>

<td><%=event.getMatchCode()%></td>
<td><%=event.getLeague()%></td>
<td><%=event.getMatchDate()%></td>
<td><%=event.getHomeTeam() + " " + event.getAwayTeam()%></td>
</tr>

 
 <%
}
}
%>

</table>

</body>
</html>

可能出了什么问题?

最佳答案

您可能需要使用@Controller来注释您的 Controller 类,而不是使用@RestController注释,这意味着所有请求处理方法都假设@ResponseBody 语义,其中(来自 Javadoc):

indicates a method return value should be bound to the web response body.

您可以检查注释内部:

关于java - index.jsp 不起作用,仅在浏览器上显示源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29182591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com