【问题标题】:Embedded Jetty - Spring MVC - view resolver - NO XML - HTTP ERROR: 404嵌入式 Jetty - Spring MVC - 视图解析器 - 没有 XML - HTTP 错误:404
【发布时间】:2014-05-04 18:36:11
【问题描述】:

我正在尝试使用嵌入式 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>

【问题讨论】:

  • 你的项目中是否存在jsp/test.jsp?
  • 是的。将文件夹结构添加到原始帖子。

标签: java spring-mvc embedded-jetty


【解决方案1】:

解决方案: 在“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

【讨论】:

  • 工作就像一个魅力!谢谢!
  • 您使用的是哪个 servlet 版本?我得到异常Caused by: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 2020-09-29
  • 1970-01-01
  • 2017-09-20
  • 2023-03-19
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多