Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向Spring/JSP-WAR添加webpackjs包_Java_Spring_Maven_Jsp_Webpack - Fatal编程技术网

Java 向Spring/JSP-WAR添加webpackjs包

Java 向Spring/JSP-WAR添加webpackjs包,java,spring,maven,jsp,webpack,Java,Spring,Maven,Jsp,Webpack,我最近看到了,尽管我想在一个遗留项目(JSP)中尝试一下,以简化部署 我取出了所有的.js代码,并将其转化为一个webpack项目,当使用npm run dist调用时,它会创建frontend_bundle.js 然后我按照[使用说明]()添加了一个pom: web.xml: <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

我最近看到了,尽管我想在一个遗留项目(JSP)中尝试一下,以简化部署

我取出了所有的.js代码,并将其转化为一个webpack项目,当使用npm run dist调用时,它会创建frontend_bundle.js

然后我按照[使用说明]()添加了一个pom:

web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <display-name>Counter Web Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

计数器Web应用程序
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/
上下文配置位置
/WEB-INF/mvc-dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
背景:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        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">

    <context:component-scan base-package="com.example.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

/WEB-INF/pages/
.jsp
现在,当我部署到maven时,我得到一个404登录控制台,用于:

得到


您需要将webjar内容显式映射到uri,如前所述

编辑:已在提供的源中

是的,你是对的,我没有看到。 其他可能的问题可能是:

如果映射了任何其他WebJAR,“如果”将禁止注册:

 if (!registry.hasMappingForPattern("/webjars/**")) 
在if处设置一个断点,然后看看会发生什么

您使用的url与资源路径不匹配: 您正在映射到

classpath:/META-INF/resources/webjars/
url不应包含“webjars”,可能是:

GET http://localhost:8080/frontend/1.5.1/frontend_bundle.js
你当然知道,但也许值得仔细检查一下这场战争,看看资源是否在你期望的地方……

我想我解决了它

更新web xml以使用ee版本3后:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">

并链接到上下文中的脚本:

<script src="${pageContext.request.contextPath}/webjars/counter-webapp-frontend/0.0.1/vendor_bundle.js"></script>
<script src="${pageContext.request.contextPath}/webjars/counter-webapp-frontend/0.0.1/app_bundle.js"></script>


JS脚本正在加载。

谢谢您的回答@Stefan Isele-但是(无注释)这不正是我在addResourceHandlers下的答案吗?我添加了注释,但这还不能修复它。
 if (!registry.hasMappingForPattern("/webjars/**")) 
classpath:/META-INF/resources/webjars/
GET http://localhost:8080/frontend/1.5.1/frontend_bundle.js
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
<script src="${pageContext.request.contextPath}/webjars/counter-webapp-frontend/0.0.1/vendor_bundle.js"></script>
<script src="${pageContext.request.contextPath}/webjars/counter-webapp-frontend/0.0.1/app_bundle.js"></script>