Dimman
Dimman

Reputation: 1206

Using TinyMCE to save content into Facelets templates

I'm trying to implement a solution with TinyMCE and JSF 2.x. I'm not sure however how to go on (different approaches).

What I'm trying to do...

Create user templates (Facelets files) with specific editable areas/sections that someone can edit via TinyMCE after they've logged in.

I dont want to use DB to store the "editable" parts or insert them dynamiclly into the Facelets templates.

I was thinking sommething like this.

  1. Load the editable content into TinyMCE.
  2. User updates the content.
  3. On submit, validate the content with Java API for XML Processing or some other xml/xhtml parser.
  4. Read the Facelets template and replace the editable content with the submited one.
  5. Replace the old Facelets file with the new one and save it.

Should I use a middle step to pre save the submited content into a Facelets file before I do the change? Any other ideas on how to go on with this would be appreciated!

Upvotes: 1

Views: 365

Answers (3)

BalusC
BalusC

Reputation: 1109292

I dont want to use DB to store the "editable" parts or insert them dynamiclly into the Facelets templates.

Then your only feasible option is to save them in local disk file system. Note that you can and should not save them in the deploy folder of the WAR, but outside the WAR. Otherwise all changes are simply lost during a redeploy or even a simple server restart. Even more, you won't be able to write files to it when the server is configured to expand the WAR in memory instead of on disk.

Saving files outside WAR is easy. Just write to a FileOutputStream on this file:

new File("/some/base/path", filename);

Getting Facelets to resolve resources from outside WAR requires a custom ResourceResolver:

public class MyResourceResolver extends ResourceResolver {

    private ResourceResolver parent;

    public MyResourceResolver(ResourceResolver parent) {
        this.parent = parent;
    }

    @Override
    public URL resolveUrl(String path) {
        URL url = parent.resolveUrl(path); // Resolves from WAR.

        if (url == null) {
            url = new File("/some/base/path", path).toURI().toURL();
        }

        return url;
    }

}

Which needs to be registered as follows in web.xml.

<context-param>
    <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
    <param-value>com.example.MyResourceResolver</param-value>
</context-param>

Upvotes: 2

Garytxo
Garytxo

Reputation: 346

I was using the richfaces html editor in a project, but we encountered several issues so we decided to create our own facelet tag which loads the tinymce files that were served up by apache. Works like a charm

Upvotes: 0

Piotr Gwiazda
Piotr Gwiazda

Reputation: 12222

Xhtml files are located by default inside WAR. You could deploy exploded WAR and save them in-place. However after redeployment you'd loose all your changes.

You could "hack" JSF to look for templates outside WAR archive but this might be tricky.

Do you need to stick to JSF? Component-based server-side technology is not the best choice. I'd better use some template-based controller-first technology like Spring MVC or Grails for such solutions. Note that in JSF it is XHTML page that controls the flow. It's not just templating technology.

Also note that this is why CMS systems were invented.

Upvotes: 1

Related Questions