还是Struts2课程实训是留下的东西,marke一下
1,向项目中添加Dojo包 Dojo.jar
2,前台页面顶端添加<%@ taglib prefix="sx" uri="/struts-dojo-tags"%> 引入Ajax的标签
3,在页面的<head>标签中添加标签<sx:head/>,引入封装好的JavaScript函数
应用举例:
页面代码:
<div id="apDiv4" style="display:none">
<form name="form2" action="selbiyeInfo">
<strong>检索住宿信息:</strong>
<strong>学号:</strong>
<input type="text" name="xuehao" value="" style="width:100px" />
<sx:a href="selbiyeInfo" targets="biyeInfo">检索</sx:a> //这是Ajax事件部分 action为selbiyeInfo
</form>
<sx:div id="biyeInfo"></sx:div>//这是Ajax要更新的目标div
</div>
Action代码:
package com.Action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.entity.student;
import com.opensymphony.xwork2.ActionSupport;
public class SelBiyeInfo extends ActionSupport {
private String xuehao = null;
private String result = null;
private String rztime = null;
private String gylouname = null;
private int fjnum;
private int cwnum;
private int cwid;
private int fjid;
private int gylnum;
public List<student> studentlist;
public List<student> studentInfolist = new ArrayList<student>();
public List<student> getStudentlist() {
return studentlist;
}
public void setStudentlist(List<student> studentlist) {
this.studentlist = studentlist;
}
public int getCwnum() {
return cwnum;
}
public void setCwnum(int cwnum) {
this.cwnum = cwnum;
}
public String getGylouname() {
return gylouname;
}
public void setGylouname(String gylouname) {
this.gylouname = gylouname;
}
public int getFjnum() {
return fjnum;
}
public void setFjnum(int fjnum) {
this.fjnum = fjnum;
}
public String getRztime() {
return rztime;
}
public void setRztime(String rztime) {
this.rztime = rztime;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getXuehao() {
return xuehao;
}
public void setXuehao(String xuehao) {
this.xuehao = xuehao;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
Connection con = DatabaseConnection.getConnection();
Statement ps = con.createStatement();
String sql = "select * from xszs_tb where xuehao='"+xuehao+"'";
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
cwid = (rs.getInt("cwid"));
//注入
this.setRztime(rs.getString("rzdate"));
}
String sql1 = "select * from fjcw_tb where id="+cwid;
ResultSet rs1 = ps.executeQuery(sql1);
while (rs1.next()) {
fjid = (rs1.getInt("fjid"));
//注入
this.setCwnum(rs1.getInt("cwnum"));
}
String sql2 = "select * from gyfj_tb where id="+fjid;
ResultSet rs2 = ps.executeQuery(sql2);
while (rs2.next()) {
gylnum = rs2.getInt("gylnum");
//注入
this.setFjnum(rs2.getInt("fjnum"));
}
String sql3 = "select * from gylou_tb where num="+gylnum;
ResultSet rs3 = ps.executeQuery(sql3);
while (rs3.next()) {
//注入
this.setGylouname(rs3.getString("name"));
}
String sql4 = "select * from xsda_tb where xuehao='"+xuehao+"'";
ResultSet rs4 = ps.executeQuery(sql4);
while (rs4.next()) {
student stu = new student();
stu.setXuehao(rs4.getString("xuehao"));
stu.setName(rs4.getString("name"));
stu.setSex(rs4.getString("sex"));
stu.setXibie(rs4.getString("xibie"));
stu.setZhuanye(rs4.getString("zhuanye"));
stu.setLeibie(rs4.getString("leibie"));
stu.setNianji(rs4.getString("nianji"));
stu.setBiyenian(rs4.getString("biyenian"));
stu.setShenfenzheng(rs4.getString("shenfenzheng"));
studentInfolist.add(stu);
}
//注入
this.setStudentlist(studentInfolist);
return SUCCESS;
}
}
struts.xml配置如下:
<action name="selbiyeInfo" class="com.Action.SelBiyeInfo">
<result name="success">/biyeInfotable.jsp</result>
</action>
biyeInfotable.jsp代码(页面就是用表格迭代显示Action注入的数据,然后通过Ajax在目标的div中显示这个页面):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>毕业住宿信息</title>
</head>
<body>
<center>
<table width="880" border="1" align="center">
<tr align="center">
<td colspan="10"><strong>学生信息</strong></td>
</tr>
<tr align="center">
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>系别</td>
<td>专业</td>
<td>类别</td>
<td>年级</td>
<td>毕业年</td>
<td>身份证</td>
</tr>
<s:iterator value="studentlist" status="st">
<tr align="center">
<td><s:property value="xuehao"/></td>
<td><s:property value="name"/></td>
<td><s:property value="sex"/></td>
<td><s:property value="xibie"/></td>
<td><s:property value="zhuanye"/></td>
<td><s:property value="leibie"/></td>
<td><s:property value="nianji"/></td>
<td><s:property value="biyenian"/></td>
<td><s:property value="shenfenzheng"/></td>
</tr>
</s:iterator>
</table><br>
<table width="880" border="1" align="center">
<tr align="center">
<td colspan="4"><strong>住宿信息</strong></td>
</tr>
<tr align="center">
<td>公寓楼</td>
<td>房间号</td>
<td>床位号</td>
<td>入住时间</td>
</tr>
<tr align="center">
<td>${gylouname}</td>
<td>${fjnum}</td>
<td>${cwnum}</td>
<td>${rztime}</td>
</tr>
</table><br>
<form name="form1" action="biyeoperate">
<input type="hidden" name="xuehao" value=${xuehao}>
<input type="submit" name="remove" value="解除住宿信息"/>
</form>
</center>
</body>
</html>
OK,完毕,知识的价值在于分享,希望对你有所帮助
于2011年端午节