我已经开始使用Spring MVC和Spring Boot创建一个hello World Web应用程序的教程。我无法让应用程序启动hello.jsp。
这是我的项目配置:
申请类:
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HelloController类:
@RestController
public class HelloController {
@RequestMapping(value="/greeting",method= RequestMethod.GET)
public String sayHello (Model model){
model.addAttribute("greeting","Hello World!");
return "HELLO!";
}
}
servlet-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="trex.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- ... -->
</beans>
web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Add Copyright notice here.
-->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>SpringWebMVCApp</display-name>
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
hello.jps文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
网络结果:
获取sayHello方法的返回值而不是model.addAttribute(“greeting”,“Hello World!”)。即使我删除了hello.jsp文件,我仍然得到相同的结果。
任何想法为什么不读这个文件?是不正确的?
非常感谢!!!
答案 0 :(得分:3)
您正在使用RestController
响应返回类型,即“Hello!”...因此您可以使用@Controller
而不是@RestController
。
答案 1 :(得分:0)
我认为项目的web.xml文件存在问题。使用.html时servlet文件的映射无效,因此项目的sevlet配置代码将是
web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>SpringWebMVCApp</display-name>
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
答案 2 :(得分:0)
例如............................................
答案 3 :(得分:0)
非常感谢你的帮助。解决方案是我缺少2个概念springboot和tomcat。因为我使用的是SpringBoot,所以我既不需要webapp文件夹也不需要.xml文件(如@ redflar3建议的那样)。
另外,我忘记了pom中的以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring.boot.version}</version>
</dependency>
项目:
应用:
package trex.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
&#13;
控制器:
package trex.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/greeting")
public String sayHello (Model model){
model.addAttribute("greeting","Hello World!");
return "hello";
}
}
&#13;
hello.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>
<p th:text="${greeting}" />
</body>
</html>
&#13;