环境struts2.1.8,用自带的那个struts2-json-plugin-2.1.8.1.jar,jso页面调试时发现可以得到action里的json对象,但就是jquery easy ui的datagird 里通过url访问都没执行action,直接跳到input 对应的页面,设了s:fielderror查看了下,说返回的rows没值,验证错误,怎么回事,jsp里没有输入啊
action代码
jsp页面代码
jsp页面执行查询按钮,firebug里可以看到action返回数据
{"code":null,"name":null,"page":0,"rows":[{"code":"xiang","id":1,"name":"箱","remark":null},{"code":"ping","id":2,"name":"瓶","remark":null},{"code":"he","id":3,"name":"盒","remark":null}],"total":0}
但刚载入页面的时候,
$(function(){
$('#datalist').datagrid({.....里这一段,url里设定的地址根本就没执行到action里面的方法,
<div style="color:red">
<s:fielderror />
</div>
调试时说 Invalid field value for field rows,action方法没执行当然为空了,它直接转到result里的input了。现在纳闷就是为什么会转,我jsp里只是显示一个datagrid,别的什么也没有啊,本来以为js有问题,瞅半天也没发现啊,什么原因啊???????
又试了试,发现action继承了actionSuppoet后有上面问题,去掉actionSuppoet就可以了,估计是actionSuppoet里校验出了问题,easyui的datagrid传给action的rows是int类型,但action定义了成了list,所以报类型转换错误,可这个grid插件得到返回的rows数据类型是json对象里的list,一种解决办法去掉变量rows,直接jsonObject里吧list给"rows"这个key,但我不想这样做,那应该怎么改啊?
action代码
- @Controller
- // 声明此类为控制层的类,且为prototype模式调用
- @Scope("prototype")
- @ParentPackage("json-default")
- @Results( {
- @Result(name = "success",type = "json"),
- @Result(name = "input",location = "/product/productUnitManage.jsp") })
- public class ProdUnitAction extends ActionSupport {
- private static final long serialVersionUID = -6595716080263610502L;
- private ScmProdUnit scmProdUnit ;
- private List<ScmProdUnit> rows =new ArrayList<ScmProdUnit>();;
- private int page;
- private int total;
- @Resource
- private ProdUnitService prodUnitService;
- public String save() throws Exception {
- this.prodUnitService.save(this.scmProdUnit);
- return "save";
- }
- public String update() throws Exception {
- this.prodUnitService.update(this.scmProdUnit);
- return "update";
- }
- public String delete() throws Exception {
- this.prodUnitService.delete(scmProdUnit);
- return "delete";
- }
- public String querybyID() throws Exception {
- scmProdUnit = prodUnitService.findById(ScmProdUnit.class, scmProdUnit
- .getId());
- return "item";
- }
- //@Action(value="querybyBuilde",results={@Result(type="json",name="success")})
- public String querybyBuilde() throws Exception {
- System.err.println("111111");
- StringBuffer sql = new StringBuffer();
- sql.append("from ScmProdUnit where 1=1 ");
- this.setRows(prodUnitService.findByConsion(sql.toString()));
- // this.setTotal(rows.size());
- System.err.println("2222222");
- return SUCCESS;
- }
- @JSON(serialize = false)
- public ScmProdUnit getScmProdUnit() {
- return scmProdUnit;
- }
- public void setScmProdUnit(ScmProdUnit scmProdUnit) {
- this.scmProdUnit = scmProdUnit;
- }
- public List<ScmProdUnit> getRows() {
- return rows;
- }
- public void setRows(List<ScmProdUnit> rows) {
- this.rows = rows;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- }
jsp页面代码
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>test</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <!-- <link rel="stylesheet" type="text/css" href="../_main.css" />-->
- <link rel="stylesheet" type="text/css" href="../js/themes/default/easyui.css">
- <link rel="stylesheet" type="text/css" href="../js/themes/icon.css">
- <script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
- <script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
- <script type="text/javascript" src="../js/locale/easyui-lang-zh_CN.js"></script>
- <script>
- $(function(){
- $('#datalist').datagrid({
- title:'单位列表',
- iconCls:'icon-ok',
- nowrap: false, //是否只显示一行,即文本过多是否省略部分。
- striped: true, //隔行变色
- singleSelect:true,
- url:'prod-unit!querybyBuilde.action',
- pagination:true,
- rownumbers: true,
- loadMsg:'数据加载中请稍后……',
- columns:[[
- {field:'itemid',checkbox:true},
- {field:'code',title:'单位编码',width:$(this).width()*0.4,align:'center'},
- {field:'name',title:'单位名称',width:$(this).width()*0.6,align:'center'}
- ]],
- toolbar: [{
- text: '添加',
- iconCls: 'icon-add',
- handler: function() {
- alert('添加数据')
- }
- }, '-', {
- text: '保存',
- iconCls: 'icon-save',
- handler: function() {
- alert('保存数据')
- }
- }]
- });
- });
- function getjsontest()
- {
- $.post("prod-unit!querybyBuilde.action",
- {
- },function(returnData,status)
- {
- if("success" == status)
- {
- alert("11111");
- //var dataa= eval('('+returnData+')');
- //alert(returnData.length);
- alert(returnData);
- }
- },"json"
- );
- }
- </script>
- </head>
- <body>
- <form id="myForm" method="post">
- <fieldset>
- <legend>
- test
- </legend>
- <table width="100%" border="0" cellpadding="1" cellspacing="4"
- class="tableStyle">
- <tr align="center">
- <td >
- <input name="btnQuery" type="button" onClick="getjsontest()"
- class="button" value="查 询">
- </td>
- </tr>
- </table>
- </fieldset>
- <table id="datalist"></table>
- <div id="result"></div>
- <div style="color:red">
- <s:fielderror />
- </div>
- </form>
- </body>
- </html>
jsp页面执行查询按钮,firebug里可以看到action返回数据
{"code":null,"name":null,"page":0,"rows":[{"code":"xiang","id":1,"name":"箱","remark":null},{"code":"ping","id":2,"name":"瓶","remark":null},{"code":"he","id":3,"name":"盒","remark":null}],"total":0}
但刚载入页面的时候,
$(function(){
$('#datalist').datagrid({.....里这一段,url里设定的地址根本就没执行到action里面的方法,
<div style="color:red">
<s:fielderror />
</div>
调试时说 Invalid field value for field rows,action方法没执行当然为空了,它直接转到result里的input了。现在纳闷就是为什么会转,我jsp里只是显示一个datagrid,别的什么也没有啊,本来以为js有问题,瞅半天也没发现啊,什么原因啊???????
又试了试,发现action继承了actionSuppoet后有上面问题,去掉actionSuppoet就可以了,估计是actionSuppoet里校验出了问题,easyui的datagrid传给action的rows是int类型,但action定义了成了list,所以报类型转换错误,可这个grid插件得到返回的rows数据类型是json对象里的list,一种解决办法去掉变量rows,直接jsonObject里吧list给"rows"这个key,但我不想这样做,那应该怎么改啊?