Search code examples
spring-mvcjtablejsp-tags

spring mvc model value with jsp tags


Here is what i have: I am using JTable (http://www.jtable.org/) inside jsp pages, along with spring mvc model. I also have setup localization, all these work fine. Below i have part of my code, added what i consider as relevant as i am not sure... Please ask me as soon as you get some input for me (will answer on monday as in weekend i doupt i can have pc access).

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:i18n/messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang"/>
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en_US"/>
</bean>

Here is what i need to do: I need to create a jtable where fields, actions,etc come from server so that these are dynamic (check Dynamic creation of multilevel Javascript object for jQuery-jTable made from other user). In my case myobj will come from server as a string, i.e.

 @RequestMapping(value = "/locales", method = RequestMethod.GET)


public ModelAndView testList(ModelAndView mv, final HttpServletRequest request) {
    mv.setViewName("list");
    mv.addObject("model",
     "{\n" +
       "                    title: '<spring:message code=\"table.users.users\"/>',\n" +
        ....
       "                    fields: {\n" +
       "                        ID: {\n" +
       "                            key: true,\n" +
       "                            list: false,\n" +
       "                            create: false,\n" +
       "                            edit: false\n" +
       "                        },\n" +
       "                        Name: {\n" +
       "                            title: '<spring:message code=\"table.name\"/>',\n" +
       "                            width: '15%',\n" +
      ...

This text you see above passed in the model of the controller will be created dynamically (using velocity engine,dynamic data,...)

list.jsp is as follows:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<tags:template>
    <jsp:body>
         <script>
            $(document).ready(function() {
                $('#ListContainer').jtable(${model});
                $('#ListContainer').jtable('load');
            });
        </script>
            <div id="ListContainer" style="width:99%;"></div>
    </jsp:body>
</tags:template>

My problem is that although i have in my site setup localization et all, the tags i.e. ' are not rendered when these are inside the content of the controller's returned model. Is there a way to say to the Controller or the InternalResourceViewResolver to resolve the model's value as if it was a jsp?

I hope i made my problem clear and gave all that is needed to respond to me, if not please feel free to ask. I am afraid since i am still leaning i do not have either clear in my mind how all these bind together the only thing i know is that i need to have a dynamic/generic jtable list fully localized list.


Solution

  • I managed to work around my problem. Still not sure if this is the best solution but i thought to let you people know in case someone else needs same solution.

    So here is how it goes, i have the following flow: controller-->{Velocity engine}-->localized jtable configuration-->jsp

    The controller gets the model and feeds it to a velocity template. Inside there i also feed (apart from my velocity template model):messageSource and localeResolver. Wherever i have localization labels in velocity i write them as follows: ${messageSource.getMessage(${field.title}, $noArgs, ${field.title}, $locale)}

    even when i need to pass some velocity model values which should be evaluated from velocity i use the macro #evaluate($table.name).

    After velocity evaluation finishes i have the configuration of jtable ready and feed it to the jsp.

    I will leave this question open for some more time in case someone can provide with a beter solution.