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