0

everyone! Excuse me, I'm a beginner in Spring technologies. I'm using SpringMVC and I can't get any HTML page via my RestController methods, but objects in JSONs or String objects I get successfully. Any help, why I permanently get this message in Catalina log, when I want to get HTML page?

org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /WEB-INF/pages/login.html

The corresponding HTML files exist, but mb HandlerMapping doesn't find them. What I must add to my project to get my HTML page in my browser, when I visit URL http://localhost:8080/login?

I use Java configuration instead XML files. My entire project is located in GitHub: https://github.com/OlegSandro/first-rest-project/tree/security. Some main detils are shown below.

My controller:

package com.example.controller;

import com.example.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class TestController {

    @GetMapping("/profile")
    public String profile() {
        return "profile";
    }

    @GetMapping("/home")
    public String home() {
        return "/home";
    }

    @GetMapping("/login")
    public ModelAndView login() {
        User user = new User();
        user.setLogin("superuser");
        user.setPassword("124567890");
        user.setId_role(2);
        return new ModelAndView("login", "login", user);
        //return new ModelAndView("login");
    }
}

My 1st configuration class:

package com.example.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.example.controller" })
//@ComponentScan(basePackages = "com.example")
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".html");
        return viewResolver;
    }
}

My 2nd configuration class:

package com.example.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {AppConfiguraion.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {WebConfiguration.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

My 3rd configuration class (for Hibernate functionality, because ORM is another part of my project):

package com.example.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import static org.hibernate.cfg.AvailableSettings.*;

import java.util.Properties;

@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"),  @ComponentScan("com.example.service")})
public class AppConfiguraion {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean getSessionFactory(){
        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

        Properties props = new Properties();
        // Setting JDBC properties
        props.put(DRIVER, env.getProperty("mysql.driver"));
        props.put(URL, env.getProperty("mysql.url"));
        props.put(USER, env.getProperty("mysql.user"));
        props.put(PASS, env.getProperty("mysql.password"));

        // Setting Hibernate properties
        props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
        props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));

        // Setting C3P0 properties
        props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
        props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
        props.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment"));
        props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
        props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));

        factoryBean.setHibernateProperties(props);
        factoryBean.setPackagesToScan("com.example.model");

        return factoryBean;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager(){
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }
}

Thanks for advance for any help!

  • you just want \@Controller not \@RestController and the method needs to return the view name as a string. – mavriksc May 22 '19 at 15:30
  • @mavriksc I replaced @RestController to @Controller, but Tomcat returned the following, while I try to go to each of the web pages ``` org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /WEB-INF/pages/profile.html org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /WEB-INF/pages/login.html org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /WEB-INF/pages//home.html ``` – Oleg Sandro May 22 '19 at 15:40
  • `viewResolver.setPrefix("/WEB-INF/pages/");` the leading / here is wrong that means absolute path – mavriksc May 22 '19 at 15:46
  • @mavriksc Unfortunately it did not help, Tomcat refers to the same paths. But I find a way out for myself. I replaced all *.html files to *.jsp files, replaced `.html` to `.jsp` in ViewResolver's suffix and the server began to find my View files. But anyway, thanks a lot for your help – Oleg Sandro May 23 '19 at 09:36

2 Answers2

0

You will want to:

  1. use @Controller not @RestController
  2. add a class-level @RequestMapping
Christian B
  • 102
  • 3
  • Yes, I really must use /@Controller instead /@RestController. Thanks a lot for your help! But my problem was also within ViewResolver (see my answer below) – Oleg Sandro May 23 '19 at 09:44
0

The problem solved. I took the following steps:

  1. I changed @RestController to @Controller.

  2. I replaced viewResolver.setSuffix(".html"); to viewResolver.setSuffix(".jsp"); in my method, which return InternalResourceViewResolver.

  3. I replaced the file extension in my all html files from html to jsp.

The project is working, but I don't know, why it doesn't work with html files. But my main problem was to get the any View in my browser.