如何使用json对象将数据插入表

时间:2014-08-29 08:38:41

标签: java xml json jsp struts2

我试图将某些数据添加到同一页面中的表格中。数据来自于。当我提交表单时,它将显示一个仅包含此内容的不同页面({"地址":" zcxx","名称":"悲伤&# 34;})。所以我无法确定应用程序中的错误。有人可以告诉我我做错了什么吗?这是我的代码。我在这个应用程序中使用struts2

web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <display-name>Struts2 Demo App</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@taglib  prefix="sj" uri="/struts-jquery-tags" %>
<%@taglib  prefix="sjg" uri="/struts-jquery-grid-tags" %>


<!DOCTYPE html PUBLIC3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/htme.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>


<s:form action="Viewact1">

    <s:textfield name="name" id="name" label="Your Name"/>
    <s:textfield name="address" id="address" label="Address"/>
    <s:submit label="submit" value="submit" id="submit" ></s:submit>   
</s:form>            
        <jsp:include page="/Styles.jsp" />

         <s:url var="updateuserurl" action="Viewact1"/>
        <sjg:grid
                                id="gridtable"
                                caption="Sample Table"
                                dataType="json"

                                href="%{updateuserurl}"
                                pager="true"
                                gridModel="gridModel"
                                rowList="10,15,20"
                                rowNum="10"
                                autowidth="true"
                                rownumbers="true"
                                onCompleteTopics="completetopics"
                                rowTotal="false"
                                viewrecords="true">

             <sjg:gridColumn name="name" index="name" title="name"  align="center" width="30"  sortable="true"/>
             <sjg:gridColumn name="address" index="address" title="address"  align="center" width="30"  sortable="true"/>

        </sjg:grid> 

</body>
</html>

struts.xml中

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<constant name="struts.devMode" value="true" /> 
<package name="basicstruts2" extends="struts-default,json-default" namespace="/">

    <action name="*act1" class="action.HWAction" method="View">     
        <result name="submit" type="json"></result>          
    </action>

Action类是HWAction.java

public class HWAction extends ActionSupport implements Action,ModelDriven<DataBean>{
DataBean ms = new DataBean();
InputBean msi = new InputBean();
//String result;

@Override
public String execute() {
    System.out.println("execute");
return SUCCESS;

}

@Override
public DataBean getModel() {
    System.out.println("getModel");        
    return ms;           
}

public String View(){

    System.out.println("Submit>"+msi);
    System.out.println("view");
    msi.getGridModel().add(ms);

    return "submit";
}

}

DataBean.java

public class DataBean {


private String name;
private String address;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}       
}

InptBean类

public class InputBean {

private String name;
private String address;

private List<DataBean> gridModel=new ArrayList<DataBean>();


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}


public List<DataBean> getGridModel() {
    return gridModel;
}

public void setGridModel(List<DataBean> gridModel) {
    this.gridModel = gridModel;
}

0 个答案:

没有答案