3

I am converting a Spring MVC app to Spring Boot. The Spring app has a Home controller:

@RestController
public class HomeController {...
    ...
@RequestMapping("/login")
public String login(Principal p) {
    return "login";
    }
}

which should just display the word 'login'. This however seems to be ignored since it also has a login.jsp under src/webapp/WEB-INF/views which does a lot more, and this is what is displayed when it is run.

When I cut and paste simple-web-app into a Boot app I just get the word 'login'.

How does Spring magically ignore the word 'login' for the more complex jsp output? And how do I mimic this in Boot?

PS I have tried adding:

<dependency> 
    <groupId>org.apache.tomcat.embed</groupId> 
    <artifactId>tomcat-embed-jasper</artifactId> 
    <scope>provided</scope> 
</dependency>

spring.mvc.view.prefix=/views/ 
spring.mvc.view.suffix=.jsp
schoon
  • 2,858
  • 3
  • 46
  • 78

2 Answers2

3

@RestController is not used for returning rendered JSPs. This is why Spring Boot just responds with the string login. Try to change this to the following:

@Controller
public class HomeController {

    @RequestMapping("/login")
    public String login(Principal p) {
        //do something with your Principal if you want...
        return "login";
    }
}

If you configured everything else right, Spring will look for a template called login (your JSP) and use this as the view.

@RestController is a Spring annotation used to build a RESTful Web Service (Rest-API endpoint defined in a controller).

By the way, here you can find some more info on how to proceed and also, unfortunately, some reasons not to use JSPs with Spring Boot and embedded containers but rather use a different templating engine.

Hope this helps!

EDIT: Another answer to this question mentions view resolvers - this is indeed not unimportant! But it's kind of a special case with Spring Boot and JSPs. To get it right, this article i just found may help!

bkis
  • 2,530
  • 1
  • 17
  • 31
  • Thanks! It now gives 404 on `/swa-boot-0.0.1-SNAPSHOT/WEB-INF/views/login.jsp'. Is there something else I need to do to point it there? – schoon Jan 28 '19 at 16:02
  • 1
    @schoon i edited my answer and added a link to an article that seems to be exactly what you need. – bkis Jan 29 '19 at 08:56
1

In Spring framework, if you want to write a String to the response body then you'll use the keyword @ResponseBody. From the documentation

@ResponseBody annotation and, therefore, writes directly to the response body versus view resolution and rendering with an HTML template.

Or, in your case, the @RestController annotation that is

A convenience annotation that is itself annotated with @Controller and @ResponseBody.

If you don't use this keyword then Spring will search for a file with the given path. For this it uses a viewResolver. See 16.5 Views and resolving them

Here is an exemple where you can see the property that allow Spring to resolve the real path of your file :

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
alain.janinm
  • 19,951
  • 10
  • 65
  • 112