我想从Struts2
我的 jsp 页面中的 Action 类中检索字段。
我的 JSP JavaScript 代码能够触发它的动作类,但在通过 JavaScript 调用假定元素的 Id 时,不显示从动作类到 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>
在struts.xml
:
<package name="jsonpack" namespace="/" extends="json-default">
<action name="getfield" class="com.mobile.TestDropDown">
<result type="json" name="success"></result>
</action>
</package>
动作类:
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;
}
}