使用 Spring Boot 的 Spring MVC WebApp 不启动“*.jsp";文件 [英] Spring MVC WebApp using Spring Boot does not launch "*.jsp" file

查看:41
本文介绍了使用 Spring Boot 的 Spring MVC WebApp 不启动“*.jsp";文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have started a tutorial to create a hello World web app using Spring MVC and Spring Boot. I cannot make the app to launch the hello.jsp.

这是我的项目配置:

应用类:

@Configuration
@ComponentScan
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}

HelloController 类:

HelloController class:

@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 文件:

servlet-config.xml file:

<?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 文件:

web.xml file:

<?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 文件:

hello.jps file:

<%@ 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 文件,我仍然得到相同的结果.

Is picking up the return value of sayHello method instead of the model.addAttribute("greeting","Hello World!"). Even if I delete the hello.jsp file I still get the same result.

任何想法为什么不读取此文件?错了吗?

Any ideas why is not reading this file? is it incorrect?

非常感谢!!!

推荐答案

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

You are using RestController which response return type i.e "Hello!"... so you can use @Controller rather than @RestController.

这篇关于使用 Spring Boot 的 Spring MVC WebApp 不启动“*.jsp";文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆