struts下实现ajax的实例

本文介绍如何在Struts框架中使用Ajax技术实现动态加载的级联下拉菜单功能。通过一个具体示例展示了如何根据用户选择的第一级选项来动态获取并填充第二级下拉菜单的选项。

 

http://www.it-eye.nl/weblog/2005/12/13/ajax-in-struts-implementing-dependend-select-boxes/

script

 

script:
<script language="javascript">
 var req;
 /*
  * Get the second options by calling a Struts action
  */
 function retrieveProjectOptions(){
   
    firstBox = document.getElementById('firstBox');
   
    //Nothing selected
    if(firstBox.selectedIndex==0){
      return;
    }
    selectedOption = firstBox.options[firstBox.selectedIndex].value;
    //get the (form based) params to push up as part of the get request
    url="retrieveProjectOptionsAjaxAction.do?selectedOption="+selectedOption;
 
    //Do the Ajax call
    if (window.XMLHttpRequest){ // Non-IE browsers
      req = new XMLHttpRequest();
      //A call-back function is define so the browser knows which function to call after the server gives a reponse back
      req.onreadystatechange = populateSecondBox;
      try {
          req.open("GET", url, true); //was get
      } catch (e) {
         alert("can not connect to server");
      }
      req.send(null);
    } else if (window.ActiveXObject) { // IE     
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
        req.onreadystatechange = populateSecondBox;
        req.open("GET", url, true);
        req.send();
      }
    }
  }
 
  //Callback function
  function populateSecondBox(){
      document.getElementById('secondBox').options.length = 0;

    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
         textToSplit = req.responseText;
        
         if(textToSplit == '803'){
            alert("No select option available on the server");
        }
          //Split the document
          returnElements=textToSplit.split("||");
          //Process each of the elements    
          for(var i=0;i<returnElements.length;i++){
             valueLabelPair = returnElements[i].split("|");
             document.getElementById('secondBox').options[i] = new Option(valueLabelPair[1], valueLabelPair[0]);
          }
        } else { 
            alert("Bad response by the server");
        }   
    }
    }
</script>

 

form

<html:form action="/addModule">
        小组 : <html:select property="group" onchange="retrieveProjectOptions()"
                styleId="firstBox" styleClass="mandatory">
                <html:option value="-1">请选择</html:option>
                <html:options collection="groups" labelProperty="groupName"
                    property="groupId" />
            </html:select>
            <html:errors property="group" />
            <br />
        所属项目 : <html:select property="project" styleId="secondBox"
                styleClass="mandatory">
                <html:option value="nothing">-First choose above-</html:option>
            </html:select>
            <html:errors property="project" />
            <br />
            模块名 : <html:text property="moduleName" />
            <html:errors property="moduleName" />
            <br />
            模块描述 : <html:text property="description" />
            <html:errors property="description" />
            <br />
            <html:hidden property="isSubmit" value="true" />

            <html:submit value="提交" />  
        </html:form>
 

struts config

 

struts config
<action path="/retrieveProjectOptionsAjaxAction"
            type="com.baidu.platform.project.action.RetrieveProjectOptionsAjaxAction">

        </action>

 

Action

 

package com.baidu.platform.project.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.baidu.platform.entities.ProjectDetail;
import com.baidu.platform.service.ProjectService;

public class RetrieveProjectOptionsAjaxAction extends Action {
    /**
     * This is the main action called from the Struts framework.
     *
     * @param mapping
     *            The ActionMapping used to select this instance.
     * @param form
     *            The optional ActionForm bean for this request.
     * @param request
     *            The HTTP Request we are processing.
     * @param response
     *            The HTTP Response we are processing.
     * @throws javax.servlet.ServletException
     * @throws java.io.IOException
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String selectedOption = request.getParameter("selectedOption");
        PrintWriter out = response.getWriter();
        // Check of het soortId wel correct is
        if ((selectedOption.trim().length() == 0)) {
            out.print("803");
        } else {
            List<ProjectDetail> options = ProjectService.getProjectsInGroup(Integer.parseInt(selectedOption));
            // Make a String representation where each option is seperated by '||' and a value and a label by '|'
            String outLine = makeOutputString(options);
            out.print(outLine);
            System.out.println(outLine);
        }
        return null;
    }

    private String makeOutputString(List<ProjectDetail> options) {
        String result = "";
        boolean first = true;
        for (ProjectDetail project : options) {
            if (!first) {
                result += "||";
            }
            first = false;
            result = result + project.getProjectId() + "|" + project.getProjectName();
        }
        return result;
    }

}
 
【顶级EI完美复现】电力系统碳排放流的计算方法【IEEE 14节点】(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完美复现】电力系统碳排放流的计算方法【IEEE 14节点】(Matlab代码实现)》的技术文档,核心内容是基于IEEE 14节点电力系统模型,利用Matlab实现碳排放流的精确计算方法。该方法通过建立电力系统中各节点的功率流动与碳排放之间的映射关系,实现对电能传输过程中碳足迹的追踪与量化分析,属于电力系统低碳调度与碳流管理领域的关键技术。文中强调“顶级EI完美复现”,表明其算法和仿真结果具有较高的学术严谨性和可重复性,适用于科研验证与教学演示。; 适合人群:电力系统、能源与动力工程、电气工程及其自动化等相关专业的研究生、科研人员以及从事电力系统低碳化、碳排放核算工作的技术人员。; 使用场景及目标:①用于电力系统碳排放流理论的学习与仿真验证;②支撑含新能源接入的电力系统低碳调度、碳交易、绿色电力溯源等课题的研究;③为撰写高水平学术论文(如EI/SCI期刊)提供可靠的代码基础和技术参考。; 阅读建议:读者应具备电力系统分析、Matlab编程的基础知识,建议结合电力系统潮流计算、节点导纳矩阵等前置知识进行学习,并通过调整系统参数和运行方式,深入理解碳排放流的分布规律与影响因素。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值