index.jsp无法正常工作,仅在浏览器上显示源代码 [英] index.jsp not working, just shows source code on browser
本文介绍了index.jsp无法正常工作,仅在浏览器上显示源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Spring MVC应用程序中,我试图将列表传递到JSP页面并在表上显示,但是我的 index.jsp 呈现得不好,仅显示了源代码在浏览器上.
In a Spring MVC application, I'm trying to pass a list to a JSP page and show it on a table, but my index.jsp isn't rendered well and just shows source code on browser.
这是我的控制人:
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
注释控制器类,而不是使用@RestController
注释,这意味着所有请求处理方法都采用@ResponseBody
语义,(来自Javadoc):
You may need to annotate your controller class with @Controller
instead of using the @RestController
annotation which implies that all request handling method assumes the @ResponseBody
semantics, which (from Javadoc):
表示方法返回值应绑定到Web响应正文.
indicates a method return value should be bound to the web response body.
您可以检查注释内部:
@RestController
@ResponseBody
这篇关于index.jsp无法正常工作,仅在浏览器上显示源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文