Spring MVC WebApp using Spring Boot does not launc

2019-09-09 21:03发布

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.

This is my project configuration:

enter image description here

Application Class:

@Configuration
@ComponentScan
public class Application {

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

}

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

web result:

enter image description here

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?

Thanks a lot!!!

4条回答
Fickle 薄情
3楼-- · 2019-09-09 21:34

I think there is a problems in a web.xml file of your project. The mapping of servlet file is invalid where you use .html so the code for sevlet configuration for your project will be

web.xml file:

<?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>
查看更多
在下西门庆
4楼-- · 2019-09-09 21:37

Thank you very much all for you help. The solution was that I was missing 2 concepts springboot and tomcat. Because I was using SpringBoot I do not need neither the webapp folder nor .xml files (as @redflar3 suggested).

Also, I have forgotten the following dependency in the pom:

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
  <version>${spring.boot.version}</version>
</dependency>

Project:

enter image description here

Application:

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

controller:

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

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>

查看更多
Explosion°爆炸
5楼-- · 2019-09-09 21:41

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

查看更多
登录 后发表回答