我使用的是struts+hibernate的框架。
例子的意思是:从页面一输入ID,到后台查询后,在页面二显示查询的结果。
代码如下:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import java.util.*;
import com.company.struts.form.*;
import com.company.hibernate.*;
public class StudyAction extends DispatchAction{
//方法一:
public ActionForward getById(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudyForm studyForm = (StudyForm) form;
try{
Short id= studyForm.getUserid();
Manager pe=new Manager();
ManagerDAO mdao=new ManagerDAO();
pe=mdao.findById(id);
studyForm.setUserid(pe.getUserid());
studyForm.setLoginid(pe.getLoginid());
studyForm.setPhone(pe.getPhone());
studyForm.setManagerid(pe.getManagerid());
request.setAttribute("Manager",pe);
}catch(Exception e){
e.printStackTrace();
}
return mapping.findForward("study");
}
//方法二:
public ActionForward getPepoleMessage(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
try{
StudyForm studyForm = (StudyForm) form;
String loginid=request.getParameter("loginid");
ManagerDAO mdao=new ManagerDAO();
Manager pe=new Manager();
List pepole=mdao.findByProperty("loginid",loginid);
for(int i=0;i<pepole.size();i++){
pe=(Manager)pepole.get(0);
}
request.setAttribute("mess",pe);
}catch(Exception e){
e.printStackTrace();
}
return mapping.findForward("message");//配置文件中指定为页面2。
}
}
页面1:
<body>
<table>
<tr>
<td>
<font size="+1"> please input your loginid to select your ID :</font>
</td>
<td>
<html:text property="loginid" οnblur="showDialog()"/>
</td>
</tr>
</table>
</body>
<script>
function showDialog()
{
var lid=window.document.getElementById("loginid");
var features = "dialogWidth:" + 800 + "px;dialogHeight:" + 600 + "px;scrollbars:yes;status:no;help:no;center:yes";
window.showModalDialog("study.do?method=getPepoleMessage&loginid="+lid.value,null,features);
}
</script>
页面2:
<body>
<table>
<tr>
<td>ID</td>
<td>name</td>
<td>phone</td>
<td>managerid</td>
</tr>
<tr>
<td><bean:write name="mess" property="userid"/></td>
<td><bean:write name="mess" property="loginid"/></td>
<td><bean:write name="mess" property="phone"/></td>
<td><bean:write name="mess" property="managerid"/></td>
</tr>
</table>
</body>