Welcome file not working with html file in spring

社会主义新天地 提交于 2019-12-29 07:49:06

问题


I have given my welcome file in web.xml But when running the application, it is showing 404 error on http://172.16.2.16:8080/sampletest/

It is a spring application.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>sampletest</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

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

I am using eclipse luna, java 8, tomcat 8 and maven framework. index.html file is directly under webapp folder and web.xml is under webapp/WEB-INF folder. If I use index.jsp instead of index.html, it is working. Then welcome page will load using http://172.16.2.16:8080/sampletest/

The issue is only with welcome file. Otherwise spring configuration is working. http://localhost:8080/sampletest/test/ will load the data from database.

Error log in console

......................

Jul 10, 2014 12:38:42 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4963 ms
Jul 10, 2014 12:38:42 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/sampletest/] in DispatcherServlet with name 'dispatcher'

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

Dispatcher

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:annotation-config />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <context:component-scan base-package="com.sample.test" />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="packagesToScan">
            <array>
                <value>com.sample.test.domain</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.CharSet">UTF-8</prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sampletest?autoConnect=true" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- HibernateTransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="openSessionInViewInterceptor"
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
        <property name="flushModeName">
            <value>FLUSH_AUTO</value>
        </property>
    </bean>
</beans>

Controller

package com.sample.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sample.test.dto.Response;
import com.sample.test.facade.AccelFlowFacade;

@Controller
public class SampleTestController {

    @Autowired
    @Qualifier("sampleTestFacade")
    SampleTestFacade sampleTestFacade;

    public SampleTestFacade getSampleTestFacade() {
        return sampleTestFacade;
    }

    public void setSampleTestFacade(SampleTestFacade sampleTestFacade) {
        this.sampleTestFacade= sampleTestFacade;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public @ResponseBody Response display() throws Exception {
        sampleTestFacade.disaply();
        Response res = new Response();
        return res;
    }
}

回答1:


Try adding <mvc:default-servlet-handler/> in your dispatcher-servlet.xml.

See here for details.




回答2:


You have mapped all your incoming requests to the dispatcher here,

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

So all your URL requests for the application goes inside the dispatcher as '/' maps all incoming requests . check for the stacktraces in your application server log

update:

You get the below warning because there are no handler for the '/' pattern,

WARNING: No mapping found for HTTP request with URI [/AccelFlow/] in DispatcherServlet with name 'dispatcher'

You can do either of below options ,

  1. Map a url with '/' to the controller
  2. Add a specific URL pattern to the spring dispatcher such as .htm or .do as you wish

Modify your web.xml,

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>  

And in your controller,

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public @ResponseBody Response display() throws Exception {
    accelFlowFacade.disaply();
    Response res = new Response();
    return res;
}



回答3:


At the startup by default all incoming requests are mapping to '/' pattern as you write in the web.xml:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

update:

  1. Try to map a Controller method for the default view:

    @RequestMapping(value = "/", method = GET)
    public String welcome() {
        return "index";
    }
    
  2. Add viewresolver to dispather-servlet.xml:

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/"
          p:suffix=".jsp" />
    
  3. Remove welcome file from the web.xml as automatically spring will search for index page by default:

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



回答4:


Welcome file can be access using following changes.

change 1. add resource path in dispatcher as following :

      <mvc:resources mapping="/" location="/index.html" />

change 2. add controller handler like following :

     @Controller
      public class RestController {

     @RequestMapping(value = "/", method = RequestMethod.GET)
      public String welcome() {
            return "index.html";
      }

     }

changes 3: index.html file should be in WebContent folder in project.

Note : If you are not able to add mvc bean in dispatcher servlet file then add

  xmlns:mvc="http://www.springframework.org/schema/mvc

in dispatcher servlet config file.




回答5:


using <mvc:resources mapping="/" location="/index.html" /> is goods for static pages , however changing it to

@RequestMapping(value = "/", method = RequestMethod.GET) public String welcome() { return "index.html"; }

is not good design as all model in other controllers may point back to index page, instead use

<mvc:resources mapping="/" location="/redfresh.html"  />

and make refresh page such

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="refresh" content="0;URL='/index'" />
</head>
<body>
</body>
</html>

and point in controller to index such:

@Controller
public class indexPageController {

 @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "text/html")
    public String index() {       
        return "index";
    }
}


来源:https://stackoverflow.com/questions/24670327/welcome-file-not-working-with-html-file-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!