使用angular js将servlet中的数据显示到<select>标记中

时间:2016-02-10 06:49:55

标签: angularjs jsp servlets

我试图在select标签中显示数据,数据来自数据库。我使用servlet从数据库中获取数据。但是我没有得到如何从angularjs调用servlet并在select标签中显示该数据。 我在这里分享我的代码,请帮帮我。 servlet1.java代码: protected void doGet(HttpServletRequest request,HttpServletResponse response)抛出ServletException,IOException {     PrintWriter out = response.getWriter();     连接con = null;     尝试{         的Class.forName(&#34; com.mysql.jdbc.Driver&#34);         con = DriverManager.getConnection(&#34; jdbc:mysql:// localhost:3306 / apm&#34;,&#34; root&#34;,&#34; root12345&#34;);         Statement st = con.createStatement();         ResultSet rs;         rs = st.executeQuery(&#34;从机器中选择machine_name&#34;);         int i = 1;         String res =&#34; [&#34 ;, separator =&#34;&#34 ;;         while(rs.next()){                 res + = separator + rs.getString(1);                 separator =&#34;,&#34 ;;             }          res + =&#34;]&#34; ;          通过out.println(RES);     }} servlet2.java代码: 尝试{      的Class.forName(&#34; com.mysql.jdbc.Driver&#34);      con = DriverManager.getConnection(&#34; jdbc:mysql:// localhost:3306 / apm&#34;,&#34; root&#34;,&#34; root12345&#34;);         Statement st = con.createStatement();         ResultSet rs;         String start = request.getParameter(&#34; start&#34;);         String index = request.getParameter(&#34; index&#34;);         String tablename = request.getParameter(&#34; tablename&#34;);         if(start == null){             start =&#34; 0&#34 ;;         }         if(index == null){             index =&#34; 1&#34 ;;         }         if(tablename == null){             tablename =&#34; data&#34 ;;         }         String column =&#34;&#34 ;;         boolean first = true;         rs = st.executeQuery(&#34;显示来自&#34; + tablename的列);         int i = 1;         String res =&#34; [&#34 ;, separator =&#34;&#34 ;;         while(rs.next()){             res + = separator + rs.getString(1);             separator =&#34;,&#34 ;;         }         res + =&#34;]&#34; ;      通过out.println(RES);       } 角度js代码: myApp.controller(&#39; MyCtrl&#39;,[&#39; $ scope&#39;,function($ scope){         $ scope.blocs = [];         $ scope.fields = [];         $ scope.addNewField = function(index){             警报(索引);             $ scope.blocs [指数] .fields.push({                 传感器:&#34;&#34;,                 sensor1:&#34;&#34;,                 sensor2:&#34;&#34;             });         };         $ scope.addBloc = function(){             $ scope.blocs.push({                 字段:[]             });         };     } ]); jsp代码: &lt; div ng-controller =&#34; MyCtrl&#34;&gt;     &lt; button ng-click =&#34; addBloc()&#34;&gt;     添加机器     &LT; /按钮&GT;     &lt; div ng-repeat =&#34; blocs中的集团&#34;&gt;         &LT; DIV&GT;             &LT;选择&GT;                 &lt; option&gt; ---选择机器 - &lt; / option&gt;             &LT; /选择&GT;             &lt; button ng-click =&#34; addNewField($ index)&#34;&gt;                 添加传感器             &LT; /按钮&GT;         &LT; / DIV&GT;         &lt; div ng-repeat =&#34; bloc.fields&#34;&gt;中的字段             &lt; select ng-model =&#34; field.sensor&#34;&gt;                 &lt; option value =&#34; sensor1&#34;&gt; sensor1&lt; / option&gt;                 &lt; option value =&#34; sensor2&#34;&gt; sensor2&lt; / option&gt;             &LT; /选择&GT;             &lt; input type =&#34; text&#34; NG-模型=&#34; field.sensor1&#34;&GT;             &lt; input type =&#34; text&#34; NG-模型=&#34; field.sensor2&#34;&GT; {{field.sensor}} {{field.sensor1}} {{field.sensor2}}         &LT; / DIV&GT;     &LT; / DIV&GT; &LT; / DIV&GT; 在第一个选择标记中,我需要从第一个servlet代码(即servlet1.java)获取数据。 在第二个选择标记中,我需要从第二个servlet代码(即servlet2.java)获取数据。 请any1帮帮我...... :(

1 个答案:

答案 0 :(得分:2)

您可以使用$http service,例如:

var servlet_url = "...";
$http.get(servlet_url)
.then(function(response) {
    $scope.myList = response.data;
    $scope.data = { selected: $scope.myList[0] };
});

然后使用ngOptions directive<select>绑定到从servlet检索到的数据:

<select ng-model='data.selected' ng-options='item in myList'></select>