簡體   English   中英

使用Spring MVC部署Web App

[英]Deploying Web App With Spring MVC

我從來沒有遇到過這個問題。 實際上,我有一個通過war文件運行的Web應用程序。 我已經配置了Spring來使用它,並且它可以完美地工作。

問題是我試圖通過Spring配置Activiti。 基本上,WEB-INF / processes文件夾中有一組.bpmn20.xml文件。 Activiti團隊提到他們不知道如何在Web應用程序中進行配置。 作為一個獨立的應用程序,我可以自動部署資源.bpmn20.xml文件,因為processs文件夾位於類路徑上。 我在網絡應用程序結構中配置時遇到麻煩。

請看下面:

我通過運行DbSchemaCreate.main()創建了Spring MVC應用程序並創建了Activiti數據庫。 實際上,我的流程似乎沒有部署在war文件上。 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"/>

編輯

該網站可能會有所幫助,但我被困住了。

WEB-INF是否在CLASSPATH中?

也許嘗試將定義了過程的文件夾(您的bpmn20.xml文件)添加到構建路徑中

該錯誤告訴您,Activiti-Engine無法找到您的文件,這就是為什么您需要告訴它在哪里可以找到它的原因。

嘗試這個 :

 <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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM