Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleCustom JSP File Container
public  class JspContainer extends WebMarkupContainer {
	
	private final static Logger log = Logger.getLogger(JspContainer.class);
	
	private static final long serialVersionUID = 1L;

	/** path to JSP file */
	private String _file;
	/** request attributes to be set before */
	Map<String, Object> _requestAttributes;
	
	protected JspContainer(String file) {
		super(file);
		this._file = file;
		this._requestAttributes = null;
		setRenderBodyOnly(true);
		setAuto(true);
	}
	
	/**
	 * Constructor. All components have names. A component's id cannot be null. This is the minimal
	 * constructor of component. It does not register a model.
	 * 
	 * @param id
	 *            The non-null id of this component
	 * @param jspPath  
	 * 				context  relative  path the JSP file
	 * @param requestAttributes  
	 * 				Map containing data put into the request Attributes before calling the JSP file
	 */
	public JspContainer(String id, String jspPath, Map<String, Object> requestAttributes) {
		super(id);
		this._file = jspPath;
		this._requestAttributes = requestAttributes;
	}

	/**
	 * {@inheritDoc}
	 * @see	org.apache.wicket.MarkupContainer#onRender(org.apache.wicket.markup.MarkupStream)
	 */
	@Override
	protected void onRender(MarkupStream markupStream) {
		super.onRender(markupStream);
	}

	/** 
	 * {@inheritDoc} 
	 * @see org.apache.wicket.MarkupContainer#onComponentTagBody(org.apache.wicket.markup.MarkupStream, org.apache.wicket.markup.ComponentTag)
	 */
	@Override protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
		super.onComponentTagBody(markupStream, openTag);
		getResponse().write(getJSPOutput());
	}
	/**
	 * Retrieve the jsp output as a string
	 * @return
	 * 		the content of the result of the JSP request in a String
	 */
	protected String getJSPOutput() {
		ServletContext context = ((WebApplication) Application.get()).getServletContext();
		try {
			// Make an attempt at locating the resource.
			// If it does not exist, the requestDispatcher.include will
			// simply ignore it.
			if (context.getResource(this._file) == null) {
				if (shouldThrowExceptionForMissingFile()) {
					throw new WicketRuntimeException(String.format("Cannot locate resource %s within current context: %s", this._file, context.getContextPath()));
				} else {
					log.warn( String.format("File will not be processed. Cannot locate resource %s within current context: %s", this._file, context.getContextPath()) );
				}
			}
		} catch (MalformedURLException e) {
			throw new WicketRuntimeException(e);
		}
		WebRequestCycle cycle = (WebRequestCycle) RequestCycle.get();
		ServletRequest request = cycle.getWebRequest().getHttpServletRequest();
		ServletResponse jspResponse = new CharResponseWrapper();

		// Set attributes to request, and store current attributes
		Map<String, Object> originalAttributes =null;
		if( this._requestAttributes != null ) {
			originalAttributes = new HashMap<String, Object>();
			for (Entry<String, Object> attribute : this._requestAttributes.entrySet()) {
				String keyAttribute = attribute.getKey();
				originalAttributes.put( keyAttribute, request.getAttribute( keyAttribute ) ); 
				request.setAttribute( keyAttribute, attribute.getValue() );
			}
		}
		try {
			context.getRequestDispatcher(this._file).include(request, jspResponse);
		} catch (ServletException e) {
			log.error("ServletException : ", e);
			throw new WicketRuntimeException(e);
		} catch (IOException e) {
			log.error("IOException : ", e);
			throw new WicketRuntimeException(e);
		} finally {
			/**
			 * restore original attributes;
			 */
			if( originalAttributes != null) {
				for (Entry<String, Object> attribute : originalAttributes.entrySet()) {
					request.setAttribute( attribute.getKey(), attribute.getValue() );
				}
			}
		}		
		if (log.isDebugEnabled()){
			log.debug(" JSP output =="  + jspResponse.toString());
		}
		return jspResponse.toString();
	}
	/**
	 *  Should we throw an Exception on JSP file missing ?
	 * @return
	 */
	private boolean shouldThrowExceptionForMissingFile() {
		return Application.get().getResourceSettings().getThrowExceptionOnMissingResource();
	}
}
Code Block
titleRegister You can register the custom component in your application class for automatic use
	
protected void init() {
    getPageSettings().addComponentResolver(new WicketJspResolver());
}
Code Block
titleInclude the JSP file in your HTML template
<wicket:jsp file="/foo.jsp"/>

OR

Code Block
titleYou can add a component to your page

   Map<String, Object> attributes =new HashMap<String, Object>();
   attributes.put( "data", "insert some data available to JSP file");
   add( new JSPContainer("jspId", "/WEB-INF/jsp/foo.jsp", attributes) );
Code Block
titleInclude the JSP file in your HTML template
<wicket:jsp is="jspId"/>