簡體   English   中英

無法在Spring MVC中查看歡迎頁面

[英]unable to view welcome page in spring mvc

這是我的pom.xml

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gopal.springmvc</groupId>
<artifactId>springmvctest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvctest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.4</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
</dependencies>
<build>
    <finalName>springmvctest</finalName>
</build>
</project>

這是我的web.xml文件:

 <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application    
  2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
  <web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
 </servlet-mapping>
 </web-app>

這是我的dispatcher-servlet.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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config />
   <context:component-scan base-package="com.gopal.springmvc"/>
   <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
  </bean>
 </beans>

這是我的控制器:

  @Controller
  public class RegistrationLoginController {

  private static final Logger logger =    
  Logger.getLogger(RegistrationLoginController.class);

@RequestMapping(value="/", method=RequestMethod.GET)
public String welcome() {
    if(logger.isDebugEnabled()){
        logger.debug("getWelcome is executed!");
    }
    System.out.println("Helloooooooo<<<<<");
    //@RequestParam
    return "welcome";
}

@RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView loginForm() {
    //@RequestParam
    return null;
}

}

這是我的welcome.jsp文件,該文件位於/ WEB-INF / jsp /文件夾中:

 <!DOCTYPE html>
  <html>
  <head>

  </head>
    <body>

     <h2>Login Form</h2>



      </body>
   </html>

當我使用eclipse在Tomcat服務器中運行此應用程序時,無法找到welcome.jsp頁面。 我可以在控制器中看到控制台打印消息和日志消息。 這意味着請求正在通過RegistrationLoginController的welcome()方法傳遞。 我的問題是我無法獲得welcome.jsp頁面。 請幫助我找到問題。

謝謝。

嘗試:

return <path_of_jsp> + "/welcome.jsp";

要獲取路徑,請在您的IDE中右鍵單擊文件“ welcome.jsp”,然后選擇“復制路徑”(對於IntelliJ,則為Ctrl + Shift + C )。

然后像上面的示例一樣將其粘貼到您的代碼中。

您的控制器方法應如下所示:

@GetMapping("/")
public String welcome() {

  return "C:/WHATEVER_YOUR_PATH_IS/welcome.jsp";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM