欢迎文件在春季无法与html文件一起使用 [英] Welcome file not working with html file in spring

查看:84
本文介绍了欢迎文件在春季无法与html文件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.xml中给出了欢迎文件 但是在运行应用程序时,它在http://172.16.2.16:8080/sampletest/

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/

这是一个Spring应用程序.

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>

我正在使用Eclipse Luna,Java 8,tomcat 8和Maven框架. index.html文件直接位于webapp文件夹下,而web.xml位于webapp/WEB-INF文件夹下. 如果我使用index.jsp而不是index.html,则它可以正常工作.然后,欢迎页面将使用http://172.16.2.16:8080/sampletest/

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/

此问题仅与欢迎文件有关.否则,弹簧配置将起作用. http://localhost:8080/sampletest/test/将从数据库中加载数据.

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

控制台错误登录

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

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

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>

调度程序

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

控制器

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

推荐答案

尝试在您的dispatcher-servlet.xml中添加<mvc:default-servlet-handler/>.

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

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆