gpt4 book ai didi

java - 我无法通过 RestController 方法获取任何 HTML 页面

转载 作者:行者123 更新时间:2023-12-02 05:21:11 25 4
gpt4 key购买 nike

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

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

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

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

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

我的第一个配置类:

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

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

最佳答案

您会想要:

  1. 使用@Controller而不是@RestController
  2. 添加类级别@RequestMapping

关于java - 我无法通过 RestController 方法获取任何 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56260196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com