1

Has anyone tried implementing Struts2-jQuery example?

I am able to do setup for example given at below link but no values are populated in the page

http://struts.jgeppert.com/struts2-jquery-showcase/index.action

and no error is show in console not able to guess wat is the problem?

SELECTLIST.jsp

    <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> 
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %> 
<%@ taglib prefix="s" uri="/struts-tags"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
Reload example with two select boxes.
     <s:form id="formSelectReload" action="echo" theme="simple" cssClass="yform">
        <fieldset>
            <legend>AJAX Form</legend>
            <div class="type-text">
                <label for="language">Language: </label>
                <s:url id="remoteurl" action="jsonsample"/> 
                <sj:select 
                    href="%{remoteurl}" 
                    id="language" 
                    onChangeTopics="reloadsecondlist" 
                    name="language" 
                    list="languageObjList" 
                    listKey="myKey" 
                    listValue="myValue" 
                    emptyOption="true" 
                    headerKey="-1" 
                    headerValue="Please Select a Language"
                />
            </div>
            <div class="type-text">
                <label for="echo">Framework: </label>
                <s:url id="remoteurl" action="jsonsample"/> 
                <sj:select 
                    href="%{remoteurl}" 
                    id="selectWithReloadTopic" 
                    formIds="formSelectReload" 
                    reloadTopics="reloadsecondlist" 
                    name="echo" 
                    list="reloadList" 
                    emptyOption="true" 
                    headerKey="-1" 
                    headerValue="Please Select a Framework"
                />
            </div>
            <div class="type-button">
                <sj:submit 
                    id="submitFormSelectReload"
                    targets="result" 
                    value="AJAX Submit" 
                    indicator="indicator" 
                    button="true"
                    />
                    <img id="indicator" 
                        src="images/indicator.gif" 
                        alt="Loading..." 
                        style="display:none"
                    />
            </div>
        </fieldset>
    </s:form>
    <br/>
    Reload example with one select box and an buttonset.
     <s:form id="formSelectCheckBox" action="echo" theme="xhtml">
        <s:url id="remoteurl" action="jsonsample"/> 
        <sj:select 
            href="%{remoteurl}" 
            id="languageSelect" 
            onChangeTopics="reloadcheckboxes" 
            name="language" 
            list="languageObjList" 
            listKey="myKey" 
            listValue="myValue" 
            emptyOption="true" 
            headerKey="-1" 
            headerValue="Please Select a Language"
            label="Language"
            required="true"
        />
        <s:url id="remoteurl" action="jsonsample"/> 
        <sj:checkboxlist 
            href="%{remoteurl}" 
            id="frameworkCheckboxes" 
            formIds="formSelectCheckBox" 
            reloadTopics="reloadcheckboxes" 
            name="echo" 
            list="reloadList" 
            label="Framework"
            required="true"
            onChangeTopics="submitCheckboxForm"
        />
        <sj:submit 
            id="submitFormSelectCheckBox"
            listenTopics="submitCheckboxForm"
            targets="result" 
            value="AJAX Submit" 
            indicator="indicator2" 
            cssStyle="display : none;"
        />
    </s:form>
    <img id="indicator2" 
        src="images/indicator.gif" 
        alt="Loading..." 
        style="display:none"
    />

    <strong>Result Div :</strong>
</body>
</html>

ACTION CLASS:

package showcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "showcase")
public class JsonSample extends ActionSupport {

  private static final long   serialVersionUID = -2223948287805083119L;
  private static final Log    log              = LogFactory.getLog(JsonSample.class);
  private List<String>        languageList;
  private List<ListValue>     languageObjList;
  private Map<String, String> languageMap;
  private List<String>        reloadList;
  private String              language;

  @Actions( {
    @Action(value = "/jsonsample", results = {
      @Result(name = "success", type = "json" , location="/web/SelectList.jsp")
    })
  })
  public String execute()
  {

    log.info("build json lists language : " + language);

    languageList = new ArrayList<String>();
    languageObjList = new ArrayList<ListValue>();
    languageMap = new HashMap<String, String>();

    languageList.add("Java");
    languageList.add("PHP");
    languageList.add("C#");

    languageMap.put("J", "Java");
    languageMap.put("P", "PHP");
    languageMap.put("C", "C#");

    languageObjList.add(new ListValue("J", "Java"));
    languageObjList.add(new ListValue("P", "PHP"));
    languageObjList.add(new ListValue("C", "C#"));

    reloadList = new ArrayList<String>();
    if (language != null && language.equalsIgnoreCase("J"))
    {
      reloadList.add("Struts2");
      reloadList.add("MyFaces");
      reloadList.add("Tapestry");
    }
    else if (language != null && language.equalsIgnoreCase("P"))
    {
      reloadList.add("CakePHP");
      reloadList.add("Symfony");
      reloadList.add("Zend");
    }
    else if (language != null && language.equalsIgnoreCase("C"))
    {
      reloadList.add("NStruts");
      reloadList.add("ProMesh.NET");
      reloadList.add("Websharp");
    }

    return SUCCESS;
  }

  public String getJSON()
  {
    return execute();
  }

  public List<String> getLanguageList()
  {
    return languageList;
  }

  public Map<String, String> getLanguageMap()
  {
    return languageMap;
  }

  public List<ListValue> getLanguageObjList()
  {
    return languageObjList;
  }

  public List<String> getReloadList()
  {
    return reloadList;
  }

  public void setLanguage(String language)
  {
    this.language = language;
  }
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
saharsh
  • 93
  • 1
  • 3
  • 13
  • Closing for offtopic ? Really ? – Andrea Ligios Jul 04 '13 at 10:17
  • Same problem faced, and reported here [http://stackoverflow.com/questions/27456010/struts2-jquery-tag-select-not-loading-data][1]. Can someone help me [1]: http://stackoverflow.com/questions/27456010/struts2-jquery-tag-select-not-loading-data – javaguy Dec 14 '14 at 03:56

1 Answers1

2

You are missing the Head Tag

The "head" tag renders required JavaScript code to configure jQuery and is required in order to use any of the tags included in the jQuery plugin.

  <head>
    <sj:head/>
  </head>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Popular answer these days. :) – Aleksandr M Jul 04 '13 at 10:34
  • @Andrea Ligios : It started to work but the problem is in select list I am getting the values as 'undefined' .. Can you please help me further... – saharsh Jul 04 '13 at 12:12
  • @AleksandrM : Do you have any idea about this. – saharsh Jul 04 '13 at 12:13
  • Thanks it working fine now.. Thanks alot.. 'undefined' problem also ruled out. – saharsh Jul 04 '13 at 12:47
  • Glad that helped. @AleksandrM: absolutely true :) – Andrea Ligios Jul 04 '13 at 12:57
  • @AndreaLigios: One more thing i would like to know if you can help me please.. When I am using xml configuration the required double select is achievable but when I am using the annotation as shown in the code above it is no.. and no error message nothing y so? – saharsh Jul 04 '13 at 17:35
  • The logic would suggest that the problem is in the Annotations :) If you can't figure it out, you may want to open a new question by posting the annotations and the working struts.xml side by side to let us debugging it – Andrea Ligios Jul 05 '13 at 08:42