index.jsp not working, just shows source code on b

2019-09-04 06:04发布

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.

Here is my 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();
        }
    }
}

And here is 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>

What could be wrong?

3条回答
一夜七次
2楼-- · 2019-09-04 06:20

You have two options to avoid this problem :

  • You have to use @Controller instead of @Restcontroller and add the bean InternalResourceViewResolver as said above to define the suffix and prefix of your pages.

See an example here : http://www.breathejava.com/spring-restful-web-service-tutorial-with-json-format-java-configuration/

  • Or, Develop in the client side an ajax request to consume your default web service. I prefer that you use AngularJS Framework.
查看更多
地球回转人心会变
3楼-- · 2019-09-04 06:25

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):

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

You can check the annotation inernals:

查看更多
做个烂人
4楼-- · 2019-09-04 06:40

I have always used the view resolver in spring bean instead of passing jsp name with extension in controller. You need the pass the jsp name without extension in this case just index and the view resolver will resolve it. It works fine. I believe this is the issue.

     <bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
           <property name="suffix">
              <value>.jsp</value>
           </property>
     </bean>

</beans>

Also you should use just @Controller annotation instead of @RestController if you dont have specfic requirement

查看更多
登录 后发表回答