不正确构建Spring MVC项目

时间:2016-11-07 11:18:00

标签: java spring maven tomcat

我有一个小型的MVC项目,可以通过Tomcat在IDEA中构建,也可以在Git中使用webapp-runner构建。它工作得很完美,但突然间,当我构建我的项目时,它在localhost:8080中打开,并看到我的js和css文件无法正常工作的问题。我在调试器(F12)中打开它,注意到我的jsp页面的HTML代码存在于所有文件中。如果需要在评论中写下额外的信息,我会发送它。

你可以在这张照片上看到它: enter image description here

这是我项目的结构:

enter image description here

其他:

servlet的context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.ya.pokupay" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources
        in the /WEB-INF/views directory -->
    <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!--&lt;!&ndash;Local database&ndash;&gt;-->
    <!--<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"-->
                <!--destroy-method="close">-->
        <!--<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />-->
        <!--<beans:property name="url" value="jdbc:mysql://localhost:3306/shopDB" />-->
        <!--<beans:property name="username" value="*****" />-->
        <!--<beans:property name="password" value="*****" />-->
    <!--</beans:bean>-->

    <!--Remote database-->
    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://db4free.net:3306/yapokupay" />
        <beans:property name="username" value="******" />
        <beans:property name="password" value="******" />
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.ya.pokupay.model.Advert</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="advertDAO" class="com.ya.pokupay.dao.AdvertDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="advertService" class="com.ya.pokupay.service.AdvertServiceImpl">
        <beans:property name="advertDAO" ref="advertDAO"/>
    </beans:bean>
    <context:component-scan base-package="com.ya.pokupay" />

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

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>


</beans:beans>

的script.js:

$( document ).ready(function() {

    $(".dropdown-menu a").on("click", function () {
        var url = $(this).attr("href");
        if (url == '#') {
            history.replaceState('', '', '/');
        } else {
            history.pushState('', '', url);
        }

    })

});

1 个答案:

答案 0 :(得分:1)

非常感谢@Lucky的帮助。我想我找到了解决问题的方法。我在我的Controller @RequestMapping(&#34; / * *&#34;)中写道,因此您可以在url中编写任何内容并获取相同的jsp文件。当app尝试通过路径资源/ **获取js和css文件时,它获得了相同的jsp页面。我刚刚删除**并且它有效。