简体   繁体   English

$.getJSON 不从 Struts 2 Action Class 检索数据到 JSP Page

[英]$.getJSON not retrieving data from Struts 2 Action Class to JSP Page

I want to retrieve fields from Struts2 Action class in my jsp page.我想从我的 jsp 页面中的Struts2 Action 类中检索字段。

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.我的 JSP JavaScript 代码能够触发它的动作类,但在通过 JavaScript 调用假定元素的 Id 时,不会显示从动作类到 jsp 页面的设置字段值。

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 :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 :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;
}
}

I don't see where you are initializing your field JS variable in the line:我没有看到您在行中初始化field JS 变量的位置:

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

What is the value of field here?这里的field值是多少?

Every thing is ok just Use this一切都好就用这个

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

Instead of代替

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

Because the json returned with url因为 json 返回了 url

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

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

So to get Eagle in textbox we access variable data.field所以为了在文本框中获取Eagle ,我们访问变量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.如果您遇到任何错误或无法正常工作,也请告诉我们。

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 );
});

it return json Object,so,you should do following steps,it works fine.它返回json对象,因此,您应该执行以下步骤,它工作正常。

json Object contains key and value,so to need to iterate the your data.then set into your field. json 对象包含键和值,因此需要迭代您的数据。然后设置到您的字段中。

     function(data) {
       //it is Json Data
        $.each(data,function(key,value){
               $('#field1').val(value);
          });
      });

Are you missing your action suffix ?您是否缺少操作后缀? The default is .action默认为.action

Try尝试

$.getJSON("getfield.action",
     {category:JScategory},
      function(data){
        $('#field1').html(field);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM