java - 使用 Spring Boot 的 Spring MVC WebApp 无法启动 "*.jsp"文件

标签 java spring spring-mvc web-applications spring-boot

我已经开始使用 Spring MVC 和 Spring Boot 创建 hello World Web 应用程序的教程。我无法让应用程序启动 hello.jsp。

这是我的项目配置:

enter image description here

应用类:

@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>

网络结果:

enter image description here

正在获取 sayHello 方法的返回值,而不是 model.addAttribute("greeting","Hello World!")。即使我删除 hello.jsp 文件,我仍然得到相同的结果。

有什么想法为什么不读这个文件吗?是不是不正确?

非常感谢!!!

最佳答案

您正在使用 RestController ,其响应返回类型即“Hello!”...因此您可以使用 @Controller 而不是 @RestController

关于java - 使用 Spring Boot 的 Spring MVC WebApp 无法启动 "*.jsp"文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37817173/

相关文章:

java - Spring JPA 中的 @OneToOne 映射是什么?

Java:从给定的全名字符串中删除逗号和姓氏

java - War 在本地独立 tomcat 7(无 eclipse)上运行良好,上传到 ec2 时不起作用

java - 如何使用spring mvc在浏览器中下载文件?

java - Spring 中的异步事件需要大量时间来执行(轮到它了)

java - 在多个字段上使用 groupingBy 条件后获取计数

Java Spring MVC + Jetty Websocket 不兼容的方法签名

java - 从具有包和名称的实例中获取依赖实例

java - 注入(inject) MockMVC 测试

java - 你应该在 java 枚举中包含未知/默认值吗