Spring mvc请求的资源不可用

时间:2016-07-25 14:11:06

标签: spring spring-mvc tomcat

应用程序和我的浏览器无法呈现我的应用程序的任何资源。我看到很多类似的问题,找不到任何解决方案。应用程序通过json(即没有视图)正常工作

有我的配置

@Configuration
@EnableWebMvc
@ComponentScan("ru.practice.web")
public class ChatlocalWebConfig
extends WebMvcConfigurerAdapter {
 @Bean
 public ViewResolver viewResolver() {
   InternalResourceViewResolver resolv = new InternalResourceViewResolver();
   resolv.setPrefix("WEB-INF/views/");
   resolv.setSuffix(".jsp");
   resolv.setViewClass(JstlView.class);
   return resolv;
   }

}

初​​始化

public class ChatlocalInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
  return new String[] { "/" };
}


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

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

控制器

@Controller
@RequestMapping("/user")
public class UserController {
 @Autowired
UserRepository userRepository;

@RequestMapping(value = "/",method = RequestMethod.GET)
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World");
return "welcome";
}
}

和项目结构 image

2 个答案:

答案 0 :(得分:0)

不确定如何使用WebMvcConfigurerAdapter执行此操作,但这有效:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver r = new InternalResourceViewResolver();
        r.setPrefix("/WEB-INF/views/");
        r.setSuffix(".jsp");
        return r;
    }

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

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

答案 1 :(得分:0)

我认为这是你的资源路径问题。改变

resolv.setPrefix("WEB-INF/views/");

resolv.setPrefix("/WEB-INF/views/");

看看它是如何运作的。