【问题标题】:Error 404: Issue accessing /WEB-INF/views/sendMessage.jsp. Reason: Not Found错误 404:访问 /WEB-INF/views/sendMessage.jsp 时出现问题。原因:未找到
【发布时间】:2014-11-19 10:42:45
【问题描述】:

我已经为此苦苦挣扎了好几个小时。我无法让我的简单 Jetty + Spring MVC + JSP Web 应用程序工作。

这是我项目的目录结构:

spring-form-utf-8-test
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   |-- com
|       |   |   `-- example
|       |   |       |-- beans
|       |   |       |   `-- forms
|       |   |       |       `-- MessageForm.java
|       |   |       |-- config
|       |   |       |   `-- WebMvcConfig.java
|       |   |       `-- controller
|       |   |           `-- FormController.java
|       |   `-- Main.java
|       |-- resources
|       `-- webapp
|           |-- images
|           |   `-- kitty.jpg
|           `-- WEB-INF
|               `-- views
|                   `-- sendMessage.jsp
`-- target

问题是当我尝试访问http://localhost:8080 时出现以下错误。

我从命令行运行服务器:

java -jar target\spring-form-utf-8-test-1.0-SNAPSHOT.jar

控制器看起来像这样,我看不出映射有什么问题:

package com.example.controller;

import com.example.beans.forms.MessageForm;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


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

    @ModelAttribute("message")
    public MessageForm createMessageForm() {
        return new MessageForm();
    }

    @RequestMapping(method = RequestMethod.GET)
    public String sendForm() {
        return "sendMessage";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processForm(@ModelAttribute("message") @Valid final MessageForm message,
        final BindingResult result,
        final Model model) {
        return "sendMessage";
    }
}

带注释的 Web 应用程序上下文如下所示

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


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

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

    @Bean
    public InternalResourceViewResolver setupViewResolver() {
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

运行 Jetty 服务器实例的主类如下所示。

import java.io.IOException;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.servlet.DispatcherServlet;

public class Main {
    public static void main(String[] args) {
        final Server server = new Server(8080);
        final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler.setErrorHandler(null);
        servletContextHandler.setContextPath("/");

        final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.example.config");
        context.getEnvironment().setDefaultProfiles("dev");

        servletContextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
        servletContextHandler.addEventListener(new ContextLoaderListener(context));


        try {
        servletContextHandler.setResourceBase(new ClassPathResource("webapp").getURI().toString());
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }

        server.setHandler(servletContextHandler);
        try {
            server.start();
            server.join();
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}

访问静态资源http://localhost:8080/images/kitty.jpg 没有任何问题。此外,如果我将控制器更改为不映射到视图,而是使用 @ResponseBody 注释,那么我可以在浏览器中看到结果。

我尝试添加一个org.apache.jasper.servlet.JspServlet 实例,就像它在this blog 上呈现的那样,但这并没有带来任何结果。错误信息是一样的。

我在这里做错了什么?

【问题讨论】:

    标签: java spring jsp spring-mvc jetty


    【解决方案1】:

    经过长时间的研究,我找到了thisstackoverflow 帖子。

    在我的情况下,将 import org.eclipse.jetty.servlet.ServletContextHandler 更改为 org.eclipse.jetty.webapp.WebAppContext 就像一个魅力。

    最后Main.java 文件如下所示。

    import java.io.IOException;
    
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.servlet.ServletHolder;
    import org.eclipse.jetty.webapp.WebAppContext;
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.servlet.DispatcherServlet;
    
    public class Main {
        public static void main(String[] args) {
            final Server server = new Server(8080);
            final WebAppContext webAppContext = new WebAppContext();
            webAppContext.setErrorHandler(null);
            webAppContext.setContextPath("/");
    
            final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.setConfigLocation("com.example.config");
            context.getEnvironment().setDefaultProfiles("dev");
    
            webAppContext.addServlet(new ServletHolder(new DispatcherServlet(context)), "/");
            webAppContext.addEventListener(new ContextLoaderListener(context));
    
    
            try {
                webAppContext.setResourceBase(new ClassPathResource("webapp").getURI().toString());
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
    
            server.setHandler(webAppContext);
            try {
                server.start();
                server.join();
            } catch (final InterruptedException e) {
                throw new RuntimeException(e);
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    

    在这行代码中将/* 更改为/ 也很重要:

    webAppContext.addServlet(new ServletHolder(new DispatcherServlet(context)), "/");
    

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 2018-10-21
      • 1970-01-01
      • 2018-01-24
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多