0
\$\begingroup\$

I have a JSF project composed by some Maven Modules.

I have one module that needs to read and write some files on server. To do this I need to know the "real" full path of the application/server.

The way it is coded, only the View layer can access this information by this method:

public final static String getRealPath(String relativePath) {
    FacesContext fc = FacesContext.getCurrentInstance();
    ServletContext context = (ServletContext)fc.getExternalContext().getContext();

    return context.getRealPath(relativePath);
}

Can my module (not in View layer) call some Faces methods like FacesContext.getCurrentInstance()? Am I breaking some rule, layer or pattern? Or should I pass this path through all layers as a method parameter?

\$\endgroup\$
1
  • \$\begingroup\$ Why to know the real full path? There's a difference in where your files are located in the server and the url the user has to access them from. That's done by servlet mappings. \$\endgroup\$
    – Aritz
    Commented May 3, 2018 at 7:51

1 Answer 1

2
\$\begingroup\$

I guess you'd be better off using Class.getRessource() to access your files. I assume they are packaged with your application.

This method would not be restricted to the view layer (where it actually not belongs). And you won't be concerned with servlet container details.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.