Search code examples
javastruts2freemarker

My interceptors are not fetching values from action page to FTL in Struts 2


I am not able to find the issue that why my FTL couldn't fetch values from action page.

This is my FTL code:

<html>
<head></head>
<body>
<#assign temp = 12311>
<h4>hello world  : ${FirstAction?if_exists.random} #{temp}</h4>
</body>
</html>

This is my struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation"
        value="true" />
    <constant name="struts.devMode" value="true" />
  <package name="default" extends="struts-default">
  
    <action name="getTutorial" class="MyActions.FirstAction" >
      <result name="success">/success.jsp</result>
      <result name="failure">/error.jsp</result>
    </action>
    
    <action name="submit" class="MyActions.FirstAction" method="submit"></action>
    
    <action name="callme" class="MyActions.FirstAction" method="myMethod">
      <result name="success">/FreeMarkerPages/testingAction.jsp</result>
      <result name="failure"  type="freemarker">/FreeMarkerPages/FirstFTL.ftl</result>
    </action>
    
  </package>
</struts>

This is my action class:

package MyActions;

import java.util.Random;

import com.opensymphony.xwork2.ActionSupport;

import service.ColorPicker;

    public class FirstAction extends ActionSupport{
        private int random;
        public String execute() {
            return "failure";
        }
        public String myMethod(){
            setRandom(9999);
            System.out.println("My method "+random);    
            return "failure";
        }
        
        public String submit(){
            System.out.println(random);
            return null;
        }
        
        public int getRandom() {
            return random;
        }
        public void setRandom(int random) {
            this.random = random;
        }

This is my FTL error :

FreeMarker template error (HTML_DEBUG mode; use RETHROW in production!)

The following has evaluated to null or missing: ==> FirstAction?if_exists.random [in template "FreeMarkerPages/FirstFTL.ftl" at line 5, column 22]


Solution

  • Stupid mistake. Instead of writing ${FirstAction?if_exists.random} i should have written ${random?if_exists}.

    This is not a static class.