Struts2-Ajax无刷新显示信息,以本项目为例就是使用div标签来生成div元素,而该div元素并非是静态的内容,而是通过div标签中的href来动态的从服务器中获取div元素的内容。而div标签中的delay则是延迟加载的意思,本项目中是采用了延迟3秒加载。
项目需要的包文件:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
log4j-1.2.17.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
struts2-dojo-plugin-2.1.8.1.jar
xwork-2.1.2.jar
Struts2Test.java源码:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Struts2Test extends ActionSupport{
List<Object> list=new ArrayList<Object>();
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
for(int i=0;i<3;i++){
Map<String,Object> map=new HashMap<String, Object>();
map.put("userName", "admin"+i);
map.put("password", "123456"+i);
map.put("name", "zhangsan"+i);
list.add(map);
}
return "success";
}
}
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="default" extends="struts-default" namespace="/">
<action name="sa" class="com.test.Struts2Test" >
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
web.xml源码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- 下面的头信息不可或缺,否则程序会报错! -->
<!-- 不加下面的头信息,错误信息为:ReferenceError: djConfig is not defined -->
<s:head/>
<sx:head parseContent="true"/>
<!-- 上面的头信息不可或缺,否则程序会报错! -->
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:url id="te" value="/sa.action"></s:url>
<sx:div href="%{te}" delay="3000">
<div align="center">
<p>此处内容即将显示</p>
</div>
</sx:div>
</body>
</html>
success.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<s:head/>
<sx:head parseContent="true"/>
<base href="<%=basePath%>">
<title>SUCCESS</title>
</head>
<body>
<div align="center">
<table border="1">
<thead>
<tr>
<td>
<div>用户名</div>
</td>
<td>
<div>密码</div>
</td>
<td>
<div>真实姓名</div>
</td>
</tr>
</thead>
<tbody>
<s:iterator value="list">
<tr>
<td>
<div><s:property value="userName"/></div>
</td>
<td>
<div><s:property value="password"/></div>
</td>
<td>
<div><s:property value="name"/></div>
</td>
</tr>
</s:iterator>
</tbody>
</table>
</div>
</body>
</html>
运行的结果:
3秒后显示的结果: