// AJAX根据父类查子类 if (ac.equals("searchsontype")) { String xml_start = "<selects>"; String xml_end = "</selects>"; String xml = ""; String fprotype = request.getParameter("fprotype"); ArrayList<HashMap> list = (ArrayList<HashMap>) dao .select("select * from protype where fatherid='" + fprotype + "' and delstatus='0' "); if (list.size() > 0) { for (HashMap map : list) { xml += "<select><value>" + map.get("id") + "</value><text>" + map.get("typename") + "</text><value>" + map.get("id") + "</value><text>" + map.get("typename") + "</text></select>"; } } String last_xml = xml_start + xml + xml_end; response.setContentType("text/xml;charset=GB2312"); response.setCharacterEncoding("utf-8"); response.getWriter().write(last_xml); response.getWriter().flush(); }
时间: 2023-12-22 14:05:49 浏览: 27
这段代码是一个Ajax请求处理的代码块。当请求参数`ac`的取值为`searchsontype`时,它会根据父类id查询子类列表,并以XML形式返回。具体的处理流程如下:
1. 定义XML的开始标记和结束标记,分别是`"<selects>"`和`"</selects>"`。
2. 从请求参数中获取父类id,即`fprotype`。
3. 使用DAO类执行SQL语句,查询所有父类id为`fprotype`且未删除的子类列表。
4. 如果查询结果不为空,则遍历子类列表,将每个子类的id和名称拼接成一个XML的`<select>`节点。
5. 将所有`<select>`节点拼接成完整的XML字符串。
6. 将XML开始标记、XML内容和XML结束标记拼接成完整的XML字符串。
7. 设置响应的内容类型为"text/xml;charset=GB2312",并将字符编码设置为"utf-8"。
8. 将XML字符串写入响应的输出流中,并刷新输出流。
总之,这段代码的作用是通过Ajax请求查询子类列表,返回一个XML格式的数据,以便在页面上动态显示子类列表。
相关问题
inputNumber.jsp代码: <p>输入运算数、选择运算符</p > <form action="____(1)_______" method="post"><!—此处产生下拉运算符供用户选择 --> <input type="text" name="numberOne" /> <____(2)________ name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </_____(2)___________> <input type="text" name="numberTwo" /><br> <input type="___(3)________" value="计算"/></form> ComputerBean.java代码 package Bean; public class ComputerBean { ___(4)________ double num1,num2,result; ___(4)_______ String opt; //此处省略部分setter和getter方法 public double optResult(String opt,double num1,double num2){ if(opt.equals("+")){ result=num1+num2; } if(opt.equals("-")){ result=num1-num2; } if(opt.equals("*")){ result=num1*num2; } if(opt.equals("/")){ result=num1/num2; } __________(5)__________________; }} 选做:完成handleCompute和lookResult.jsp代码
inputNumber.jsp代码:
<p>输入运算数、选择运算符</p>
<form action="handleCompute.jsp" method="post">
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="numberOne" />
<input type="text" name="numberTwo" /><br>
<input type="submit" value="计算"/>
</form>
handleCompute.jsp代码:
<%@ page import="Bean.ComputerBean" %>
<%
double num1 = Double.parseDouble(request.getParameter("numberOne"));
double num2 = Double.parseDouble(request.getParameter("numberTwo"));
String opt = request.getParameter("operator");
ComputerBean computer = new ComputerBean();
double result = computer.optResult(opt, num1, num2);
session.setAttribute("result", result);
response.sendRedirect("lookResult.jsp");
%>
lookResult.jsp代码:
<%@ page import="Bean.ComputerBean" %>
<%
ComputerBean computer = new ComputerBean();
double result = (double)session.getAttribute("result");
%>
<p>计算结果为:<%=result%></p>
帮我审查一下下带private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) { Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>(); List<String> colName = new ArrayList<>(); colName.add(Constants.ProjectView.COL_NAME_EXPRESSION); colName.add(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE); colName.add(Constants.ProjectView.COL_NAME_TOTAL); colName.add(Constants.ProjectView.COL_NAME_PERCENT); colName.add(Constants.ProjectView.COL_NAME_UNIT); for(String name : colName) { String value = null; if (Constants.ProjectView.COL_NAME_EXPRESSION.equals(name)) { value = bidNodeViewDto.getExpressionName() + ""; }else if(Constants.ProjectView.COL_NAME_EXPERSSION_VALUE.equals(name)) { value = bidNodeViewDto.getExpressionVal() + ""; }else if(Constants.ProjectView.COL_NAME_PERCENT.equals(name)) { value = bidNodeViewDto.getPercentTotal() + ""; }else if(Constants.ProjectView.COL_NAME_TOTAL.equals(name)) { value = bidNodeViewDto.getAmount() + ""; }else if(Constants.ProjectView.COL_NAME_UNIT.equals(name)) { value = bidNodeViewDto.getUnitIndex() + ""; } String targetCode = targetCodeMap.get(name); if(Strings.isBlank(targetCode)) { continue; } ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult(); itemsCustomTargetResultMap.put(targetCode,itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null)); } return itemsCustomTargetResultMap; }
这段代码的主要作用是根据传入的参数`bidNodeViewDto`和`targetCodeMap`生成一个`Map`,其中`Map`的键值为`targetCode`,值为`ItemsCustomTargetResult`对象。
这个方法看起来没有太大问题,但是有一些可以改进的地方:
1. 在循环中,每次迭代都会新建一个`String`类型的变量`value`并初始化为`null`,这个可以放到循环外部先声明再初始化,避免每次都进行声明和初始化。
2. `if...else`嵌套语句比较多,可以考虑使用`switch`语句来代替。
3. 在循环中,如果`targetCode`为空字符串,则直接跳过当前迭代,没有任何处理。这个可以添加一个日志输出或者抛出异常来提醒调用者传入的参数有误。
下面是一种改进后的代码:
```
private Map<String, ItemsCustomTargetResult> getItemsCustomTargetResult(BidNodeViewDto bidNodeViewDto, Map<String, String> targetCodeMap) {
Map<String, ItemsCustomTargetResult> itemsCustomTargetResultMap = new HashMap<>();
List<String> colName = Arrays.asList(Constants.ProjectView.COL_NAME_EXPRESSION, Constants.ProjectView.COL_NAME_EXPERSSION_VALUE, Constants.ProjectView.COL_NAME_TOTAL, Constants.ProjectView.COL_NAME_PERCENT, Constants.ProjectView.COL_NAME_UNIT);
for (String name : colName) {
String value = null;
switch (name) {
case Constants.ProjectView.COL_NAME_EXPRESSION:
value = String.valueOf(bidNodeViewDto.getExpressionName());
break;
case Constants.ProjectView.COL_NAME_EXPERSSION_VALUE:
value = String.valueOf(bidNodeViewDto.getExpressionVal());
break;
case Constants.ProjectView.COL_NAME_TOTAL:
value = String.valueOf(bidNodeViewDto.getAmount());
break;
case Constants.ProjectView.COL_NAME_PERCENT:
value = String.valueOf(bidNodeViewDto.getPercentTotal());
break;
case Constants.ProjectView.COL_NAME_UNIT:
value = String.valueOf(bidNodeViewDto.getUnitIndex());
break;
default:
throw new IllegalArgumentException("Invalid column name: " + name);
}
String targetCode = targetCodeMap.get(name);
if (targetCode == null || targetCode.isEmpty()) {
// 日志输出或者抛出异常
continue;
}
ItemsCustomTargetResult itemsCustomTargetResult = new ItemsCustomTargetResult();
itemsCustomTargetResultMap.put(targetCode, itemsCustomTargetResult.obtainItemsCustomTargetResult(targetCode, null, value, null));
}
return itemsCustomTargetResultMap;
}
```