0

每个人!打扰一下,我是 Spring 技术的初学者。我正在使用 SpringMVC,我无法通过我的 RestController 方法获取任何 HTML 页面,但我成功获取了 JSON 或字符串对象中的对象。任何帮助,当我想获取 HTML 页面时,为什么我会在 Catalina 日志中永久收到此消息?

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

相应的 HTML 文件存在,但 mb HandlerMapping 没有找到它们。当我访问 URL 时,我必须添加到我的项目中以在我的浏览器中获取我的 HTML 页面http://localhost:8080/login

我使用 Java 配置而不是 XML 文件。我的整个项目位于 GitHub https://github.com/OlegSandro/first-rest-project/tree/security:. 一些主要细节如下所示。

我的控制器:

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");
    }
}

我的第一个配置类:

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;
    }
}

我的第二个配置类:

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[] {"/"};
    }
}

我的第三个配置类(用于 Hibernate 功能,因为 ORM 是我项目的另一部分):

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;
    }
}

感谢您提前提供任何帮助!

4

2 回答 2

0

问题解决了。我采取了以下步骤:

  1. @RestController改为@Controller.

  2. 我在我的方法中替换viewResolver.setSuffix(".html");viewResolver.setSuffix(".jsp");返回InternalResourceViewResolver.

  3. 我将所有 html 文件中的文件扩展名从 html 替换为 jsp。

该项目正在运行,但我不知道为什么它不适用于 html 文件。但我的主要问题是在我的浏览器中获取任何视图。

于 2019-05-23T09:53:05.210 回答
0

你会想要:

  1. 使用 @Controller 而不是 @RestController
  2. 添加一个类级别的@RequestMapping
于 2019-05-22T16:03:12.137 回答