我想展示welcome.jsp是我在项目中的初始页面,但它不会出现所以任何人都会发现错误并帮助我

时间:2016-03-04 11:36:02

标签: java jsp spring-mvc

<?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作为第一页,它显示资源未找到

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

第一个可用文件将被视为欢迎文件

答案 2 :(得分:0)

我可以看到你正在使用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中。希望这会有所帮助。