简体   繁体   English

Spring MVC使用单个控制器管理多个视图

[英]Spring MVC managing multiple views with single controller

Looking for an effective way to implement order management module. 寻找实现订单管理模块的有效方法。 There are different order types (approximately about 15). 有不同的订单类型(大约15种)。 Each order has a seperate view. 每个订单都有单独的视图。 But the actions performed on UI are same irrespective of order type. 但是,无论订单类型如何,在UI上执行的操作都是相同的。 Below is the structure of my DTO 以下是我的DTO的结构

abstract class Order

abstract class SecurityOrder extends Order

abstract class TermDepositOrder extends Order
.....
.....
.....

I am trying to implement a single controller capable of managing all views. 我正在尝试实现一个能够管理所有视图的控制器。 Something similar to the one below: 与以下内容类似:

@Controller
public class OrderController<F extends Order> {

    public F validate(F order) {
    }

    public F insert(F order) {
    }
}

I am not sure how spring mvc would be able to map request parameters properly to the order instance as it doesn't know which order instance to instantiate. 我不确定spring mvc如何将请求参数正确映射到订单实例,因为它不知道要实例化哪个订单实例。

Is it possible to achieve this with single controller or should I go with a controller for each order type and duplicate same code across all of them? 是否可以使用单个控制器来实现此目的,还是应该为每种订单类型使用一个控制器,并在所有订单中重复相同的代码?

Was able to do it by providing a custom Method Processor. 通过提供自定义方法处理器能够做到这一点。 Below is the sample code. 下面是示例代码。

Custom Implementation: 定制实施:

public class OrderControllerArgumentResolver extends 
            ModelAttributeMethodProcessor { 

    public OrderControllerArgumentResolver() { 
            super(true); 
    } 

    @Override 
    public boolean supportsParameter(MethodParameter paramMethodParameter) { 
            //Resolve - custom annotation created to differentiate order parameter from others. 
            //This is to make sure this resolver is used only while resolving OrderDTO is used as method parameter 
            if (paramMethodParameter.getParameterAnnotation(Resolve.class) != null) { 
                    return true; 
            } 
            return false; 
    } 

    @Override 
    protected final Object createAttribute(String attributeName, 
                    MethodParameter parameter, WebDataBinderFactory binderFactory, 
                    NativeWebRequest request) throws Exception { 
            //Instantiate appropriate order instance based on input. 
            return BeanUtils.instantiateClass(OrderTypeEnum.getDTOClass(request 
                            .getParameter("orderType"))); 
    } 

    @Override 
    protected void bindRequestParameters(WebDataBinder binder, 
                    NativeWebRequest request) { 
            ServletRequest servletRequest = request 
                            .getNativeRequest(ServletRequest.class); 
            ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder; 
            servletBinder.bind(servletRequest); 
    } 
}

Configuration: 组态:

<mvc:annotation-driven>
    <mvc:argument-resolvers> 
            <bean 
                    class="test.util.OrderControllerArgumentResolver" /> 
    </mvc:argument-resolvers> 
</mvc:annotation-driven>

Was able to achieve desired result with this approach. 用这种方法能够达到预期的效果。 But not sure whether this approach cause any other problems. 但不确定这种方法是否会引起任何其他问题。 Please suggest. 请提出建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM