gpt4 book ai didi

spring-webflow - 如何从这样的 URL 启动 Spring Web Flow/resource/edit/n

转载 作者:行者123 更新时间:2023-12-01 14:30:39 24 4
gpt4 key购买 nike

我想使用 Spring Web Flow 主要是因为它的 PRG 功能。

假设我有像 /widget/edit/99/widget/edit/123 这样的 URL,其中数字代表 id的小部件。

如何启动 /widget/edit 流程,传入 id

默认情况下,流程 URL 必须与流程名称文件夹结构相匹配。

我想将 url 保留在 /widget/edit/99 而不是重定向。

(使用 2.4 版)

最佳答案

您需要尝试使用 FlowController 和 DefaultFlowUrlHandler。

FlowController - 它充当 Web Flow 定义的控制逻辑和 DispatcherServlet 的网关。

根据文档,在 DefaultFlowUrlHandler 中的 createFlowDefinitionUrl 方法中:

    The flow definition URL for the given flow id will be built by appending 
the flow id to the base app context and servlet paths.

Example - given a request originating at:
http://someHost/someApp/someServlet/nestedPath/foo

and a request for the flow id "nestedPath/bar", the new flow definition URL
would be:
http://someHost/someApp/someServlet/nestedPath/bar

因此,如果请求类似于:somehost/yourContextPath/yourServletContext/widget/edit/99 并且流 ID 是 widget/edit,则新的流定义 URL 将是:somehost/yourContextPath/yourServletContext/widget/edit

假设某些配置为:

web.xml 配置将“/widgetinventory/*”请求映射到 yourServletContext servlet:

    <servlet>
<servlet-name>yourServletContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>yourServletContext</servlet-name>
<url-pattern>/widgetinventory/*</url-pattern>
</servlet-mapping>

你的ServletContext-servlet.xml:servlet 路径与“/widgetinventory//”匹配的所有请求都映射到“flowController”bean。

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
<property name="mappings">
<value>/app/**/**=flowController</value>
</property>
</bean>

<bean name="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
<property name="flowUrlHandler" ref="customDefaultUrlhandler" />
</bean>

<bean id="customDefaultUrlhandler" class="package.CustomDefaultFlowUrlHandler"/>

<flow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/>

<flow:flow-builder-services id="flowBuilderServices" view-factory-creator="viewFactoryCreator" validator="validator"/>

<!-- Create the registry of flow definitions -->
<flow:flow-registry id="flowRegistry" base-path="/WEB-INF/flows/" flow-builder-services="flowBuilderServices">
<flow:flow-location-pattern value="**/*-flow.xml"/>
</flow:flow-registry>

请参阅映射到 customDefaultUrlhandler 的 flowUrlHandler,它是 DefaultFlowUrlHandler 的扩展.有助于更改您在其中指定的流 url 的两个方法是:getFlowId 和 createFlowDefinitionUrl。

重写 DefaultFlowUrlHandler 中的 flowId 和 createFlowDefinitionUrl 方法。

但这一切都假设您的流 url 的类型为:somehost/{yourContextPath}/{yourServletContext}/widget/edit/99,其中 widget/edit 是 flowId,99 是一些 widgetId。

    public class CustomDefaultUrlhandler extends DefaultFlowUrlHandler{

You need to alter flowId and createFlowDefinitionUrl if request is something like this:
http://host/{yourContextPath}/{yourServletContext}/widget/edit/99 to direct to widget/edit flow

@Override
public String getFlowId(HttpServletRequest request) {
String pathInfo = request.getPathInfo(); // /widget/edit/99
if (pathInfo != null) {
String widgetId = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
if(widgetId != null){
return pathInfo.substring(1,pathInfo.lastIndexOf("/")); //return widget/edit by stripping /99
}else{
return pathInfo.substring(1); //return widget/edit
}
} else {
String servletPath = request.getServletPath();
if (StringUtils.hasText(servletPath)) {
int dotIndex = servletPath.lastIndexOf('.');
if (dotIndex != -1) {
return servletPath.substring(1, dotIndex);
} else {
return servletPath.substring(1);
}
} else {
String contextPath = request.getContextPath();
if (StringUtils.hasText(contextPath)) {
return request.getContextPath().substring(1);
} else {
return null;
}
}
}
}

@Override
public String createFlowDefinitionUrl(String flowId, AttributeMap input, HttpServletRequest request) {
//now flowId = "widget/edit"
StringBuffer url = new StringBuffer();
if (request.getPathInfo() != null) {
//for /{yourContextPath}/{yourServletContext}/widget/edit/99 - pathInfo is /widget/edit/99
url.append(request.getContextPath());
url.append(request.getServletPath());
url.append('/');
url.append(flowId);
} else {
String servletPath = request.getServletPath();
if (StringUtils.hasText(servletPath)) {
url.append(request.getContextPath());
url.append('/');
url.append(flowId);
int dotIndex = servletPath.lastIndexOf('.');
if (dotIndex != -1) {
url.append(servletPath.substring(dotIndex));
}
} else {
url.append('/');
url.append(flowId);
}
}
if (input != null && !input.isEmpty()) {
url.append('?');
if (request.getPathInfo() != null) {
//append your widget id here and retrieve this in flow by requestParameters el.
String widgetId = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
url.append("widgetId ="+widgetId);
}
appendQueryParameters(url, input.asMap(), getEncodingScheme(request));
}
return url.toString();
}
}

基本上,我们根据您的 URL 自定义 flowid,并将 id 作为请求参数传递。

确保您的流程 ID 在您的场景中是 widget/edit。

在此处查看有关如何以所需格式获取流 ID 的链接:Spring Webflow - How to Get List of FLOW IDs

关于spring-webflow - 如何从这样的 URL 启动 Spring Web Flow/resource/edit/n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com