java - HTTP 状态 404 – 未找到:源服务器未找到目标资源的当前表示
问题描述
尝试访问 Spring 框架中的服务时出现错误。
控制器类:-
package com.spring.mvc.tutorial;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("message", "Hello World from Spring 4 MVC");
return "welcome";
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("message", "Hello World Again, from Spring 4 MVC");
return "welcome";
}
}
配置类:-
package com.spring.mvc.tutorial;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring.mvc.tutorial")
public class HelloWorldConfiguration {
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
初始化类:-
package com.spring.mvc.tutorial;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class HelloWorldInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(HelloWorldConfiguration.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
看法 :-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
注意:- 请求:http://localhost:8080/SpringMvcHelloWorld/ 这是在 Eclipse Photon 中开发并部署到 Tomcat 8.5。
解决方案
推荐阅读
- aggregate - Cognos Analytics 聚合多个条件
- apache-kafka - /opt/kafka/kafka_2.12-2.6.0/bin/kafka-run-class.sh:第 318 行:/usr/lib/jvm/java-8-openjdk-amd64 /bin/java:没有这样的文件或目录
- c# - 当父进程为 64 位时,StdIN/StdOUT 管道出现问题
- python - 如何在 Altair (Python) 中调试“基准”转换和聚合?
- javascript - 通过 `await` 区分对 `then` 的调用
- mysql - Mysql Infile - 在客户端机器上工作,但不在服务器上
- javascript - 不和谐角色反应机器人
- google-apps-script - 活动工作表始终是 Google 表格的最左侧工作表
- python - 为什么 Where 子句会导致返回更多数据?
- jenkins - 如何在分配节点之前在 Jenkins 中加载属性