在命令Pattern java中传递参数

时间:2016-03-05 07:37:36

标签: java jsp servlets

我有一个具有主要功能的类和方法。

public class SeriesOfCommand {
    List<Products> listofproducts = ListOfProducts.retrieveProducts();

    public List<Products> filterByPrice(float min,float max){           
        if(listofproducts!=null){
            System.out.println(listofproducts.size() +" size: " +listofproducts);
            List<Products> filteredListOfProducts = listofproducts.stream()
                .filter(p -> p.getPrice() >= min && p.getPrice() <= max)
                .collect(Collectors.toList());
            System.out.println(filteredListOfProducts.toString());
            return filteredListOfProducts;
        }
        else{
            return null;
        }
    }
}

所以这里我想在使用execute调用该方法时使用COMMAND PATTERN 但我面临的问题是如何在方法weightFilter.filterByWeight(min, max)中传递这些参数。我有一个servlet,它从jsp

读取用户输入
public class WeightFilter implements ICommmand {
    SeriesOfCommand weightFilter;

    public WeightFilter(SeriesOfCommand weightFilter) {
        this.weightFilter = weightFilter;
    }

    @Override
    public void filterBy() {
        //here how do I give min and max values to the method as am reading the user inputs from servlet ans jsp
        weightFilter.filterByWeight(min, max);
    }
}

获取参数并将其设置为属性的Servlet

    String minWeight = request.getParameter("minWeight");
    String maxWeight = request.getParameter("maxWeight");
    float min = Float.parseFloat(minWeight);
    float max = Float.parseFloat(maxWeight);

    request.setAttribute("minWeight", minWeight);
    request.setAttribute("maxWeight", maxWeight);
    request.setAttribute("filterByWeight", weightFilter.filterBy(min, max));

调用方法.. 然后将属性设置为min和max,并打印JSP中方法给出的结果

0 个答案:

没有答案