gpt4 book ai didi

java - 错误 404 : Issue accessing/WEB-INF/views/sendMessage.原因:未找到

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:23 25 4
gpt4 key购买 nike

我已经为此苦苦挣扎了几个小时。我无法让我的简单 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 时,出现以下错误。

Error 404

我从命令行运行服务器:

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

Controller 看起来像这样,我没有发现映射有任何问题:

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工作没有任何问题。另外,如果我将 Controller 更改为不映射到 View ,而是使用 @ResponseBody 注释,那么我可以在浏览器中看到结果。

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

我在这里做错了什么?

最佳答案

经过长时间的研究,我发现 this stackoverflow 帖子。

就我而言,将 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)), "/");

关于java - 错误 404 : Issue accessing/WEB-INF/views/sendMessage.原因:未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26996613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com