【问题标题】:Deploying Web App With Spring MVC使用 Spring MVC 部署 Web 应用程序
【发布时间】:2012-09-17 20:29:19
【问题描述】:

我以前从未遇到过这个问题。实际上,我有一个通过war文件运行的Web应用程序。我已经将 Spring 配置为使用它并且它运行良好。

问题是我正在尝试通过 Spring 配置 Activiti。基本上,WEB-INF/processes 文件夹中有一组 .bpmn20.xml 文件。 Activiti 团队提到他们不知道如何在 Web 应用程序中进行配置。作为一个独立的应用程序,我可以自动部署资源 .bpmn20.xml 文件,因为进程文件夹位于类路径上。我在配置 Web 应用程序结构时遇到问题。

请看下面:

我创建了一个 Spring MVC 应用程序并通过运行 DbSchemaCreate.main() 创建了一个 Activiti 数据库。实际上,我的流程似乎没有部署在战争文件上。当 Tomcat 启动时,ProcessEngine 通过 Spring 启动并工作。我可以访问 RuntimeService。代码如下:

web.xml

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

   <display-name>WebApp</display-name>

   <context-param>
      <!-- Specifies the list of Spring Configuration files in comma separated format.-->
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/activiti.xml
      </param-value>
   </context-param>

   <listener>
      <!-- Loads your Configuration Files-->
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

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

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

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

activiti.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   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-3.0.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
     <property name="dataSource" ref="dataSource"/>
     <property name="databaseSchemaUpdate" value="true"/>
     <property name="jobExecutorActivate" value="false"/>
     <property name="transactionManager" ref="transactionManager"></property>
     <!-- <propety name="beans">
        <map>
           <entry key="printer" value-ref="printer"/>
        </map>
        </property>-->
     <property name="deploymentResources" value="classpath*:/processes.*.bpmn20.xml"/>
  </bean>

  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
     <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
  </bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
     <property name="driverClass" value="com.mysql.jdbc.Driver"/>
     <property name="url" value="jdbc:mysql://localhost:3306/activiti_example"/>
     <property name="username" value="root"/>
     <property name="password" value="password"/>
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>

未部署自动资源部署。我也尝试过通过代码进行部署,但它抛出了一个异常:

 repositoryService.createDeployment().addClasspathResource("ProcessExample.bpmn20.xml").deploy();
 runtimeService.startProcessInstanceByKey("processExample", mapOfProcessVariables);

org.springframework.web.util.NestedServletException: Request processing failed; nested exception  is org.activiti.engine.ActivitiException: resource 'ProcessExample.bpmn20.xml' not found
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

  org.activiti.engine.ActivitiException: resource 'ProcessExample.bpmn20.xml' not found
  org.activiti.engine.impl.repository.DeploymentBuilderImpl.addClasspathResource   (DeploymentBuilderImpl.java:59)
  com.webchannel.web.EmailController.sendE(EController.java:46)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  java.lang.reflect.Method.invoke(Method.java:597)
  org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
  org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
  org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

如何在 webapp 中找到该资源?

我已尝试将其放入 WEB-INF/processes/ProcessExample.bpmn20.xml。

我也试过了:

<property name="deploymentResources" value="/WEB-INF/processes.*.bpmn20.xml"/>

编辑

这个网站可能会有所帮助,但我被困住了。

Is WEB-INF in the CLASSPATH?

【问题讨论】:

    标签: java web-applications spring-mvc war activiti


    【解决方案1】:

    试试这个:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
    	<!-- for specifies the Web application display name  --> 
    	<display-name>APMC</display-name>
    	
       <!-- For Authentication processing mechanisms -->
       <filter>  
            <filter-name>springSecurityFilterChain</filter-name>  
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
        </filter>  
        <filter-mapping>  
            <filter-name>springSecurityFilterChain</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>  
      
      	<!-- For mapping request resource and combine its results with the matching JSP -->
    	<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring-security.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      
     <servlet>
     	<servlet-name>rest</servlet-name>
     	<servlet-class>
      		org.springframework.web.servlet.DispatcherServlet
     	</servlet-class>
     	<load-on-startup>2</load-on-startup>
     </servlet>
    
     <servlet-mapping>
     	<servlet-name>rest</servlet-name>
     	<url-pattern>/rest/*</url-pattern>
     </servlet-mapping>
      
     <!-- ContextLoaderListener  provides access to the ServletContext  -->
     <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring-config.xml, 
          			 /WEB-INF/spring-security.xml
       </param-value>
     </context-param>
      <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
    </web-app>
    上面的文件是 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:task="http://www.springframework.org/schema/task"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"	
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    					    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    					    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
    					    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    					    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
      
      	<context:component-scan base-package="com.apmc.dao" />
      	
    	<context:component-scan base-package="com.apmc.services" />
    	<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
        <task:executor id="taskExecutor" pool-size="1"  /> 
        <task:scheduler id="taskScheduler" pool-size="1"  />
        
    	<context:component-scan base-package="com.apmc.controller" />
    	<context:component-scan base-package="com.apmc.rest" />
      	<context:property-placeholder location="classpath:database.properties" />
      	<context:property-placeholder location="classpath:log4j.properties" />
    
    	<mvc:resources mapping="/resources/**" location="/resources/mytheme/" />
    	<mvc:annotation-driven />
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${database.driver}" />
    		<property name="url" value="${database.url}" />
    		<property name="username" value="${database.user}" />
    		<property name="password" value="${database.password}" />
    	</bean>
    	
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">  
                <list>  
                    <value>com.apmc.domain.Vehicle</value>
                </list>  
            </property> 
            
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    				<prop key="hibernate.format_sql">false</prop>
    				<prop key="hibernate.use_sql_comments">false</prop>
    			</props>
    		</property>
    		
    	</bean>
     
    	<bean id="txManager"
    		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	
    	<bean id="persistenceExceptionTranslationPostProcessor"
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
     		
    	<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>
    	
    	<bean id="messageSource"
    		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    
    		<property name="basename" value="/WEB-INF/messages" />
    
    	</bean>
    	
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">  
    		<property name="sessionFactory" ref="sessionFactory" />
        </bean> 
        
    </beans>

    以上代码为spring-config.xml

    在src文件夹的resources文件夹中添加database.properties文件。所以在database.properties中写代码

    database.driver=com.mysql.jdbc.Driver
    database.url=jdbc:mysql://localhost:3306/apmc_db
    database.user=root
    database.password=
    hibernate.show_sql=true      

    【讨论】:

      【解决方案2】:

      也许尝试将定义进程的文件夹(您的 bpmn20.xml 文件)添加到 构建路径

      错误告诉你,Activiti-Engine 找不到你的文件,这就是为什么你需要告诉它在哪里找到它。

      【讨论】:

        猜你喜欢
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 2011-02-11
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多