netbeans下实现ajax的传值(post传值)

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ui="http://www.sun.com/web/ui">
    <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
    <f:view>
        <ui:page binding="#{department$Page1.page1}" id="page1">
            <ui:html binding="#{department$Page1.html1}" id="html1">
                <ui:head binding="#{department$Page1.head1}" id="head1">
                    <ui:link binding="#{department$Page1.link1}" id="link1" url="/resources/stylesheet.css"/>
                    <script type="text/javascript">
                         var xmlHttp;
                         function creatXMLHttpRequest() {
                            if (window.ActiveXObject) {
                            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            else if (window.XMLHttpRequest) {
                                 xmlHttp = new XMLHttpRequest();
                            }
                            else {
                                return;
                              }
                            }

                          //提交部门下拉框选择内容
                         function select_change(){
                              var depart = "depart=" + document.getElementById("form1:dropDown1").value;
                              if(depart != ""){
                                     creatXMLHttpRequest();
                                     var url = "/HRMS/faces/department/Page1.jsp";
                                     xmlHttp.open("POST",url,true);
                                     xmlHttp.onreadystatechange = showMembers;
                                     xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                     xmlHttp.send(depart);
                              }
                         }
                         //根据部门返回的职工列表动态加载数据
                         function showMembers(){
                              if(xmlHttp.readystate == 4){
                                     if(xmlHttp.status == 200){
                                        var tmpTeam = xmlHttp.responseText;
                                        document.getElementById("form1:textArea1").value = xmlHttp.responseText;
                                        document.getElementById("form1:textArea1").innerText = xmlHttp.responseText;
                                        ClearOption(document.getElementById("form1:dropDown2"));
                                        var arrTmp = tmpTeam.split(",");
                                        for(var i = 0;i &lt;arrTmp.length;i++){
                                            var oOption = document.createElement("option");
                                            document.getElementById("form1:dropDown2").options.add(oOption);
                                            if(arrTmp[i].split(":")[0] == "" &amp;&amp; typeof (arrTmp[i].split(":")[1]) == "undefined"){
                                               ClearOption(document.getElementById("form1:dropDown2"));
                                               break;
                                            }else{
                                                oOption.innerText = arrTmp[i].split(":")[1];
                                                oOption.value = arrTmp[i].split(":")[0];
                                            }
                                        }
                                     }else{
                                        //alert("您请求的页面有异常");
                                     }
                              }else{
                                  //alert("信息没有返回请等等待");
                              }
                         }
                         function ClearOption(ob){
                            var l = ob.length;
                            for(var i = 1;i &lt; l;l--){ob.remove(i);}
                            }
                    </script>
                </ui:head>
                <ui:body binding="#{department$Page1.body1}" id="body1" style="-rave-layout: grid">
                    <ui:form binding="#{department$Page1.form1}" id="form1">
                        <ui:dropDown binding="#{department$Page1.dropDown1}" id="dropDown1" items="#{department$Page1.dropDown1DefaultOptions.options}"
                            onChange="select_change();" style="left: 120px; top: 72px; position: absolute; width: 120px"/>
                        <ui:dropDown binding="#{department$Page1.dropDown2}" id="dropDown2" items="#{department$Page1.dropDown2DefaultOptions.options}" style="left: 336px; top: 72px; position: absolute; width: 119px"/>
                        <ui:textArea binding="#{department$Page1.textArea1}" id="textArea1" style="height: 432px; left: 528px; top: 96px; position: absolute; width: 408px"/>
                    </ui:form>
                </ui:body>
            </ui:html>
        </ui:page>
    </f:view>
</jsp:root>

/*
 * Page1.java
 *
 * Created on 2007年11月19日, 下午5:20
 * Copyright Administrator
 */
package hrms.department;

import basedata.DbDepartMent;
import basedata.Team;
import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.DropDown;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.Page;
import com.sun.rave.web.ui.component.TextArea;
import com.sun.rave.web.ui.model.SingleSelectOptionsList;
import comm.BaseHibernateDAO;
import comm.Command;
import hrms.ApplicationBean1;
import hrms.RequestBean1;
import hrms.SessionBean1;
import java.io.IOException;
import java.util.ArrayList;
import javax.faces.FacesException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.SQLQuery;
import servers.DepartMentServer;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class Page1 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
   
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        departmentDataProvider2.setCachedRowSet((javax.sql.rowset.CachedRowSet)getValue("#{SessionBean1.departmentRowSet2}"));
        dropDown2DefaultOptions.setOptions(new com.sun.rave.web.ui.model.Option[] {new com.sun.rave.web.ui.model.Option("", "/u8bf7/u9009/u62e9")});
    }
   
    private Page page1 = new Page();
   
    public Page getPage1() {
        return page1;
    }
   
    public void setPage1(Page p) {
        this.page1 = p;
    }
   
    private Html html1 = new Html();
   
    public Html getHtml1() {
        return html1;
    }
   
    public void setHtml1(Html h) {
        this.html1 = h;
    }
   
    private Head head1 = new Head();
   
    public Head getHead1() {
        return head1;
    }
   
    public void setHead1(Head h) {
        this.head1 = h;
    }
   
    private Link link1 = new Link();
   
    public Link getLink1() {
        return link1;
    }
   
    public void setLink1(Link l) {
        this.link1 = l;
    }
   
    private Body body1 = new Body();
   
    public Body getBody1() {
        return body1;
    }
   
    public void setBody1(Body b) {
        this.body1 = b;
    }
   
    private Form form1 = new Form();
   
    public Form getForm1() {
        return form1;
    }
   
    public void setForm1(Form f) {
        this.form1 = f;
    }

    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }

    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }

    private DropDown dropDown2 = new DropDown();

    public DropDown getDropDown2() {
        return dropDown2;
    }

    public void setDropDown2(DropDown dd) {
        this.dropDown2 = dd;
    }

    private SingleSelectOptionsList dropDown2DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown2DefaultOptions() {
        return dropDown2DefaultOptions;
    }

    public void setDropDown2DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown2DefaultOptions = ssol;
    }

    private CachedRowSetDataProvider departmentDataProvider2 = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getDepartmentDataProvider2() {
        return departmentDataProvider2;
    }

    public void setDepartmentDataProvider2(CachedRowSetDataProvider crsdp) {
        this.departmentDataProvider2 = crsdp;
    }

    private TextArea textArea1 = new TextArea();

    public TextArea getTextArea1() {
        return textArea1;
    }

    public void setTextArea1(TextArea ta) {
        this.textArea1 = ta;
    }
   
    // </editor-fold>
   
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Page1() {
    }
   
    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     *
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        try{
            String teamTmp = "";
            HttpServletRequest request = (HttpServletRequest)this.getFacesContext().getExternalContext().getRequest();
            String depart = request.getParameter("depart");
            System.out.println(depart);
            HttpServletResponse res =  (HttpServletResponse)this.getFacesContext().getExternalContext().getResponse();
            DepartMentServer dms = new DepartMentServer();
            DbDepartMent departDB  = new DbDepartMent();
            String teamsql = "select * from team where departcode='"+ depart +"'";   
            BaseHibernateDAO teamdao = new BaseHibernateDAO();
            SQLQuery teamquery = teamdao.getSession().createSQLQuery(teamsql);
            teamquery.addEntity(Team.class);
            ArrayList<Team> idlist = (ArrayList<Team>) teamquery.list();
            if(depart != null){
                for(int i=0;i<idlist.size();i++){
                    if(teamTmp == ""){
                        teamTmp = String.valueOf(idlist.get(i).getId()) + ":" + idlist.get(i).getTeam();
                    }
                    else{
                         teamTmp += "," + String.valueOf(idlist.get(i).getId()) + ":" + idlist.get(i).getTeam();
                    }
                }
                res.setContentType("text/text;charset=UTF-8");
                res.getOutputStream().print(teamTmp);
                res.getOutputStream().flush();
                res.notify();
                res.getOutputStream().close();
          }
        }catch(Exception ex){
            System.out.println(ex);
        }
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
       
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
       
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }
   
    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    public void preprocess() {
    }
   
    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    public void prerender() {
        Command c = new Command ();
        this.dropDown1DefaultOptions.setOptions(c.DropList(this.departmentDataProvider2,"DEPARTNAME","请选择"));
    }
   
    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    public void destroy() {
        departmentDataProvider2.close();
    }

    /**
     * <p>返回对已限定范围的数据 Bean 的引用。</p>
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1)getBean("ApplicationBean1");
    }

    /**
     * <p>返回对已限定范围的数据 Bean 的引用。</p>
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1)getBean("RequestBean1");
    }

    /**
     * <p>返回对已限定范围的数据 Bean 的引用。</p>
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1)getBean("SessionBean1");
    }
  
}

(这个是一个部门和班组级联的例子) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值