Struts and DataGrid display data

Source: Internet
Author: User

1. Create a WEB Project
Project name: sajdemo
Ii. Support for adding jar packages
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
3. Add a configuration file and modify a web. xml file
Web. xml configuration
<? 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 configuration
<? 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>
4. Create action and JavaBean
1. Create a package under SRC
Package name: CN. jbit. sajdemo. Domain
Package name: CN. jbit. sajdemo. Web. Action
2. Create action and JavaBean under the package
JavaBean:
Public class product {
Private string productid;
Private string productname;
Private double productprice;
// Omit get and set
}

Public class orderitem {
Private string Itemid;
Private integer count;
Private product;
// Omit get and set
}

Public class orderitemaction extends actionsupport {
Private jsonobject result;
Public String list (){
// Create a product
Product p1 = new product ();
P1.setproductid ("p123 ");
P1.setproductname ("iPhone ");
P1.setproductprice (3000d );

Product P2 = new product ();
P2.setproductid ("p124 ");
P2.setproductname ("Samsung mobile ");
P2.setproductprice (3000d );
// Create order item
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 );


// Create a set and add order items
List <orderitem> orderitems = new arraylist <orderitem> ();
Orderitems. Add (oi1 );
Orderitems. Add (oi2 );


// Add the set to map
Map <string, Object> map = new hashmap <string, Object> ();
Map. Put ("rows", orderitems );


// Convert map to JSON format
Result = jsonobject. fromobject (MAP );

// Output JSON format data
System. Out. println (result );
Return success;
}
// Omit get and set
}
5. Add easyui support
Jquery-easyui-1.3.2
Vi. View
<% @ 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: 'item qty ', width: 100 },
{Field: 'itemid', Title: 'order item No. ', width: 100 },
{Field: 'product', Title: 'item name', formatter: function (value ){
Return Value. productid;
}, Width: 100}
]
});
</SCRIPT>
</Html>

This article from the "Su Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1571537

Struts and DataGrid display data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.