action:
package com;
import java.rmi.RemoteException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import service.MyServiceStub;
import service.MyServiceStub.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.xml.internal.ws.client.RequestContext;
public class FindstudentsAction extends ActionSupport {
public String execute() throws Exception {
//设置远程服务调用地址
String target = "http://127.0.0.1:8080/axis2/services/MyService";
//List<Student> list=new ArrayList<Student>();
try {
//根据地址构造用户存根
MyServiceStub stub = new MyServiceStub(target);
//调用查询学生列表方法
MyServiceStub.QueryStudentsResponse qsr = stub.queryStudents();
//返回学生类型对象数组
Student[] sts = qsr.get_return();
//将数组输出至前台页面
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("sts",sts );
//ActionContext.getContext().getSession().put("sts", sts);
} catch (RemoteException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
}
jsp:
<%@ 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>
</head>
<body>
<table width="90%" border="1">
<tr>
<th>用户id</th>
<th>用户姓名</th>
<th>用户住址</th>
</tr>
<s:iterator value="#request.sts">
<tr>
<td align="center"><s:property value="id"/> </td>
<td align="center"><s:property value="name"/> </td>
<td align="center"><s:property value="address"/> </td>
</tr>
</s:iterator>
</table>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class FindstudentsAction extends ActionSupport {
public String execute() throws Exception {
//设置远程服务调用地址
String target = "http://127.0.0.1:8080/axis2/services/MyService";
//List<Student> list=new ArrayList<Student>();
try {
//根据地址构造用户存根
MyServiceStub stub = new MyServiceStub(target);
//调用查询学生列表方法
MyServiceStub.QueryStudentsResponse qsr = stub.queryStudents();
//返回学生类型对象数组
Student[] sts = qsr.get_return();
//将数组输出至前台页面
// HttpServletRequest request = ServletActionContext.getRequest();
// request.setAttribute("sts",sts );
ActionContext.getContext().getSession().put("sts", sts);
} catch (RemoteException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
}
jsp:
<%@ page language="java" import="java.util.*,java.util.List" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>
</head>
<body>
<c:forEach items="${sts}" var="student">
${student.number}
${student.name}
${student.tel}
${student.address} <br>
</c:forEach>
</body>
</html>