1

I want to retrieve fields from Struts2 Action class in my jsp page.

My JavaScript code of JSP is able to trigger it's action class, but not showing set field value from action class to jsp page on calling the Id of supposed element through JavaScript.

My console is showing everything fine. I have gone through various examples & don't know why I run into same problem all time. I'm not able to figure out the exact problem.

Here is my code:

In new.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>getJSON example</title>
 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>

<s:select label="Category" id="category" name="category" list="{'Select','Animal','Bird'}"></s:select><br/>

<s:textfield label="Field1" id="field1" name="field1" ></s:textfield>

<script type="text/javascript">
$(document).ready(function(){
$(document).on('change','#category',function(){
var JScategory=$(this).val();
$.getJSON("getfield",
{category:JScategory},
function(data){
$('#field1').html(field);
});
}
);

});
</script>
</body>
</html>

In struts.xml:

 <package name="jsonpack" namespace="/" extends="json-default">
 
<action name="getfield" class="com.mobile.TestDropDown">
<result type="json" name="success"></result>
</action>

</package>

Action Class:

package com.mobile;

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

import com.db.AdDAO;
import com.opensymphony.xwork2.ActionSupport;

public class TestDropDown  extends ActionSupport{
    

        private String category;
        private String field;

        public String getCategory() {
        return category;
        }

        public void setCategory(String category) {
        this.category = category;
        }

        public String getField() {
        return field;
        }

        public void setField(String field) {
        this.field = field;
        }
@Override
public String execute() throws Exception {
System.out.println("cat:"+category);
if(category.equals("Animal")){
field="Tiger";
}else if(category.equals("Bird")){
field="Eagle";
}
return SUCCESS;
}
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Why are you using textfield? Do you want to show tiger in textbox? – prem30488 Jun 29 '14 at 12:20
  • Please refer this example to load dynamic content. http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts-2-and-ajax/ http://tech.learnerandtutor.com/dynamic-drop-down-list-with-struts2-ui-tag-without-ajax-jquery/ – Rajeshkumar Jul 02 '14 at 04:26

5 Answers5

0

I don't see where you are initializing your field JS variable in the line:

$('#field1').html(field);

What is the value of field here?

rob
  • 1,286
  • 11
  • 12
0

Every thing is ok just Use this

 $('#field1').val(data.field);

Instead of

 $('#field1').html(field);

Because the json returned with url

http://localhost:8083/yourProjectName/getfield?category=Bird is

{"category":"Bird","field":"Eagle"}

So to get Eagle in textbox we access variable data.field

 <script type="text/javascript">
      $(document).ready(function(){
           $(document).on('change','#category',function(){
                var JScategory=$(this).val();
                $.getJSON("getfield",
                {category:JScategory},
                function(data){
                     $('#field1').val(data.field);
                });
                }
           );
      });
 </script>

Also let us know if you get any error or it is not working.

prem30488
  • 2,828
  • 2
  • 25
  • 57
  • No, it's not solving anything. – Kumar Chandra Vart Jun 29 '14 at 13:07
  • The same code you posted is running in my pc. Instead of `$('#field1').html(field);` I changed it to `$('#field1').val(data.field);` and I am getting Tiger and Eagle in textbox in my pc. What's wrong in your code? – prem30488 Jun 29 '14 at 19:05
  • Make sure you have not changed code. Make sure you are using the same code as posted in this question. Because I have tested it and it is working in my pc. Try posting the url in browser and see json data. post it here what `json` data you've got. – prem30488 Jun 29 '14 at 19:16
0

The value from the category you should get as

var JScategory=$("#category").val();

and use

$.getJSON("<s:url namespace='/' action='getfield'/>", { category: JScategory })
.done(function(json) {
  console.log( "JSON Data: " + json );
  $("#field1").val(json.field);
})
.fail(function( jqxhr, textStatus, error ) {
  var err = textStatus + ", " + error;
  console.log( "Request Failed: " + err );
});
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

it return json Object,so,you should do following steps,it works fine.

json Object contains key and value,so to need to iterate the your data.then set into your field.

     function(data) {
       //it is Json Data
        $.each(data,function(key,value){
               $('#field1').val(value);
          });
      });
hari
  • 1,874
  • 1
  • 16
  • 10
0

Are you missing your action suffix ? The default is .action

Try

$.getJSON("getfield.action",
     {category:JScategory},
      function(data){
        $('#field1').html(field);
 });
Quincy
  • 4,393
  • 3
  • 26
  • 40