簡體   English   中英

嵌入式Jetty - Spring MVC - 視圖解析器 - 無XML - HTTP錯誤:404

[英]Embedded Jetty - Spring MVC - view resolver - NO XML - HTTP ERROR: 404

我正在嘗試使用嵌入式Jetty建立一個簡單的Spring MVC服務器。 我已經設置了服務器,啟用了spring並為.jsp文件配置了一個視圖解析器。 控制器給我404以下消息:

Problem accessing /jsp/test.jsp. Reason:
Not Found

有誰知道問題是什么? 我一直在谷歌上搜索兩天。

服務器:

private static final int DEFAULT_PORT = 8080;
private static final String CONTEXT_PATH = "/";
private static final String CONFIG_LOCATION = "spring.config";
private static final String MAPPING_URL = "/*";

private EmbeddedJettyServer(){
}

public static void main(String[] args) throws Exception {
    Server server = new Server(DEFAULT_PORT);
    server.setHandler(getServletContextHandler(getContext()));
    server.start();
    server.join();
}

private static ServletContextHandler getServletContextHandler(WebApplicationContext context) throws IOException {
    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setErrorHandler(null);
    contextHandler.setContextPath(CONTEXT_PATH);
    contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
    contextHandler.addEventListener(new ContextLoaderListener(context));
    contextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
    return contextHandler;
}

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

Spring配置/查看解析器:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter {

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

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/jsp/");
    viewResolver.setContentType("text/html; charset=UTF-8");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

控制器:

@RequestMapping("/")
public ModelAndView index() {
    return new ModelAndView("test");
}

編輯:文件夾結構:

│   pom.xml
│
├───src
│   ├───main
│   │   ├───java
│   │   │   ├───controller
│   │   │   │       Ping.java
│   │   │   │       Prime.java
│   │   │   │
│   │   │   ├───run
│   │   │   │       EmbeddedJettyServer.java
│   │   │   │
│   │   │   └───spring
│   │   │       └───config
│   │   │               WebMvcConfig.java
│   │   │
│   │   ├───resources
│   │   │   ├───html
│   │   │   │       test.html
│   │   │   │
│   │   │   └───jsp
│   │   │           test.jsp
│   │   │
│   │   └───webapp
│   │       └───jsp
│   │               test.jsp
│   │
│   └───test
│       └───java
└───target

pom.xml中:

    <resources>
        <resource>
            <targetPath>/webapp</targetPath>
            <directory>src/main/webapp</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

解決方案:在“EmbeddedJettyServer”中我改變了

"MAPPING_URL" to "/".
"ServletContextHandler" to "WebAppContext"

在pom中添加了missin依賴:

    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jsp</artifactId>
        <version>9.2.0.M0</version>
    </dependency>

還必須配置IDE以使用JDK而不是JRE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM