Announcement Announcement Module
Collapse
No announcement yet.
freemarker config in spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • freemarker config in spring

    I still can't seem to get FreeMarker working w/ Spring. Whatever I put in my url, I get blank pages back. Anyone have any ideas here? (Also, I'm not exactly sure what I should be entering in the url, I thought anything that ends like the prop key mappings I have in my *-servlet.xml file.) It's only slightly different than the samples/dbimage war, but I went w/ what what in my ProSpring book cause I understood the explanations better.

    I've got my *.ftl files as such ...

    ~/WEB-INF/views/product/productList.ftl
    ~/WEB-INF/views/order/orderList.ftl

    ... and my controller is
    Code:
    public class MyController extends MultiActionController {
    
        public ModelAndView showProductList(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            return new ModelAndView("productList");
        }
    
        public ModelAndView showOrderList(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            return new ModelAndView("orderList");
        }
    }
    Here's my web.xml ...
    Code:
    <web-app>
        <servlet>
            <servlet-name>myApp</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>myApp</servlet-name>
            <url-pattern>*.ftl</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>/</welcome-file>
            <welcome-file>/index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    And here's myApp-servlet.xml
    Code:
    <beans>
    
        <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath" value="/WEB-INF/views/"/>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="suffix" value=".ftl"/>
        </bean>
    
        <bean id="myController" class="com.myTest.web.MyController">
            <property name="methodNameResolver">
                <ref local="myAppMethodNameResolver"/>
            </property>
        </bean>
    
        <bean id="myAppMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name="mappings">
                <props>
                    <prop key="/product/productList">queueList</prop>
                    <prop key="/order/orderList">mailList</prop>
                </props>
            </property>
        </bean>
    </beans>

  • #2
    I'm still having a very difficult time integrating freemarker w/ Spring controllers ...

    Q1. Is it normal to put all web pages under ~/WEB-INF/views/... instead of the war root if you don't want people accessing them directly from the url?

    Q2. Doesn't freeMarker say that my *.ftl pages are in ~/WEB-INF/views and are my <property name="mappings"> below shown correctly? (not from /, but are relative to /WEB-INF/views).

    Q3. Shouldn't a url of localhost:8080/indexController or w/ =index or something return the *.ftl page in ~/WEB-INF/views/order/orderList.ftl?

    myapp-servlet.xml
    Code:
    <beans>
    
        <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
            <property name="templateLoaderPath" value="/WEB-INF/views/"/>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="suffix" value=".ftl"/>
        </bean>
    
        <bean id="indexController"
            class="com.web.controllers.IndexController">
    
            <property name="methodNameResolver">
                <ref local="indexMethodNameResolver"/>
            </property>
        </bean>
    
        <bean id="indexMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name="mappings">
                <props>
                    <prop key="order/orderList.ftl">order-index</prop>
                </props>
            </property>
        </bean>
    
    </beans>
    Code:
    public class IndexController extends MultiActionController {
    
        public ModelAndView index(HttpServletRequest request,
                HttpServletResponse response) {
    
            return new ModelAndView("order-index");
        }
    }

    Comment


    • #3
      Spring and freemarker

      Also I cannot get Spring and Freemarker working, how do you do it?

      Comment


      • #4
        /*
        some notes:
        my freemarker template is located in WEB-INF->classes->templates
        and my jsp just has $message
        and i do not use the html head body tags in my freemarker template file, i think that had caused a display problem.
        */

        <!-- so freemarker can find the needed template ftl file in my servlet xml -->

        <bean id="freemarkerConfig" class="org.springframework.ui.freemarker.
        FreeMarkerConfigurationFactoryBean">
        <property name="templateLoaderPath">
        <value>classpath:templates/</value>
        </property>
        </bean>


        // part of a class that uses freemarker with your request object:

        // gently place the formRequestObj into the freemarker template
        // i.e. user filled out an online web form and hit submit

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("formRequestObj", formRequestObj);

        try {
        // mix well

        StringWriter stringWriter = new StringWriter();
        Template template = freemarkerConfig.getTemplate(templateName);
        template.process(model, stringWriter);
        stringWriter.flush();
        stringWriter.close();

        message = new String(stringWriter.getBuffer());
        // presto, stick message in your jsp like:
        // <c:out value="${message}" escapeXml="false" />

        } catch (TemplateException e) {
        e.printStackTrace();
        //message = e.toString();
        }
        ...

        hope this give you ideas that may help.
        ---

        Comment


        • #5
          Thanks

          Hi,

          Thanks for that !! - it sounds like a way to do it, I have not tried yet, but I understand how it works.

          But it doesn't seem like the way Spring *wants* you to do it - I thought Spring just wanted it to go direct from the controller to the Freemarker template without a JSP.

          Well.... I feel sad there isn't any example of that and the documentation is very very lacking, otherwise Freemarker and Spring could be good friends.

          Thanks, Philip

          Comment


          • #6
            continued

            Welcome. Sorry, to explained I was doing. I was putting it in a requestReceived.jsp page for a printable receipt page.

            Comment

            Working...
            X