【问题标题】:Spring MVC WebApp using Spring Boot does not launch "*.jsp" file使用 Spring Boot 的 Spring MVC WebApp 不会启动“*.jsp”文件
【发布时间】:2016-10-15 11:46:30
【问题描述】:

我已经开始使用 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 文件,我仍然得到相同的结果。

任何想法为什么不读取这个文件?是不是错了?

非常感谢!!!

【问题讨论】:

  • 如果你真的使用spring boot,你就不需要spring-servlet.xml和web.xml..
  • 我也尝试了以下但仍然是同样的问题: contextConfigLocation/WEB-INF/config/servlet-config.xml org.springframework.web.context.ContextLoaderListener

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


【解决方案1】:

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

【讨论】:

  • 我已经尝试过了,但在加载时出现以下错误:localhost:8080/greeting Whitelabel 错误页面 此应用程序没有明确的 /error 映射,因此您将此视为后备。 2016 年 6 月 15 日星期三 08:56:31 BST 出现意外错误(类型=未找到,状态=404)。没有可用的消息
  • 你回来了 HELLO!在视图(jsp 页面)中有 hello.jsp 所以我认为有效的返回类型将是“hello”
  • 您能详细说明一下吗? @ShreeKrishna
  • ok.. 因为您正在使用内部视图解析器,并且在配置中为 jsp 页面指定 /WEB-INF/JSP 文件夹,所以当您从 sayHello 方法返回字符串时,它会查找指定 jsp /WEB-INF/JSP 文件夹中的页面,即您返回 HELLO!所以它会在没有 HELLO 的地方寻找 HELLO!.jsp 页面! /WEB-INF/JSP 文件夹中的 jsp 页面,而不是 hello.jsp 页面.. 所以我告诉你在 sayHello 方法中返回“hello”....并考虑 @controller 注释...
  • 感谢@ShreeKrishna。我试过返回 --> return "hello";我有相同的结果(你好,而不是你好!在网络上)。如果我使用 Controller 而不是 RestController 我会收到 Whitelabel Error Page 错误。我认为该应用程序没有同时查看任何 .xml(web.xml 和 servlet-config.xml)。我是否需要指定 Web 配置文件的位置? (类路径或类似的配置位置?)如果是这样,请协助如何做。非常感谢!
【解决方案2】:

我认为您的项目的 web.xml 文件存在问题。 servlet 文件的映射在您使用 .html 的地方无效,因此您项目的 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>

【讨论】:

  • 感谢@ShreeKrishna 的快速回复。我已经尝试过该解决方案以及使用 /*html* 等。没有任何区别
【解决方案3】:

【讨论】:

    【解决方案4】:

    非常感谢大家的帮助。解决方案是我缺少 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);
        }
    }

    控制器:

    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";
        }
    }

    你好.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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2017-11-12
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多