Dfr wrote:

Hello,

Is there a best way to set http headers with JSF ?
Currently i need set different Last-Modified values for different JSF pages. It generally based on data fetched from storage by backing beans. Is it ok to set such headers in getter methods of bean ? I currently don't like this approach being fan of SRP(Single Responsibility Principle), i.e. getter must return data, but not set headers.

And one more question, does anoyone set "Cache" headers manually or just stick with defauts ?

Thank you.



I do set cache headers in the app I currently work on, but do it from a standard servlet filter rather than at the JSF level.

If the headers you want to set can be deduced from just the view id then I suggest you write your code as a PhaseListener that runs on "before render".

If you need page-specific data then I suggest you write a custom JSF component that you add to the top of every page that needs the headers set. This header can then evaluate an EL expression to fetch whatever it needs to determine the correct Last-Modified value. This seems more elegant than having backing beans explicitly writing headers. However it is a lot more work. As an easier alternative, something like this:
 <h:outputText style="display:none" value="#{bean.doSomething}"/>
can be used to invoke arbitrary back-end logic during rendering. It's hacky but there is no component I know of that can invoke a backing bean method without generating output to the page. The display:none style makes it pretty clear that this is really just triggering a method call.

Regards,

Simon

Reply via email to