0

I'm building a spring boot application. My problem is that when I run the project, my login page is not shown. This is my code:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestLogin {

    @RequestMapping("/")
    public String login() {
        return "login";
    }
}

I get only a white page and "login" is written in it. When I try to change the @RestController with @Controller I get this GET http://localhost:8080/ 404 (). My login.html page is located under the webapp>tpl>login.html

How can I display my login page?

Edit

This is my application class

@SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer {

    private static Logger logger = LoggerFactory.getLogger(ExampleApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ExampleApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(ExampleApplication.class, args);
    }
}
2
  • Are you working with JSPs? Commented Apr 16, 2017 at 21:20
  • @Paner I'm working with angular and html Commented Apr 16, 2017 at 21:21

4 Answers 4

2

I dont know your configuration but:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().authorizeRequests()
                                         .antMatchers("/**").permitAll();
        http.authorizeRequests().antMatchers("/**").permitAll();
    }
}

In the Application.properties file add:

spring.mvc.view.suffix: .html

Change @RestController to @Controller for RestLogin class. Also put your html file inside the static folder inside resources folder.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried to create a test.html in the resources>static folder, then I in the RestLogin class I did return "test" but I'm getting the same problem
When I change RestController annotation to Controller I got this GET localhost:8080 404 ()
you have to add the server context if you dont know it add server.contextPath=/server to your Application.properties file and then when you run it you should go to localhost:8080/server
Should the server.contextPath value be the same as what I have to put in the @RequestMapping ?
No, it is for the url.
|
0

You need an application class with a main method. See this tutorial.

Here's a snippet:

package hello;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

7 Comments

@Llg can you please share all of your code? It is hard to help you using only what you have provided.
@Llg it appears as though Spring is unable to find your login.html file. Where is it located in your project structure?
It's under two folders in the webapp folder
@Llg apologies, I see you have that in your post. You should create a resources directory and place login.html in /resources/templates/
Is spring boot only detecting files in the /ressouces/templates/ ? Is it impossible to keep them in the webapp? Excuse me I'm new to spring boot
|
0

This is the normal behavior.

New version of Spring web comes with @RestController annotation which nothing but @Controller + @ResponseBody. So when you have a return statement in a method you must use @RestController or annotate your method with @ResponseBody.

Here the problem is that Spring don't know a lot about the http method type, can you please try to use @GetMapping("/") to combinbe path and method at the same time.

1 Comment

It's recommanded to have web files under src/main/resources/templates/
0

According to your posted code and your description, you're getting an expected behavior.

When you annotate your controller with @RestController, that means that your methods on that controller will try to return their result as JSON.

According to your code:

@RestController
public class RestLogin {

    @RequestMapping("/")
    public String login() {
        return "login";
    }
}

You're returning the String "login", that's why you're getting empty white page with the word login as JSON

If you will change the @RestController to @Controller then it no longer will return your string as JSON,

but Spring will try to figure out from the that "login" string a view, and for that you'll need to add a view resolver bean to your project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.