新的activi 中未能找到老版简介中提供的ActivitiRestApplication,所以就直接由springmvc为Diagram Viewer提供服务,也好大家理解一下Diagram Viewer怎么运行的。
1、在官方提供的Zip文件(可以从www.activiti.org/download.html下载)中有一个activiti-explorer.war文件,解压文件,复制其中的diagram-viewer复制到项目中。
复制modules\activiti-diagram-rest下面的java源文件到项目源文件中,如放到com.ffcs.zzy.workflow.control包下面
BaseProcessDefinitionDiagramLayoutResource.java
ProcessDefinitionDiagramLayoutResource.java
ProcessInstanceDiagramLayoutResource.java
ProcessInstanceHighlightsResource.java
2、修改上述后面3个源文件的RequestMapping,在原来的路径前面加/service,总共有3个地方,原来是
@RequestMapping(value="/process-definition/{processDefinitionId}/diagram-layout", method = RequestMethod.GET, produces = "application/json")
修改成
@RequestMapping(value="/service/process-definition/{processDefinitionId}/diagram-layout", method = RequestMethod.GET, produces = "application/json")
3、调整spring-mvc.xml,原来是
修改成
expression="org.springframework.stereotype.Controller"/>
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
4、添加spring-mvc-modeler.xml,
5、添加JsonpAdvice,其中com.ffcs.zzy.workflow.control就是上面源文件的所在的包
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
@ControllerAdvice(basePackages = "com.ffcs.zzy.workflow.control")
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
private final String[] jsonpQueryParamNames;
public JsonpAdvice() {
super("callback", "jsonp");
this.jsonpQueryParamNames = new String[]{"callback"};
}
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
//如果不存在callback这个请求参数,直接返回,不需要处理为jsonp
if ((servletRequest.getParameter("callback")==null)) {
return;
}
//按设定的请求参数(JsonAdvice构造方法中的this.jsonpQueryParamNames = new String[]{"callback"};),处理返回结果为jsonp格式
for (String name : this.jsonpQueryParamNames) {
String value = servletRequest.getParameter(name);
if (value != null) {
MediaType contentTypeToUse = getContentType(contentType, request, response);
response.getHeaders().setContentType(contentTypeToUse);
bodyContainer.setJsonpFunction(value);
return;
}
}
}
}
6、前台调用的地址
http://localhost/zzy-webapp/activiti/diagram-viewer/index.html?processDefinitionId=metaVersionProcess:1:12504&processInstanceId=212501
其中processDefinitionId是流程定义的ID,processInstanceId为流程实例的ID
就可以显示了。。