[英]Spring MVC with Tomcat won't open Hello World html page
I'm trying to open a simple html page that writes Hello world, on localhost:8080/hello but it is inside a Spring MVC app. 我正在尝试在localhost:8080 / hello上打开一个写Hello World的简单html页面,但它在Spring MVC应用程序内部。 I use Spring Boot that comes with its own tomcat.
我使用带有自己的tomcat的Spring Boot。
web.xml web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Angular Rest Spring</display-name>
<servlet>
<servlet-name>angular</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>angular</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
I tried putting the page inside: 我尝试将页面放入其中:
app/src/main/resources
app/src/main/resources/templates
web
web/WEB-INF
It can't find it. 它找不到。 It says:
它说:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Sep 07 15:52:20 EEST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
Any ideas? 有任何想法吗?
If you are using spring boot there is no need for writing a web.xml. 如果您使用的是Spring Boot,则无需编写web.xml。 Spring automatically maps all @Controller annotated classes and reads the @RequestMapping annotations from those.
Spring自动映射所有@Controller注释的类,并从中读取@RequestMapping注释。
Write an application class : 编写一个应用程序类:
package com.sample.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.sample.web")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Write a controller class like like this: 像这样编写一个控制器类:
package com.sample.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
return "hello";
}
}
And write a template html file in src/main/resources/templates : 并在src / main / resources / templates中写一个模板html文件:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
</body>
</html>
I use intellij in which I can run the application class which will then make the hello page available on http://localhost:8080/hello 我使用的是intellij,可以在其中运行应用程序类,该应用程序类随后将在http:// localhost:8080 / hello上提供问候页面
As far as i'm concerned, web pages in a spring boot application (using maven) go into: 就我而言,Spring Boot应用程序中的网页(使用Maven)进入:
src/main/resources/META-INF/resources/ src / main / resources / META-INF / resources /
This folder is basically the equivalent to src/main/webapp in a standard maven web app. 此文件夹基本上等同于标准maven Web应用程序中的src / main / webapp 。
使用spring工具套件 ,创建spring legacy项目或spring starter项目。它将配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.