jqueryjsonajaxstruts2struts2-jquery

Struts2 jQuery tag select not loading data


I am trying to use <sj:select> tag to render my list in Struts2 JSP.

At the same location I have used a <s:select> tag as well

Data is getting populated in the <s:select> tag whereas no data is being populated in the <sj:select> tag.

This is also used:

<sj:head jqueryui="true" />

A normal use of Struts2 <s:select> tag, gives me result

<s:select list="languageList"></s:select>

but in the same jsp but just a another div gives nothing even when the same object (List) here named, languageList is used, as shown in the snippets

        <s:url id="remoteurl" action="sample2"/> 
    <sj:select 
        href="%{remoteurl}" 
        id="echo" 
        name="echo" 
        list="languageList" 
        emptyOption="true" 
        ="-1" 
        headerValue="Please Select a Language"
/>

Have edited the question as per the suggestion.

I mean to say that the action class is returning the object languageList but the UI in jsp is not being able to render one, please check snapshot from the O/P screen for understanding better.

Please help me figure out what is wrong here?

I was trying to replicate the tutorial on struts jQuery select as shown in the link: http://struts.jgeppert.com/struts2-jquery-showcase/index.action

JSP:

<%@ 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">
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Double Combo</title>

<sj:head jqueryui="true" />
<sb:head includeScripts="true" includeScriptsValidation="true" />
<meta http-equiv="X-UA-Compatible"
    content="IE=EmulateIE7; IE=EmulateIE9">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport"
    content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />

<!-- begin jquery -->

<script src="<%=request.getContextPath()%>/jq/jquery-1.8.2.min.js"
    type="text/javascript"></script>

<script src="<%=request.getContextPath()%>/jq/functions.js"
    type="text/javascript"></script>

</head>
<body>
    <div id="simple">
        <s:select list="languageList"></s:select>
    </div>

    <s:form id="formSelectOne" action="echo" theme="simple"
        cssClass="yform">
        <fieldset>
            <legend>AJAX Form populated by a String List</legend>
            <div class="type-text">
                <label for="echo">Echo: </label>
                <s:url id="remoteurl" action="sample2" />
                <sj:select href="%{remoteurl}" id="echo" name="echo"
                    list="languageList" emptyOption="true" headerKey="-1"
                    headerValue="Please Select a Language" />
            </div>
            <div class="type-button">
                <sj:submit targets="result1" value="AJAX One" indicator="indicator"
                    button="true" />
                <img id="indicator" src="images/indicator.gif" alt="Loading..."
                    style="display: none" />
            </div>
        </fieldset>
    </s:form>

    

</body>
</html>

Action Class:

package com.action;

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

import org.apache.struts2.convention.annotation.*;

import com.opensymphony.xwork2.ActionSupport;
@ParentPackage( value = "showcase")
public class Sample extends ActionSupport{
    
    private static final long serialVersionUID = -2223948287805083119L;
    private List<String> languageList;
    private List<ListValue> languageObjList;
    private Map<String, String> languageMap;

    @Actions({
        @Action(
        value="/sample2", 
        results={
        @Result(name="success",type="json")
        }) 
    })
    public String execute() {
        
      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++"));

      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 class ListValue {
    private String myKey;
    private String myValue;

    public ListValue(String myKey, String myValue) {
      super();
      this.myKey = myKey;
      this.myValue = myValue;
    }

    public String getMyKey()
    {
      return myKey;
    }

    public void setMyKey(String myKey)
    {
      this.myKey = myKey;
    }

    public String getMyValue()
    {
      return myValue;
    }

    public void setMyValue(String myValue)
    {
      this.myValue = myValue;
    }
  }

}

Action Sample2 returns this json:

{"JSON":"success","languageList":["Java","PHP","C++"],"languageMap":{"P":"PHP","C":"C++","J":"Java"},"languageObjList":[{"myKey":"J","myValue":"Java"},{"myKey":"P","myValue":"PHP"},{"myKey":"C","myValue":"C++"}]}

Solution

  • Solved.

    May this be of some help to others,

    All I had to do was change the web.xml as below:

     <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <session-config>
    

    Earlier I had set the filter as

      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
      </filter-mapping>
      <session-config>
    

    So the struts2-jquery tag could not find the corresponding .js files

    Solution: change from <url-pattern>*.action</url-pattern> to <url-pattern>/*</url-pattern> Thus, please be sure to check the web.xml for such anomalies which often go overlooked as in my case.