简体   繁体   English

Spring MVC Annot。 + Jetty 9 +视图解析器-找不到jsp页面

[英]Spring MVC Annot. + Jetty 9 + View Resolvers - Can't find jsp page

The question has been asked before however the answers given either have not worked for me, or the answers sidestepped the question completely in favour of a different base example. 之前已经问过这个问题, 但是给出的答案对我没有用,或者答案完全绕开了问题,转而使用其他基本示例。

I am attempting to use a simple base example of a Spring MVC servlet embedded in a Jetty instance. 我试图使用一个嵌入在Jetty实例中的Spring MVC servlet的简单基本示例。 My controllers are picking up the page requests and are returning the correct view name. 我的控制器正在接收页面请求并返回正确的视图名称。 It's when the jsp page is being looked for that fails. 在寻找jsp页面时失败了。 The problem, I believe, is around the Servlet Mappings, based on other answers I've seen, but I fail to see the solution in my situation. 我相信问题是基于我所看到的其他答案的Servlet映射,但我无法看到我所处的解决方案。

Jetty Server Setup: Jetty服务器设置:

public JettyServer(int port) throws JettyServerException {
    webServerPort = port;
    webServer = new Server(webServerPort);
    webServer.setStopAtShutdown(true);
    try {
        webServer.setHandler(getServletContextHandler(getContext()));
    } catch (IOException e) {
        throw new JettyServerException("Cannot instantiate JettyServer instance", e);
    }
}

private ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {

    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setContextPath("/");

    contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/");
    contextHandler.addEventListener(new ContextLoaderListener(context));
    contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());

    return contextHandler;
}

private WebApplicationContext getContext() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.company.webapptemplate.config");
    return context;
}

The Web App config class found under the package com.company.webapptemplate.config : com.company.webapptemplate.config下的Web App配置类:

@Configuration
@EnableWebMvc
@ComponentScan("com.company.webapptemplate.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

The controller code: 控制器代码:

@Controller
@RequestMapping("/")
public class ApplicationController {

    @RequestMapping(method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        return "index";
    } 
}

The non-java classes stuff under target/classes after running a maven / IntelliJ build: 运行Maven / IntelliJ构建后, target/classes下的非Java类内容:

|____webapp
| |____WEB-INF
| | |____views
| | | |____index.jsp
| | |____web.xml (unused)

The response when pointing my browser to localhost:8080 after running the app in IntelliJ: 在IntelliJ中运行应用程序后将浏览器指向localhost:8080时的响应:

Problem accessing /WEB-INF/views/index.jsp. Reason:

    Not Found

And the smoking gun in the logs that I don't know what to do about: 还有我不知道该怎么办的原木上的吸烟枪:

2014-09-11 23:12:54 DEBUG org.springframework.web.servlet.handler.AbstractHandlerMethodMapping:297 - Looking up handler method for path /WEB-INF/views/index.jsp
2014-09-11 23:12:54 DEBUG org.springframework.web.servlet.handler.AbstractHandlerMethodMapping:305 - Did not find handler method for [/WEB-INF/views/index.jsp]
2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@5ddbd0c7] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'
2014-09-11 23:12:54 TRACE org.springframework.web.servlet.handler.AbstractUrlHandlerMapping:127 - No handler mapping found for [/WEB-INF/views/index.jsp]
2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@a67e8f5] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'
2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@2bef3229] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'
2014-09-11 23:12:54 TRACE org.springframework.web.servlet.DispatcherServlet:1101 - Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@64c63847] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'
2014-09-11 23:12:54 WARN  org.springframework.web.servlet.DispatcherServlet:1120 - No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-44175c06'

If anyone can tell me which piece of the puzzle I'm missing I'd be greatly appreciative. 如果有人能告诉我我缺少哪一个难题,我将不胜感激。

Thanks. 谢谢。

Sotirios comment pushed me forward - how to get a Jsp handling servlet into the embedded Jetty.... Sotirios的评论推动了我前进-如何使Jsp处理servlet进入嵌入式Jetty。

In getServletContextHandler I changed the instantiation of contextHandler to now be a WebAppContext . getServletContextHandler我将contextHandler的实例更改为WebAppContext All the same methods apply to this. 所有相同的方法都适用于此。

I then created this method to take the returned WebAppContext and set up Jsp Handling (snippet taken from here : https://github.com/jetty-project/embedded-jetty-jsp/blob/master/src/main/java/org/eclipse/jetty/demo/Main.java ) 然后,我创建了此方法以采用返回的WebAppContext并设置Jsp处理(摘录自此处: https : //github.com/jetty-project/embedded-jetty-jsp/blob/master/src/main/java/org /eclipse/jetty/demo/Main.java

private void setupJspHandler(WebAppContext context) {

    //Ensure the jsp engine is initialized correctly
    JettyJasperInitializer sci = new JettyJasperInitializer();

    ServletContainerInitializersStarter sciStarter = new ServletContainerInitializersStarter(context);
    ContainerInitializer initializer = new ContainerInitializer(sci, null);
    List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
    initializers.add(initializer);
    context.setAttribute("org.eclipse.jetty.containerInitializers", initializers);
    context.addBean(sciStarter, true);

    // Set Classloader of Context to be sane (needed for JSTL)
    // JSP requires a non-System classloader, this simply wraps the
    // embedded System classloader in a way that makes it suitable
    // for JSP to use
    ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
    context.setClassLoader(jspClassLoader);

    // Add JSP Servlet (must be named "jsp")
    ServletHolder holderJsp = new ServletHolder("jsp",JspServlet.class);
    holderJsp.setInitOrder(0);
    holderJsp.setInitParameter("logVerbosityLevel","INFO");
    holderJsp.setInitParameter("fork","false");
    holderJsp.setInitParameter("xpoweredBy","false");
    holderJsp.setInitParameter("compilerTargetVM","1.7");
    holderJsp.setInitParameter("compilerSourceVM","1.7");
    holderJsp.setInitParameter("keepgenerated","true");
    context.addServlet(holderJsp, "*.jsp");
}

I imported the following in my maven pom: 我在maven pom中导入了以下内容:

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-jsp</artifactId>
  <version>${jetty.version}</version>
</dependency>

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>apache-jsp</artifactId>
  <version>${jetty.version}</version>
</dependency>

And also had to turn my logging down as the running of the servlet initializer dumped out logging for the WebApplicationInitializer scan as it checked the WHOLE classpath, although other questions and pages seem to give some hints on how to deal with this, for example.... 并且还不得不拒绝我的日志记录,因为servlet初始化程序的运行在检查整个类路径时会放弃对WebApplicationInitializer扫描的日志记录,尽管例如其他问题和页面似乎也提供了一些有关如何处理此问题的提示。 ..

https://jira.codehaus.org/browse/JETTY-1503 https://jira.codehaus.org/browse/JETTY-1503

Now it works like a beaut'. 现在,它像漂亮的东西一样工作。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM