java - Spring 4 + Tiles (3 || 2.2.2) - Any way to access beans in view context? -
i trying access beans in tiles view.
tried solutions:
solution 1 tried
the solution accessing spring beans tiles view (jsp) francarl posted (servletcontextattributeexporter) working fine on first view (hello.jsp, not theme.jsp).
<?xml version="1.0" encoding="iso-8859-1" ?> <!doctype tiles-definitions public "-//apache software foundation//dtd tiles configuration 3.0//en" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="defaulttheme" template="/web-inf/views/theme.jsp" /> </tiles-definitions>
i can access bean in hello.jsp not in /web-inf/views/theme.jsp
solution 2 tried
tried skaffman solution, 'tilesexposingbeansviewresolver', executing , buildview , it's code view.setexposecontextbeansasattributes(this.exposecontextbeansasattributes);
executed too. rendermergedoutputmodel
view class not executed. tried lot of way doing this. (tried these orders -1, 1, 10, 100, 99999). don't sure whatever can access these beans @ theme.jsp work. (i don't know why 1 doesn't fire up)
solution 3 tried
tried create viewpreparer, execute working , springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this);
sucessfully injects bean viewpreparer. unabled transfer viewpreparer theme , hello.jsp
public class viewpiewpreparer implements viewpreparer { @autowired private mainbean kmpv; public void execute(tilesrequestcontext tilesrequest, attributecontext attributecontext) throws preparerexception { springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this); system.out.println(kmpv.gettwits().size()); // working // cant access of these in hello , template tilesrequest.getrequestscope().put("deneme", "deneme"); tilesrequest.getsessionscope().put("deneme", "deneme"); tilesrequest.getapplicationcontext().getapplicationscope().put("deneme", "deneme") } }
is there solution this? need access news , tweets theme.jsp.
public class mainbean { @autowired kamptanimfacade kamptanimfacade; @autowired kategorifacade kategorifacade; public list<kategori> getkamplar() { return kategorifacade.getmenulist(); } public list<status> gettwits() { return mainservlet.twitter.statuses; } public string getdeneme() { return "deneme"; } }
if set tilesconfigurer resolve preparers bean names:
@bean public tilesconfigurer gettilesconfigurer() { tilesconfigurer configurer = new tilesconfigurer(); configurer.setdefinitions(new string[] { "/web-inf/tiles.xml" }); ... configurer.setpreparerfactoryclass(springbeanpreparerfactory.class); return configurer; }
and configure tiles definition use specific view preparer (spring bean name) in tiles.xml:
<definition name="viewname" template="/web-inf/.../view.jsp" preparer="aviewpreparer"> <put-attribute .... /> </definition>
you can define bean (inside @componentscan) like:
@component public class aviewpreparer implements viewpreparer { @autowired private autowiredbean autowiredbean; @override public void execute(request tilesrequest, attributecontext attributecontext) throws preparerexception { attributecontext.putattribute("tilesattributename", new attribute("tilesattributevalue")); } }
this way can print attribute (string) using in jsp:
<tiles:insertattribute name="tilesattributename"/>
if need use complex object attribute, bean or list, can use following in jsp:
<tiles:importattribute name="tilesattributename"/>
and then, have object in page scope , can work (in case of list):
<c:foreach items="${tilesattributename}" var="item"> ${item} </c:foreach>
you can use simplespringpreparerfactory instead of springbeanpreparerfactory in tilesconfigurer, in case should use complete class name of view preparer in tiles.xml:
<definition name="viewname" template="/web-inf/.../view.jsp" preparer="package.name.of.view.preparer.aviewpreparer"> <put-attribute .... /> </definition>
Comments
Post a Comment