我有一个小型 MVC 项目,可以通过 Tomcat 或 Git 中的 webapp-runner 在 IDEA 中构建。它工作得很好,但是当我构建我的项目时,它突然在 localhost:8080 中打开并看到我的 js 和 css 文件不起作用的问题。我在调试器 (F12) 中打开它并注意到在所有文件中都有我的 jsp 页面的 HTML 代码。如果需要更多信息,请写在评论中,我会发送。
这是我的项目结构:
附加:
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>
<!--<!–Local 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://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>
脚本.js:
$( document ).ready(function() {
$(".dropdown-menu a").on("click", function () {
var url = $(this).attr("href");
if (url == '#') {
history.replaceState('', '', '/');
} else {
history.pushState('', '', url);
}
})
});
最佳答案
非常感谢@Lucky 的帮助。我想我找到了问题的答案。我在我的 Controller 中写了 @RequestMapping("/* *") 所以你可以在 url 中写任何东西并获得相同的 jsp 文件。当应用程序尝试通过路径 resources/** 获取 js 和 css 文件时,它得到了相同的 jsp 页面。我刚刚删除了 ** 并且它有效。
关于java - 未正确构建 Spring MVC 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463887/