簡體   English   中英

我想顯示welcome.jsp是我在項目中的初始頁面,但是它沒有出現,因此任何人都可以發現錯誤並幫助我

[英]i want to show the welcome.jsp is my initial page in the project but it not coming so any one find the mistakes and help me

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>mavenproject1</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.5.RELEASE</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.2.RELEASE</version>
  </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

這是控制器類

 @Controller
public class LoginController {
@RequestMapping(value = {"/"},method = RequestMethod.GET)
public ModelAndView showFirstPage(){
    System.out.println("Hi Hello mallesh How are you..");
    ModelAndView model=new ModelAndView();
    model.setViewName("welcome");
    return model;
}
}

這是配置類

@EnableWebMvc
@Configuration
@ComponentScan({"com.mycomany.mavenproject1.*"})
public class LoginApplicationConfig {
@Bean
public InternalResourceViewResolver viewResolver(){
    InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
}

這是webApplicationInitializer類

public class WebAppApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { LoginApplicationConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
   return new String[] { "/" };  
}

}

這是我的welcome.jsp類

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Welcome To My Project...</h1>
        </body>
    </html>

每當我運行我的項目時,它都不會將welcome.page顯示為首頁,這表明找不到資源

您可以在web.xml文件中添加以下代碼。

<welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

除了先前的Sachin Aryal答案之外,您還可以添加更多歡迎頁面。

<welcome-file-list> <welcome-file>welcome.jsp</welcome-file> <welcome-file>firstpage.html</welcome-file> <welcome-file>first.jsp</welcome-file> </welcome-file-list>

第一個可用文件將作為歡迎文件

如我所見,您正在使用Javaconfig,即沒有xml方法。 我將建議您這樣做1)創建一個根配置類,即

 @Configuration
  @ComponentScan({ "com.myrootpackage.whatsoever" })
   public class RootClass{
}

2)設置您已經擁有的類,即LoginApplication配置以擴展WebMvcConfigurerAdapter,這將使您也可以加載資源或將來可能具有任何css或js的任何文件夾

Class   LoginApplication  extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/resources/");
     //your view resolver bean as you already have above goes here.
    }}

3)現在在您已經擁有的webApplicationInitializer類中,請注意以下幾點

 @Override
   protected Class<?>[] getRootConfigClasses() {
      return new Class[] { RootClass.class };
} 


@Override
  protected Class<?>[] getServletConfigClasses() {
      return class[]{LoginApplication.class];
}

最后,確保該應用程序運行時,您應該具有Locahlhost:yourport,並且沒有其他任何內容可以看到welcome.jsp頁面,並確保welcome.jsp頁面位於WEB-INF / views中。 希望這可以幫助。

暫無
暫無

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

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