PlayFramework如何从超级控制器

时间:2017-04-23 11:25:11

标签: java controller superclass

我正在完成我的大学项目,但我遇到了奇怪的问题。由于我是学生,如果它是平淡无奇的,请在上述道歉。 我有我的BasicCommonController有List backendErrors = new ArrayList<>() ,我有另一个扩展BasicCommonController的Controller,我可以从BasicCommonController访问backendErrors列表,但我不能 把新元素放到列表中,wchich总是空的。我试图通过super.backendErrors访问,但它也无法正常工作。 如何向super.backendErrors添加一些错误并在另一个控制器中访问它

1 个答案:

答案 0 :(得分:0)

这是抽象控制器:

package controllers;

import org.apache.commons.lang3.StringUtils;
import play.Logger;
import play.Play;
import play.mvc.Controller;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by vv on 22.04.2017.
 */
public class BasicAbstractController extends Controller {

    public static final String GO_HOME = "/";

    public List<String> backendErrors = new ArrayList<>();

    public static String getPlaceToObserve(){
        String place = Play.application().configuration().getString("storage.place");
        if(StringUtils.isNotBlank(place)){
            return place;
        }
        return StringUtils.EMPTY;
    }

    public static String getServerInstance(){
        String instance = Play.application().configuration().getString("storage.place");
        if(StringUtils.isNotBlank(instance)){
            return instance;
        }
        return StringUtils.EMPTY;
    }
}

这是示例控制器

package controllers;

import com.google.common.io.Files;
import com.sun.org.apache.regexp.internal.RE;
import constans.AppCommunicates;
import play.Logger;
import play.mvc.Http;
import play.mvc.Result;
import util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by vv on 22.04.2017.
 */
public class FileUploadController extends BasicAbstractController {


    public Result upload() {
        Http.MultipartFormData<File> body = request().body().asMultipartFormData();
        Http.MultipartFormData.FilePart<File> picture = body.getFile("picture");
        if (picture != null) {
            String fileName = picture.getFilename();
            String contentType = picture.getContentType();
            File file = picture.getFile();
            File fileToSave = new File(getPlaceToObserve() + "/" + picture.getFilename());
            try{
                Files.copy(file,fileToSave);
            }
            catch (IOException ioe){
                Logger.error("Unable to write file");
            }
            Logger.error("File Handled Cuccessfully");
            return redirect(GO_HOME);
        } else {
            flash("error", "Missing file");
            return badRequest();
        }
    }

    public Result delete(String fileName){
        List<File> files = FileUtil.getCurrentFileNames();
        File fileToDelete = null;
        for (File file : files) {
            if(file.getName().equals(fileName)){
                fileToDelete = file;
                break;
            }
        }
        boolean deletionResult = FileUtil.deleteGivenFile(fileToDelete);
        if(!deletionResult){
//            i am not able to add smthg here
            backendErrors.add(AppCommunicates.UNABLE_TO_DELETE_FILE);
        }
        return redirect(GO_HOME);
    }
}

我无法添加或访问其他控制器的列表