Oddjob Struts to JSF 2

The initial migration of the Oddjob Webapp from Struts to Java Server Faces is complete. Functionally the new JSF app is an exact match for it’s old Struts counterpart.

Oddjob JSF Webapp

Oddjob JSF Webapp

Compare this with the Struts version at the bottom of this post – the only give away is the url which is now index.xhtml not index.jsp.

Comparing Struts and JSF it’s very noticeable how much neater the JSF code base is and how much nicer Facelets are than JSP pages.

Frames in JSF wasn’t easy being deprecated in HTML 5 and so not really supported. The <h:form> tag does support the target attribute so by setting this to _top and by returning "index.xhtml?faces-redirect=true" from the form actions it was possible to cause all frames to be reloaded.

How to set the refresh parameter in the HTTP header was also not immediately obvious. Struts gives you a lot more of the flow of a Servlet whereas JSF does much more for you and as a result hides much more from you. After a lot of Googling it was also quite straightforard. I added a

	<f:event listener="#{refresh.setRefreshInHeader}" type="preRenderView" />

to the index.xhtml page, and a corresponding method in the refresh backing bean.

	/** Refresh in seconds. */
	private Long refresh;

	/**
	 * Capture the preRenderView event to set the refresh in the HTTP header.
	 */
	public void setRefreshInHeader(ComponentSystemEvent event) {

		if (refresh != null) {
			FacesContext context = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) 
					context.getExternalContext().getResponse();
			response.setHeader("Refresh", refresh.toString());
		}
	}

And this seems to do the trick.

The code is in GitHub and on Sourceforge in the snapshots.

Comments are closed.