全国免费服务热线:

18982081108
✕

成都弹性云服务器,成都高防云服务器,互联网数据服务,成都祈钰瑶

struts与datagrid显示数据-创新互联

一、创建Web工程
   工程名称:sajdemo
二、添加jar包支持
   --struts-2.8.7.jar
   commons-fileupload-1.2.2.jar
   commons-io-2.0.1.jar
   commons-lang3-3.1.jar
   freemarker-2.3.19.jar
   struts2-core-2.3.7.jar
   ognl-3.0.5.jar
   xwork-core-2.3.7.jar
   javassist-3.11.0.GA.jar
   --json.jar   
   json-lib-2.3-jdk15.jar
   struts2-json-plugin-2.3.7.jar
   commons-lang-2.4.jar
   ezmorph-1.0.6.jar
   commons-beanutils-1.8.0.jar
三、添加配置文件与修改web.xml文件
   web.xml配置
   <?xml version="1.0" encoding="UTF-8"?>
   <web-app version="2.5" 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_2_5.xsd">
      <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>
      <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
   </web-app>
   struts.xml配置
   <?xml version="1.0" encoding="UTF-8" ?>
   <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
      "http://struts.apache.org/dtds/struts-2.3.dtd">
   
   <struts>
      <constant name="struts.devMode" value="true" />
      <package name="data" namespace="/" extends="json-default">
         <action name="orderItemAction_*" class="cn.jbit.sajdemo.web.action.OrderItemAction" method="{1}">
            <result name="success" type="json">
               <param name="root">result</param>
            </result>
         </action>
      </package>
   </struts>
四、创建Action与javabean
   1.在src下创建包
      包名:cn.jbit.sajdemo.domain
      包名:cn.jbit.sajdemo.web.action
   2.在包下创建Action与javabean
      javabean:
      public class Product {
         private String productId;
         private String productName;
         private Double productPrice;
         //省略get and set
      }
      
      public class OrderItem {
         private String itemId;
         private Integer count;
         private Product product;
         //省略get and set
      }
      
      public class OrderItemAction extends ActionSupport {
         private JSONObject result;
         public String list(){
            //创建产品
            Product p1 = new Product();
            p1.setProductId("p123");
            p1.setProductName("苹果手机");
            p1.setProductPrice(3000d);
            
            Product p2 = new Product();
            p2.setProductId("p124");
            p2.setProductName("三星手机");
            p2.setProductPrice(3000d);
            //创建订单项
            OrderItem oi1 = new OrderItem();
            oi1.setCount(10);
            oi1.setItemId("o123");
            oi1.setProduct(p1);
            
            OrderItem oi2 = new OrderItem();
            oi2.setCount(20);
            oi2.setItemId("o124");
            oi2.setProduct(p2);
            
            
            //创建集合并添加订单项
            List<OrderItem> orderItems = new ArrayList<OrderItem>();
            orderItems.add(oi1);
            orderItems.add(oi2);
            
            
            //将集合添加到Map中
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("rows", orderItems);
            
            
            //将Map转换为Json格式
            result = JSONObject.fromObject(map);
            
            //输出Json后的格式数据
            System.out.println(result);
            return SUCCESS;
         }
         //省略get and set
      }
五、添加EasyUI支持
   jquery-easyui-1.3.2
六、视图
   <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
   <%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
   %>
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
   <html>
    <head>
      <base href="<%=basePath%>">
      <title>My JSP 'index.jsp' starting page</title>
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">   
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
       <link rel="stylesheet" href="jquery-easyui-1.3.2/themes/default/easyui.css" type="text/css"></link>
      <link rel="stylesheet" href="jquery-easyui-1.3.2/themes/icon.css" type="text/css"></link>
      <script type="text/javascript" src="jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>
      <script type="text/javascript" src="jquery-easyui-1.3.2/jquery.easyui.min.js"></script>  
   </head>
    <body>
      <table id="dg"></table>  
    </body>
    <script type="text/javascript">
       $('#dg').datagrid({   
         url:'orderItemAction_list.action',   
         columns:[[   
             {field:'count',title:'商品数量',width:100},   
             {field:'itemId',title:'订单项编号',width:100},   
             {field:'product',title:'商品名称',formatter:function(value){
                return value.productId;
             },width:100}   
         ]]   
      });
    </script>
   </html>
   struts与datagrid显示数据

创新互联主营赣县网站建设的网络公司,主营网站建设方案,app软件定制开发,赣县h5重庆小程序开发公司搭建,赣县网站营销推广欢迎赣县等地区企业咨询

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。