日期:2014-05-16 浏览次数:20844 次
前面记录了实现动态下拉菜单标签的过程,这仅仅完成了级联的第一步,这里将描述第二步,级联变化的实现。
要实现两个输入的变化:接口版本和接口参数,我的设想是调用两次ajax与后台交互获取数据,然后再重新渲染页面。但我又比较懒惰,不想为每个ajax都实现一个servlet,而且那样写的话也显得代码冗余了。我的设想是实现一个servlet,这个servlet根据接受请求的参数,来判断需要调用某个类的某个方法进行处理。一种较为常见的做法是在servlet里写一些if...else之类的条件语句来完成这个判断过程。我又觉得这个做法比较呆板,代码耦合太高,如果新增一个功能点都需要修改servlet。所幸,受struts的启发,我使用发射机制实现了这个自动寻找调用方法的过程。
一、servlet的实现
package com.vness.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.vness.tool.actionDealer; public class dataServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); String param = request.getParameter("param"); String action = request.getParameter("action"); String xmlData= actionDealer.doAction(action, param); PrintWriter out = response.getWriter(); out.write(xmlData); out.close(); } }
二、actionDealer类
这个类使用到了反射,这个类的功能完全可以在servlet里实现,但那样做不符合MVC的思想。整个类就做一件事情,利用发射转发请求到后台处理。
package com.vness.tool; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import com.vness.data.apiInfo; public class actionDealer { public actionDealer(){} public static String doAction(String action, String param) { apiInfo api = new apiInfo(); Class apiClass = api.getClass(); String xmlData = ""; try { Method callMethod = apiClass .getDeclaredMethod(action, String.class); xmlData = (String) callMethod.invoke(api, param); } catch (SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (NoSuchMethodException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return xmlData; } }
package com.vness.data; import java.sql.ResultSet; import com.vness.tool.dbTool; public class apiInfo { //作为数据载体,返回到servlet private String xmlData; private String apiJrno; private static String XML_LABLE_PRE="<display_value>"; private static String XML_LABLE_AFT="</display_value>"; public static String VERSQL_PRE="SELECT DISTINCT API_VER FROM MPOPTAPII WHERE API_ID= '"; public static String VERSQL_AFT="'"; public String getXmlData() { return xmlData; } public void setXmlData(String xmlData) { this.xmlData = xmlData; } public String getApiJrno() { return apiJrno; } public void setApiJrno(String apiJrno) { this.apiJrno = apiJrno; } //生成查询API版本的SQL public String createVerSql() throws Exception{ if(apiJrno==null||apiJrno==""){ throw new Exception("[ apiInfo::createVerSql ] api Jrno is null."); } return VERSQL_PRE+apiJrno+VERSQL_AFT; } private String getVerData(String api_id) throws Exception{ setApiJrno(api_id); String sql=createVerSql(); dbTool dt= new dbTool(); dt.