有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring DispatchServlet在Jetty中找不到资源

我看到很多人都有类似的问题,但没有一个答案适合我。我正在做的是尝试将Jetty与Spring 4嵌入到零配置中。为了更好地描述我的问题,我在下面写了一些代码:

JettyStarter。阶级

public class JettyStarter {
    ...
    private static final String CONFIG_LOCATION = "com....config";
    private static final String DEFAULT_MAPPING_URL = "/*";
    private static final String DEFAULT_CONTEXT_PATH = "/";
    ...

    private ServletContextHandler getServletContextHandler() throws IOException {
        WebApplicationContext context = getContext();

        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);

        contextHandler.setErrorHandler(null);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(context);

        ServletHolder servletHolder = new ServletHolder("Servlet Dispatcher", dispatcherServlet);
        servletHolder.setInitOrder(1);
        contextHandler.addServlet(servletHolder, DEFAULT_MAPPING_URL);


        contextHandler.addEventListener(new ContextLoaderListener(context));

        contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());

        contextHandler.setContextPath(DEFAULT_CONTEXT_PATH);

        return contextHandler;
    }

    private void startJetty() throws Exception
    {
        ...
        HandlerCollection handlers = new HandlerCollection();
        handlers.addHandler(getServletContextHandler());
        ...

        server.setHandler(handlers);
        server.start();
        server.join();
    }

    private WebApplicationContext getContext() throws IOException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(CONFIG_LOCATION);
        ...
        return context;
}
    ...
}

WebMvcConfig。阶级

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com...controllers")
public class WebMvcConfig extends WebMvcConfigurerAdapter{
    ...

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver()
    {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/view");
        internalResourceViewResolver.setSuffix(".jsp");
        internalResourceViewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        return internalResourceViewResolver;
    }
}

家庭控制器。阶级

@Controller
public class HomeController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@RequestParam(value="error", required=false) boolean error, ModelMap model) 
    {
        ...
        return "/pub/login";
    }
}

最后,在服务器启动后,我在尝试访问localhost:8080/login时收到了404错误消息

WARNING: No mapping found for HTTP request with URI [/WEB-INF/jsp/view/pub/login.jsp] in DispatcherServlet with name 'Servlet Dispatcher'

我很确定资源“login.jsp”位于包中的文件夹“/webapp/WEB-INF/jsp/view/pub”下。但为什么它一直说找不到它


解决方案:

奇怪的限制,我不能在8小时内回答自己的问题

通过跟踪Spring的源代码,我最终得到了我的页面显示。原因是我将Jetty ServletContextHandler的ResourceBase设置为“webapp”,并在其下创建了“WEB-INF”子文件夹,所有资源都位于“WEB-INF/jsp/view/…”下也但是ResourceHttpRequestHandler无法查看WEB-INF文件夹,因为我们可以在那里看到代码:

protected boolean isInvalidPath(String path) {
    return (path.contains("WEB-INF") || path.contains("META-INF") || StringUtils.cleanPath(path).startsWith(".."));
}

因此,解决方案是将资源库更改为“webapp/WEB-INF”,并将InternalResourceViewResolver前缀更改为“/jsp/view”。但它确实有效

现在我的问题是,ResourceHttpRequestHandler被用于直接限制资源访问,servlet不应该使用它,为什么我的非配置版本会加载它?但不适用于XML配置版本?使用任何其他处理程序的XML配置是否通过了此限制或我缺少的内容?感谢任何人转发


共 (1) 个答案

  1. # 1 楼答案

    您需要告诉Spring您已经为bean添加了注释。这通常是在XML文档中完成的,称为注释驱动。但是我相信你必须在@Configuration元素中使用@AnnotationDrivenConfigalon,这样你的bean才能自动连接

    编辑:正如OP提到的那样@AnnotationDrivenConfig已被弃用。 你能试试https://stackoverflow.com/a/8076045/2231632