application.properties

frebpwbc  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(280)

application.properties无法获取。很小的程序不知道是怎么回事,我已经谷歌和做了研究。
但是当我在firstcontroller.java中使用这段代码时,它确实起作用了。。。。

return new ModelAndView("/WEB-INF/jsp/welcome.jsp","dateAndTime",dateAndTime);

firstcontroller.java文件

package com.vpp.fleetman.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
public class FirstController {

  @RequestMapping("/home.html")
    public ModelAndView firstPage(){
      Date dateAndTime = new Date();

     //This work, I just want to remove the hard coded and use application.properties
     // return new ModelAndView("/WEB-INF/jsp/welcome.jsp","dateAndTime",dateAndTime);

    //Let's use a view resolver for the views
    return new ModelAndView("welcome","dateAndTime",dateAndTime);
  }
}

webapp/web inf/jsp/welcome.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h1>Welcome from Intelij ..... Time is: ${dateAndTime} </h1>

<c:forEach var="i" begin="1" end="5">
    <p>${i}</p>
</c:forEach>

application.properties使用facet设置的默认值

sping.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.virtualpairprogrammers</groupId>
    <artifactId>fleetman</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fleetman</name>
    <description>VPP Fleet Management Application</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

当我http://localhost:8080/home.html

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 27 13:57:24 EDT 2021
There was an unexpected error (type=Not Found, status=404).
/welcome.jsp

一旦我使用application.properties的引用,它就不起作用了。。。当我保留硬编码的ref??任何指导都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题