Spring Mvc Java 无法在名称为 'dispatcher' 的 servlet 中解析名称为 'home' 的视图

Spring Mvc Java Could not resolve view with name 'home' in servlet with name 'dispatcher'

我的第一个应用程序有问题。我尝试解决这个问题几个小时。有什么想法吗??

HTTP Status 500 – Internal Server Error

类型异常报告

消息无法在名称为 dispatcher

的 servlet 中解析名称为 'home' 的视图

说明服务器遇到意外情况,无法完成请求。

异常

javax.servlet.ServletException: Could not resolve view with name 'home' in servlet with name 'dispatcher' org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1251) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

注意服务器日志中提供了根本原因的完整堆栈跟踪。 Apache Tomcat/9.0.1

//控制器

public class SystemController {


@Autowired
private SysDeaUseService sysDeaUseService;

@RequestMapping(value = {"/","/home"}, method = RequestMethod.GET)
public String home(ModelMap model) {
    return "home";
}

@RequestMapping(path = "/system", method = RequestMethod.GET)
public String sys(ModelMap model) {
    return "system";
}

@RequestMapping(path = "/system/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> getAllSystem() {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("Result", "Ok");
    resultMap.put("Records", sysDeaUseService.getAllSystem());
    return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
}

@RequestMapping(path = "/system/save-edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
        , produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> saveSystem(@ModelAttribute System system) {
    sysDeaUseService.saveSystem(system);
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("Result", "Ok");
    resultMap.put("Records", sysDeaUseService.getAllSystem());
    return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
}

@RequestMapping(path = "/system/delete", method = RequestMethod.POST)
public ResponseEntity<Map<String, Object>> deleteSystem(Integer systemId) {
    sysDeaUseService.deleteSystem(systemId);
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("Result", "Ok");
    return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);

}

//AppConfig

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@PropertySource({"classpath:application.properties"})
@ComponentScan(basePackages = {"com.wn"})
public class AppConfig extends WebMvcConfigurerAdapter {

@Autowired
private Environment env;

@Bean
public TilesViewResolver getTilesViewResolver() {
    TilesViewResolver tilesViewResolver = new TilesViewResolver();
    tilesViewResolver.setPrefix("/WEB-INF/Views/Page");
    tilesViewResolver.setSuffix(".jsp");
    tilesViewResolver.setViewClass(TilesView.class);
    return tilesViewResolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().indentOutput(true);
    converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}

private Properties setHibernateProperties() {
    Properties props = new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
            setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
            setProperty("hibernate.generate_statistics", "true");
            setProperty("hibernate.show.sql", "true");
        }
    };
    return props;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBuilder = new LocalSessionFactoryBean();

    sessionFactoryBuilder.setDataSource(prepareDataSource());
    sessionFactoryBuilder.setPackagesToScan(new String[]{"com.wn.entity"});
    sessionFactoryBuilder.setHibernateProperties(setHibernateProperties());

    return sessionFactoryBuilder;
}

@Bean
public DataSource prepareDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getProperty("jdb.driver.class.name"));
    dataSource.setUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user.name"));
    dataSource.setPassword(env.getProperty("jdbc.password"));

    return dataSource;
}

@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {

    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);

    return txManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
        }
    };
}

@Bean
public TilesConfigurer getTilesConfigurer() {
    TilesConfigurer tilesConfigurer = new TilesConfigurer();
    tilesConfigurer.setCheckRefresh(true);
    tilesConfigurer.setDefinitionsFactoryClass(TilesDefinitionsConfig.class);
    return tilesConfigurer;
}

//ApplicationInitializer

public class ApplicationInitializer implements WebApplicationInitializer {

   @Override
   public void onStartup(ServletContext container) throws ServletException {

    AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();

    rootCtx.register(AppConfig.class);
    container.addListener(new ContextLoaderListener(rootCtx));

    AnnotationConfigWebApplicationContext annotationConfigWebAppCtx = new AnnotationConfigWebApplicationContext();

    annotationConfigWebAppCtx.register(DispatcherServlet.class);

    ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(rootCtx));

    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
}

//TilesDefinitionConfig

public final class TilesDefinitionsConfig implements DefinitionsFactory{

private static final Map<String,Definition> tilesDefinitions = new HashMap<>();
private static final Attribute BASE_TEMPLATE = new Attribute("/WEB-INF/Views/tiles/layouts/layout.jsp");

@Override
public Definition getDefinition(String name, Request tilesContext) {
    return tilesDefinitions.get(name);
}

private static void addDefaultLayoutDef(String name, String title, String body) {
    Map<String, Attribute> attributes = new HashMap<>();

    attributes.put("title", new Attribute(title));
    attributes.put("header", new Attribute("/WEB-INF/Views/tiles/template/header.jsp"));
    attributes.put("menu", new Attribute("/WEB-INF/Views/tiles/template/menu.jsp"));
    attributes.put("body", new Attribute(body));
    attributes.put("footer", new Attribute("/WEB-INF/Views/tiles/template/footer.jsp"));

    tilesDefinitions.put(name, new Definition(name, BASE_TEMPLATE, attributes));
}

public static void addDefinitions(){
    addDefaultLayoutDef("home","Home", "/WEB-INF/Views/Page/home.jsp");
}

public static void addDefinitionsSystem() {
    addDefaultLayoutDef("system","Systemy", "/WEB-INF/Views/Page/system.jsp");
}

public static void addDefinitionsDeal() {
    addDefaultLayoutDef("deal","Umowy", "/WEB-INF/Views/Page/deal.jsp");
}

public static void addDefinitionsContact() {
    addDefaultLayoutDef("contact","Kontakt", "/WEB-INF/Views/Page/contact.jsp");
}

}

@控制器 public class 系统控制器 {

将控制器注释放入 class 并重新部署。在日志上启用调试,然后检查日志中的此值: RequestMappingHandlerMapping:- 将“{[/home],methods=[GET]}”映射到...

我发现哪里出了问题。

如果我在 class 中配置 Tiles - 我需要:

<dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>9.0.2</version>
    </dependency> 

tiles.xml不需要

在我的例子中,这是一个 文件名问题 ,我在文件中使用了 Spaces,在 调试后 我了解了问题并更改了文件名,并且 将 Space 替换为下划线或连字符 .