My favorites | Sign in
Project Home Wiki Issues Source
Project Information
Members

Overview

Plugin was developed as a part of sibutu project. Plugin consists of two parts - annotation based interceptor for preloading JCR nodes, and wrapper classes for transparent wrapping of JCR Nodes so they implement both javax,jcr.Node and java.util.Map. This approach allows node's properties to be set directly by Struts2, also validation can easily be done and JCR node's properties can be referenced in JSP pages in a standard way.

Features

  • Easy access to jcr node's properties on jsp pages
  • Preloading nodes using annotation

Installation

  • Download the Plugin and put in into your WEB-INF/lib folder.
  • Configure the interceptors stack to include JCR interceptor
<interceptor name="jcrActionsInterceptor" class="org.sibutu.plugin.struts2.jcr.interceptor.JcrActionsInterceptor" />
 <interceptor-stack name="myStack">
 ...
 <interceptor-ref name="jcrAction" />
 <interceptor-ref name="staticParams" />
 <interceptor-ref name="params">
 <param name="excludeParams">dojo\..*</param>
 <param name="ordered">true</param>
 </interceptor-ref>
 ...
 </interceptor-stack>
  • Add lines to spring config describing JcrTemplateWrapper
<bean id="wrapperFactory" class="org.sibutu.plugin.struts2.jcr.wrapper.SimpleNodeWrapperFactory"/>
<bean id="jcrTemplate" class="org.sibutu.plugin.struts2.jcr.wrapper.JcrTemplateWrapper">
  <property name="sessionFactory" ref="jcrSessionFactory"/>
  <property name="wrapperFactory" ref="wrapperFactory"/>
  <property name="allowCreate" value="true"/>
</bean>

Example

The following example demonstrates plugin usage advantages. There is struts action AddressAction which is used to edit address. Address is a JCR node, having property addressId which is unique for each address node and serves as identifier.

Struts action

public class AddressAction extends ActionSupport {
    Long addressId;
    Node address;
    JcrTemplate jcrTemplate;
    public String save() throws RepositoryException{
      address.save();
      return SUCCESS;
    }
    public Long getAddressId() {
      return addressId;
    }
    public void setAddressId(Long addressId) {
      this.addressId = addressId;
    }
    @JcrNode(node="/address", id="addressId")
    public Node getAddress() {
      return address;
    }
    public void setAddress(Node address) {
      this.address = address;
    }
  }

JSP page

  <s:form action="address!save" name="addressForm" id="addressForm">
      <s:textfield name="address.country" size="50" label="Country" required="true"/>
      <s:textfield name="address.city" size="50" label="City" required="true"/>
      <s:textfield name="address.state" size="50" label="State" required="true"/>
      <s:textfield name="address.address" size="50" label="Address" required="true"/>
  </s:form>

Because the node is wrapped, jsp code interacts with the node in a way like it is java.util.Map. Then the page is rendered, the node is automatically loaded from repository using path from the annotation and the addressId provided as a parameter. After a user fills in and submits the form the node loaded again and it's properties are set by struts. The only thing we need to do is to save the node.

Powered by Google Project Hosting