简体   繁体   中英

unable to view welcome page in spring mvc

Here is my 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>

Here is my web.xml file:

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

Here is my dispatcher-servlet.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: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>

This is my controller:

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

}

This is my welcome.jsp file which is available in /WEB-INF/jsp/ folder:

 <!DOCTYPE html>
  <html>
  <head>

  </head>
    <body>

     <h2>Login Form</h2>



      </body>
   </html>

When I am running this application in tomcat server using eclipse, I am not able to find welcome.jsp page. I can see in console print message and log message which is present in my controller. That means request is passing through welcome() method of RegistrationLoginController. my problem is that I am not able to get welcome.jsp page. Please help me to find the issue.

Thank you.

Try:

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

To get the path, right click on the file "welcome.jsp" from your IDE and choose Copy Path ( Ctrl+Shift+C for IntelliJ).

Then paste it to your code like the example above.

your controller method should look something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM