Java 如何在struts 2应用程序中使用datatables获取搜索结果并呈现它们
我有一个struts 2应用程序,我想在其中使用datatables来获取搜索结果并显示它们。屏幕如下所示Java 如何在struts 2应用程序中使用datatables获取搜索结果并呈现它们,java,jquery,struts2,datatables,jquery-datatables,Java,Jquery,Struts2,Datatables,Jquery Datatables,我有一个struts 2应用程序,我想在其中使用datatables来获取搜索结果并显示它们。屏幕如下所示 <action name="writeJSON" class="com.rajesh.json.ReadJSON" method="writeJSON"> <result type="json" /> 我希望它最终看起来像这样,并且我希望使用数据表从ajax调用中获得结果 我有一个action类(Dashboard.java),它有一个名为viewSearch
<action name="writeJSON" class="com.rajesh.json.ReadJSON" method="writeJSON">
<result type="json" />
我希望它最终看起来像这样,并且我希望使用数据表从ajax调用中获得结果
我有一个action类(Dashboard.java),它有一个名为viewSearchResult()的方法,如下所示
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.schenker.ocean.dao.SearchUpdateDAO;
import com.schenker.ocean.vo.Shipment;
public class Dashboard extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
//members
private String stt;
private String hawb;
private String chi;
private String invoice;
private String shipment;
private String Search;
private String tabIndex;
private String searchType;
private List<Shipment> aaData;
public String getTabIndex() {
return tabIndex;
}
public void setTabIndex(String tabIndex) {
this.tabIndex = tabIndex;
}
public String getSearch() {
return Search;
}
public void setSearch(String search) {
Search = search;
}
//getters and setters for members
public String getStt() {
return stt;
}
public void setStt(String stt) {
this.stt = stt;
}
public String getHawb() {
return hawb;
}
public void setHawb(String hawb) {
this.hawb = hawb;
}
public String getChi() {
return chi;
}
public void setChi(String chi) {
this.chi = chi;
}
public String getInvoice() {
return invoice;
}
public void setInvoice(String invoice) {
this.invoice = invoice;
}
public String getShipment() {
return shipment;
}
public void setShipment(String shipment) {
this.shipment = shipment;
}
//methods
public String getDashboard(){
return "success";
}
public String viewSearchResult(){
SearchUpdateDAO sudao= new SearchUpdateDAO();
aaData=sudao.getViewSearch(chi, stt, hawb, invoice, shipment, "");
return "success";
}
public String assignSearchResult(){
return "success";
}
public String shipDocsResult(){
return "success";
}
public String assignUpdateResult(){
return "success";
}
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
public List<Shipment> getAaData() {
return aaData;
}
public void setAaData(List<Shipment> aaData) {
this.aaData = aaData;
}
}
import java.util.List;
导入com.opensymphony.xwork2.ActionSupport;
导入com.schenker.ocean.dao.SearchUpdateDAO;
进口com.schenker.ocean.vo.shipping;
公共类Dashboard扩展了ActionSupport{
/**
*
*/
私有静态最终长serialVersionUID=1L;
//成员
私有字符串stt;
私有字符串hawb;
私人串池;
私人字符串发票;
私人字符串装运;
私有字符串搜索;
私有字符串索引;
私有字符串搜索类型;
私人名单数据;
公共字符串getTabIndex(){
返回索引;
}
public void setTabIndex(字符串tabIndex){
this.tabIndex=tabIndex;
}
公共字符串getSearch(){
返回搜索;
}
公共无效集合搜索(字符串搜索){
搜索=搜索;
}
//成员的getter和setter
公共字符串getStt(){
返回stt;
}
公共无效设置TT(字符串stt){
this.stt=stt;
}
公共字符串getHawb(){
返回hawb;
}
公共无效setHawb(字符串hawb){
this.hawb=hawb;
}
公共字符串getChi(){
回气;
}
公共无效集(字符串集){
this.chi=chi;
}
公共字符串getInvoice(){
退货发票;
}
公共发票(字符串发票){
本发票=发票;
}
公共字符串getShipping(){
退货;
}
公共无效集合装运(连续装运){
本次装运=装运;
}
//方法
公共字符串getDashboard(){
返回“成功”;
}
公共字符串viewSearchResult(){
SearchUpdateDAO sudao=新的SearchUpdateDAO();
aaData=sudao.getViewSearch(chi、stt、hawb、发票、发货,“”);
返回“成功”;
}
公共字符串assignSearchResult(){
返回“成功”;
}
公共字符串shipDocsResult(){
返回“成功”;
}
公共字符串assignUpdateResult(){
返回“成功”;
}
公共字符串getSearchType(){
返回搜索类型;
}
public void setSearchType(字符串搜索类型){
this.searchType=searchType;
}
公共列表getAaData(){
返回数据;
}
公共无效setAaData(列表aaData){
this.aaData=aaData;
}
}
下面是我的jsp页面(左侧还有搜索菜单)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=UTF-8">
<link rel="stylesheet" type="text/css" HREF="css/styles.css">
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript">
/* $(document).ready(function() { */
function postJSONData(){
var view = $('#view').val();
var assign = $('#assign').val();
var search=$('').val();
var stt=$('[name="stt"]').val();
var hawb=$('[name="hawb"]').val();
var chi=$('[name="chi"]').val();
var invoice=$('[name="invoice"]').val();
var shipment=$('[name="shipment"]').val();
$('#viewTable').dataTable( {
} );
};
/* }); */
</script>
<title></title>
<style type="text/css" media="screen">
body {
font-family: verdana, sans-serif;
font-size: 12px;
}
#header {
/* background: #ccc; */
padding: 0px;
}
#sidebar {
float: left;
width: 20%;
background: #C7C7C7;
}
#content {
margin-left: 22%;
}
#footer {
clear: right;
/* background: #eee; */
padding: 0px;
}
</style>
</head>
<body bgcolor="#ffffff" style="border: none; padding: 0px;">
<div id="header">
<table width="100%" height="20%">
<tr>
<td width="50%" height="1"
style="font-family: arial; font-size: 15px;" id="ezlogowrap" colspan="5"><img
src="images/ez-view-logo.png"></td>
<td width="50%" valign="top" style="padding-right: 15px;" colspan="5"><div
id="logowrap" align="right">
<img src="images/DbSchenkerLogo.gif" alt="DB Schenker logo">
</div></td>
</tr>
<!-- <tr><td colspan="10" height="25%"></td></tr> -->
<tr>
<td colspan="10"
style="background: #000066; width: 100%; height: 36px;"> </td>
</tr>
<tr><td colspan="10" height="25%"></td></tr>
</table>
</div>
<div id="sidebar">
<table width="20%" align="left">
<s:form name="searchForm" action="getDashboard" method="post">
<tr>
<th align="center" colspan="2">Search</th>
</tr>
<tr>
<td colspan="1"><s:textfield size="20" maxlength="20" name="stt" key="label.stt"></s:textfield> </td>
</tr>
<tr>
<td colspan="1"><s:textfield size="20" maxlength="20" name="hawb" key="label.hawb"></s:textfield> </td>
</tr>
<tr>
<td colspan="1"><s:textfield size="20" maxlength="20" name="chi" key="label.chi"></s:textfield> </td>
</tr>
<tr>
<td colspan="1"><s:textfield size="20" maxlength="20" name="invoice" key="label.invoice"></s:textfield> </td>
</tr>
<tr>
<td colspan="1"><s:textfield size="20" maxlength="20" name="shipment" key="label.shipment"></s:textfield> </td>
</tr>
<tr>
<td align="right" style="padding-top: 2px;" colspan="2"><s:submit key="label.search" onclick="postJSONData();" ></s:submit> </td>
</tr>
</s:form>
</table>
</div>
<div id="content">
<table align="left">
<tbody>
<tr>
<td colspan="8">
<div id="display jqueryDataTable">
<table id="viewTable">
<thead>
<tr>
<th>System</th>
<th>Doctype</th>
<th>STT</th>
<th>HAWB</th>
<th>CHI</th>
<th>Invoice</th>
<th>Shipment</th>
<th>Shipdate</th>
</tr>
</thead>
<tfoot>
<tr>
<th>System</th>
<th>Doctype</th>
<th>STT</th>
<th>HAWB</th>
<th>CHI</th>
<th>Invoice</th>
<th>Shipment</th>
<th>Shipdate</th>
</tr>
</tfoot>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="footer">
<table width="100%" height="20%">
<tr>
<td colspan="10"
style="background: #000066; width: 100%; height: 25px;"><div
align="center" class="style1"
style="font-size: 11px; color: #ffffff; font-family: Arial, Helvetica, sans-serif; padding-left: 5px;">DB
Schenker, Inc. © 2014</div></td>
</tr>
</table>
</div>
</body>
</html>
/*$(文档).ready(函数(){*/
函数postJSONData(){
var view=$('#view').val();
var assign=$('#assign').val();
var search=$('').val();
var stt=$('[name=“stt”]')。val();
var hawb=$('[name=“hawb”]')。val();
var chi=$('[name=“chi”]').val();
var发票=$('[name=“invoice”]')。val();
var shipping=$('[name=“shipping”]')。val();
$('#viewTable')。数据表({
} );
};
/* }); */
身体{
字体系列:verdana,无衬线;
字体大小:12px;
}
#标题{
/*背景:#ccc*/
填充:0px;
}
#边栏{
浮动:左;
宽度:20%;
背景:#C7C7C7;
}
#内容{
左边缘:22%;
}
#页脚{
清楚:对,;
/*背景:#eee*/
填充:0px;
}
搜寻
系统
Doctype
STT
哈布
芝加哥
发票联
装运
交货日期
系统
Doctype
STT
哈布
芝加哥
发票联
装运
交货日期
分贝
申克公司&复印件;2014
下面是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.enable.DynamicMethodInvocation" value="true" /> -->
<constant name="struts.devMode" value="true"/>
<constant name="struts.custom.i18n.resources" value="ApplicationResources"/>
<!-- <constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" /> -->
<package name="logindefault" extends="struts-default,json-default">
<action name="login" method="execute" class="com.schenker.ocean.actions.LoginAction">
<result name="success">/searchpage.jsp</result>
<result name="input">/login.jsp</result>
<result name="fail">/login.jsp</result>
</action>
</package>
<package name="default" extends="logindefault" namespace="/">
<action name="login" method="execute" class="com.schenker.ocean.actions.LoginAction">
<result name="success">/searchpage.jsp</result>
<result name="input">/login.jsp</result>
<result name="fail">/login.jsp</result>
</action>
<action name="getDashboard" method="getDashboard" class="com.schenker.ocean.actions.Dashboard">
<result name="success">/searchpage.jsp</result>
<result name="input">/login.jsp</result>
<result name="fail">/login.jsp</result>
</action>
<action name="viewSearchResult" method="viewSearchResult" class="com.schenker.ocean.actions.Dashboard">
<result name="success">/searchpage.jsp</result>
<result name="input">/login.jsp</result>
<result name="fail">/login.jsp</result>
</action>
</package>
</struts>
/searchpage.jsp
/login.jsp
/login.jsp
/searchpage.jsp
/login.jsp
/login.jsp
/searchpage.jsp
/login.jsp
/login.jsp
/searchpage.jsp
/login.jsp
/login.jsp
我的问题是如何让datatables调用viewSearchResult()并在searchform中传递参数?如何确保响应由datatables处理,而不是由struts转发到另一个页面