大家!不好意思,我是Spring技术的初学者。我正在使用SpringMVC,但是我无法通过RestController方法获取任何HTML页面,但是可以成功获取JSONs或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
。一些主要的detil如下所示。
我的控制器:
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[] {"/"};
}
}
我的第3个配置类(用于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;
}
}
感谢您的帮助!
答案 0 :(得分:0)
您将要:
答案 1 :(得分:0)
问题解决了。我采取了以下步骤:
我将@RestController
更改为@Controller
。
我在方法中将viewResolver.setSuffix(".html");
替换为viewResolver.setSuffix(".jsp");
,并返回InternalResourceViewResolver
。
我将所有html文件中的文件扩展名从html替换为jsp。
该项目正在运行,但我不知道为什么它不适用于html文件。但是我的主要问题是在浏览器中获取任意视图。