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/ 这是在EclipsePhoton中开发的,并部署到Tomcat8.5中
# 1 楼答案
我还不能评论,因此这是一个答案的形式
当tomcat没有初始化web应用程序时,我也遇到了类似的问题。你可以添加为系统。出来在初始化器上打印LN以查看它是否首先初始化webapp
并在此处共享您的服务器日志
# 2 楼答案
我也遇到了同样的问题,通过检查@controller和@RequestMapping等属性绑定到mvc包的位置,解决了这个问题
要检查源是否已绑定:
转到控制器页面,同时按下@controller上的control和left按钮
如果源未绑定,则它将显示如图所示。
如果源未绑定,则通过单击“附加源”按钮并指向spring-webmvc-5.1.3来附加源。发布源代码
按照上述两个步骤检查其他罐子
# 3 楼答案
尝试重定向到控制器类中的html主页面:
更换总管。带有主页路径的html