java - Freemarker and Struts 2, sometimes it evaluates as a sequence+extended_hash -


first let me using struts2 + freemarker real blast. yet there's driving me crazy, because cannot understand why happens. ask here maybe else has idea share it.

i've got action, property. say

private string mytext; 

then i've got setter , getter:

public void setmytext(string mytext) {   this.mytext = mytext; }  public string getmytext() {   if (mytext == null)     mytext = "(empty)";    return this.mytext; } 

the result (in struts.xml) freemarker result. in freemarker template there's line following:

<p>the text is: ${mytext}</p> 

now consider i'm calling action without text parameter: url

http:localhost:8080/myapp/myaction 

as getter provides default value, when action processed , result passed template, property set default; (html on browser side)

<p>the text is: (empty)</p> 

if call action parameter set, instead (i mean like:

http:localhost:8080/myapp/myaction?mytext=hallo 

) things go wrong. freemarker fires following exception:

exception occurred during processing request: "${...}" content: expected string or automatically convertible string (number, date or boolean), has evaluated sequence+extended_hash (string[] wrapped f.e.b.arraymodel) 

it seems "mytext" found twice... doing wrong? or, @ least, there can explain me why happens?

p.s.: it's found twice; following way workaround problem:

<#if mytext?is_sequence>${mytext[0]}<#else>${mytext}</#if> 

yet seems me not viable wrap every variable in way.

p.p.s.: further hint: in freemarker template there's call action lines before. like:

<@s.action var="inneraction" name="gettable" namespace="/foo" /> 

if comment line above, works fine.

the mytext variable freemarker context, if want use action property

<p>the text is: ${action.mytext}</p> 

note action prefix not required access action properties. property resolution method applied when resolving freemarker variables:

property resoloution

your action properties automatically resolved - in velocity view.

for example ${name} result in stack.findvalue("name"), results in action.getname() being executed.

a search process used resolve variable, searching following scopes in order, until value found :

  • freemarker variables
  • value stack
  • request attributes
  • session attributes
  • servlet context attributes

and later can read objects accessible context.

objects in context

the following variables exist in freemarker views

  • req - current httpservletrequest
  • res - current httpservletresponse
  • stack - current ognlvaluestack
  • ognl - ognltool instance class contains useful methods execute ognl expressions against arbitary objects, , method generate select list using pattern. (i.e. taking name of list property, listkey , listvalue)
  • struts - instance of strutsbeanwrapper
  • action - current struts action

  • exception - optional exception instance, if view jsp exception or servlet exception view

the error might caused searches value stack , returning didn't expect depending on structure of stack @ moment of execution. adding prefix variable point out exact location of property should fix redundancy in code when searching in value stack.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -