index jsp not working just shows source code on browser

index jsp not working just shows source code on browser  using -'java,spring,jsp,model-view-controller'

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;

public class IndexController {

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

And here is JSP:

<%@ page import="com.orantaj.model.BasketballEvent" %>
<%@ page import="java.util.List" %>

        <th>Maç Kodu</th>
        <th>Maç Saati</th>
    <%List<BasketballEvent> basketballEvents = (List<BasketballEvent>) request.getAttribute("basketballEvents");%>
    <%if (basketballEvents != null && basketballEvents.size() > 0) {%>
    <%for (BasketballEvent event : basketballEvents) {%>

        <td><%=event.getHomeTeam() + " " + event.getAwayTeam()%></td>


What could be wrong?

asked Oct 13, 2015 by vijaygupta1980
0 votes

3 Answers

0 votes

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:

answered Oct 13, 2015 by rolvyrf
0 votes

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.


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

answered Oct 13, 2015 by tseetha
0 votes

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 :

  • Or, Develop in the client side an ajax request to consume your default web service. I prefer that you use AngularJS Framework.
answered Oct 13, 2015 by ajit.chavhan