Support for dynamic and translated names in Apache Tiles

I have a Spring MVC project that uses Apache Tiles. I implemented it so that the headers could be read from the message source as follows:

<tiles-definitions> <definition name="some-definition" extends="public.base"> <put-attribute name="title" value="some-definition.title" cascade="true" /> </definition> </tiles-definitions> 

And in my template file (defined by public.base ), I do the following:

 <title><spring:message text="" code="${title}" /></title> 

Now this works fine for static translated titles, but I also want to support dynamic headers, for example. to display the company name. I could do it like this:

 <tiles-definitions> <definition name="some-definition" extends="public.base"> <put-attribute name="title" expression="${company.name}" /> </definition> </tiles-definitions> 

And then just print the title in my template as follows: <c:out value="${title}" /> . However, the problem is that my code breaks because the value of the title attribute is no longer the message key. I want to be able to support the following scenarios:

  • Static headers, for example. "About us"
  • Purely dynamic headers, for example. "$ {Company.name}"
  • Dynamic headers with translated content, for example. "Welcome to $ {company.name}"

Ideally, I could use an expression language in my message source, but I could not get this to work. I experimented a lot with various solutions, but I can not find the right one. If I could use the expression language in my message source, that would be easy. For example, is it possible to somehow do the following?

 some-definition.title = Hello there, ${company.name} 

And in my template:

 <spring:message text="" code="some-definition.title" var="test" /> <c:out value="${test}" /> 

The above does not work as it displays ${company.name} , not the actual contents of the variable. Is there a way to do something like this work? Or are there other ways I can support the scripts listed above?

I was thinking of creating a custom JSTL tag where I would parse a string expression in plain Java code (the string that was translated), but I realized that I probably would have to explicitly specify the root object to "replace variables" "work like described here , then it doesn't look like such a dynamic solution.

Is there a way I can complete this task? Any help is much appreciated!

+6
source share
3 answers

can localization of tile definitions in general? ref http://tiles.apache.org/framework/tutorial/advanced/l10n.html

Otherwise, I would look at ViewPreparer, which extends spring messages for you.

0
source

A fragment definition may define an attribute called "controllerClass". This class must implement the org.apache.struts.tiles.Controller interface and its overridden execute () method is called before the fragment definition is displayed.

The execute method has read / write access to the name-value pairs of the tile attributes, as well as to the HttpServletRequest object. You can find your attribute and change the values ​​dynamically or replace it with a semi-statistical value from the properties file.

 <definition name="some-definition" extends="public.base" controllerClass="pkgName.CustomController"> <put-attribute name="title" expression="${company.name}" /> </definition> 

And then in your CustomController class you can change the attributes

 public class CustomController implements Controller{ public void execute(ComponentContext tileContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException, IOException { String attrName = (String)tileContext.getAttribute("title"); String attrVal = request.getAttribute("pageTitle"); //get a dynamic value or else retrieve from a resource bundle tileContext.putAttribute(attrName,attrVal); } public void perform(ComponentContext tileContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws ServletException, IOException { execute(tileContext, request, response, servletContext); } } 
0
source

You cannot use Tiles for this, juste your Spring -Model:

In the controller:

 @RequestMapping(value = { "/page1" }, method = RequestMethod.GET) public String page1() { model.addAttribute("titre_page", messageSource.getMessage("home.hello", null,null) } 

In layout.jsp:

 ... <title>${titre_page}</title> ... <tiles:insertAttribute name="body" /> .... 
0
source

Source: https://habr.com/ru/post/984784/


All Articles