直接上代码:
<%@ 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"> <script type="text/javascript" src="jquery-1.11.1.min.js"></script> <script type="text/javascript" > $(function(){ $('#search').click(function(){ var _name=$(':input[name]').val(); $.post('<%=basePath%>demo/list?x='+_name+'&t='+new Date().getTime(),function(data){ var _html="<table><tr><th>aaa</th><th>bbb</th><th>ccc</th></tr>"; var _tr=""; $.each(data.list,function(i,e){ _tr+="<tr><td>"+e.x+"</td><td>"+e.y+"</td><td>"+e.z+"</td></tr>"; }); var _table=_html+_tr+"</table>"; $('#userlist').html(_table); $('table').css('border','1px').width(500); }); }); }); </script> </head> <body> <input type="text" name="name"/><button id="search">Search</button> <hr/> <div id="userlist"></div> </body> </html>
package hglq4.cn.eshop.action.demo; import hglq4.cn.eshop.vo.demo.DemoVO; import java.util.ArrayList; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") @ParentPackage("json-default") //@ParentPackage("struts-default") @Results({@Result(name="success",location="/index.jsp")}) //@Namespace("/s") public class DemoAction extends ActionSupport { private String x; private List<DemoVO> list=new ArrayList<DemoVO>(); public List<DemoVO> getList() { return list; } public String getX() { return x; } public void setX(String x) { this.x = x; } //@Action(value="add",results={@Result(name="success",location="/index.jsp")}) public String add(){ System.out.println("-------5678-----------"); return SUCCESS; } public String del(){ System.out.println("---------2-----------"); return SUCCESS; } @Action(value="list",results={@Result(name="success",type="json")}) public String list(){ System.out.println("---------list-----------"); if(x.equals("a")){ for(int i=0;i<10;i++){ DemoVO d=new DemoVO("1","2",i+++""); list.add(d); } }else{ for(int i=20;i<80;i++){ DemoVO d=new DemoVO("ad","ef",i+++""); list.add(d); } } return SUCCESS; } }
package hglq4.cn.eshop.vo.demo; public class DemoVO { private String x; private String y; private String z; public DemoVO(String i, String j, String k) { this.x=i; this.y=j; this.z=k; } public String getX() { return x; } public void setX(String x) { this.x = x; } public String getY() { return y; } public void setY(String y) { this.y = y; } public String getZ() { return z; } public void setZ(String z) { this.z = z; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--确定搜索包的路径。只要是结尾为action的包都要搜索。--> <constant name="struts.convention.package.locators" value="action" /> </struts>
<%@ 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" type="text/css" href="styles.css"> --> </head> <body> <%=new Date().toLocaleString() %> <br> </body> </html>