How does Spring MVC decide on conflict between homecontroller and jsp?

schoon :

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

@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!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91758&siteId=1