将数据从servlet显示到<;选择>;使用AngularJS标记
我试图在select标记中显示数据,数据来自数据库。我正在使用servlet从数据库中获取数据。但我不知道如何从angularjs调用servlet并在select标记中显示该数据 我在这里分享我的代码,请帮帮我 servlet1.java代码:将数据从servlet显示到<;选择>;使用AngularJS标记,angularjs,jsp,servlets,Angularjs,Jsp,Servlets,我试图在select标记中显示数据,数据来自数据库。我正在使用servlet从数据库中获取数据。但我不知道如何从angularjs调用servlet并在select标记中显示该数据 我在这里分享我的代码,请帮帮我 servlet1.java代码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Pri
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select machine_name from machines");
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}}
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
String start = request.getParameter("start");
String index = request.getParameter("index");
String tablename = request.getParameter("tablename");
if (start == null) {
start = "0";
}
if (index == null) {
index = "1";
}
if (tablename == null) {
tablename = "data";
}
String column = "";
boolean first = true;
rs = st.executeQuery("show columns from" + tablename );
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.blocs = [];
$scope.fields = [];
$scope.addNewField = function (index) {
alert(index);
$scope.blocs[index].fields.push({
sensor : "",
sensor1 : "",
sensor2 : ""
});
};
$scope.addBloc = function () {
$scope.blocs.push({
fields : []
});
};
}
]);
<div ng-controller="MyCtrl">
<button ng-click="addBloc()">
Add Machines
</button>
<div ng-repeat="bloc in blocs">
<div>
<select>
<option>---select machine--</option>
</select>
<button ng-click="addNewField($index)">
Add sensors
</button>
</div>
<div ng-repeat="field in bloc.fields">
<select ng-model="field.sensor">
<option value="sensor1">sensor1</option>
<option value="sensor2">sensor2</option>
</select>
<input type="text" ng-model="field.sensor1">
<input type="text" ng-model="field.sensor2"> {{field.sensor}} {{field.sensor1}} {{field.sensor2}}
</div>
</div>
</div>
servlet2.java代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select machine_name from machines");
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}}
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
String start = request.getParameter("start");
String index = request.getParameter("index");
String tablename = request.getParameter("tablename");
if (start == null) {
start = "0";
}
if (index == null) {
index = "1";
}
if (tablename == null) {
tablename = "data";
}
String column = "";
boolean first = true;
rs = st.executeQuery("show columns from" + tablename );
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.blocs = [];
$scope.fields = [];
$scope.addNewField = function (index) {
alert(index);
$scope.blocs[index].fields.push({
sensor : "",
sensor1 : "",
sensor2 : ""
});
};
$scope.addBloc = function () {
$scope.blocs.push({
fields : []
});
};
}
]);
<div ng-controller="MyCtrl">
<button ng-click="addBloc()">
Add Machines
</button>
<div ng-repeat="bloc in blocs">
<div>
<select>
<option>---select machine--</option>
</select>
<button ng-click="addNewField($index)">
Add sensors
</button>
</div>
<div ng-repeat="field in bloc.fields">
<select ng-model="field.sensor">
<option value="sensor1">sensor1</option>
<option value="sensor2">sensor2</option>
</select>
<input type="text" ng-model="field.sensor1">
<input type="text" ng-model="field.sensor2"> {{field.sensor}} {{field.sensor1}} {{field.sensor2}}
</div>
</div>
</div>
角度js代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select machine_name from machines");
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}}
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
String start = request.getParameter("start");
String index = request.getParameter("index");
String tablename = request.getParameter("tablename");
if (start == null) {
start = "0";
}
if (index == null) {
index = "1";
}
if (tablename == null) {
tablename = "data";
}
String column = "";
boolean first = true;
rs = st.executeQuery("show columns from" + tablename );
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.blocs = [];
$scope.fields = [];
$scope.addNewField = function (index) {
alert(index);
$scope.blocs[index].fields.push({
sensor : "",
sensor1 : "",
sensor2 : ""
});
};
$scope.addBloc = function () {
$scope.blocs.push({
fields : []
});
};
}
]);
<div ng-controller="MyCtrl">
<button ng-click="addBloc()">
Add Machines
</button>
<div ng-repeat="bloc in blocs">
<div>
<select>
<option>---select machine--</option>
</select>
<button ng-click="addNewField($index)">
Add sensors
</button>
</div>
<div ng-repeat="field in bloc.fields">
<select ng-model="field.sensor">
<option value="sensor1">sensor1</option>
<option value="sensor2">sensor2</option>
</select>
<input type="text" ng-model="field.sensor1">
<input type="text" ng-model="field.sensor2"> {{field.sensor}} {{field.sensor1}} {{field.sensor2}}
</div>
</div>
</div>
jsp代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select machine_name from machines");
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}}
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/apm", "root", "root12345");
Statement st = con.createStatement();
ResultSet rs;
String start = request.getParameter("start");
String index = request.getParameter("index");
String tablename = request.getParameter("tablename");
if (start == null) {
start = "0";
}
if (index == null) {
index = "1";
}
if (tablename == null) {
tablename = "data";
}
String column = "";
boolean first = true;
rs = st.executeQuery("show columns from" + tablename );
int i = 1;
String res = "[",separator = "";
while (rs.next()) {
res += separator + rs.getString(1) ;
separator = ",";
}
res += "]" ;
out.println(res);
}
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.blocs = [];
$scope.fields = [];
$scope.addNewField = function (index) {
alert(index);
$scope.blocs[index].fields.push({
sensor : "",
sensor1 : "",
sensor2 : ""
});
};
$scope.addBloc = function () {
$scope.blocs.push({
fields : []
});
};
}
]);
<div ng-controller="MyCtrl">
<button ng-click="addBloc()">
Add Machines
</button>
<div ng-repeat="bloc in blocs">
<div>
<select>
<option>---select machine--</option>
</select>
<button ng-click="addNewField($index)">
Add sensors
</button>
</div>
<div ng-repeat="field in bloc.fields">
<select ng-model="field.sensor">
<option value="sensor1">sensor1</option>
<option value="sensor2">sensor2</option>
</select>
<input type="text" ng-model="field.sensor1">
<input type="text" ng-model="field.sensor2"> {{field.sensor}} {{field.sensor1}} {{field.sensor2}}
</div>
</div>
</div>
添加机器
---选择机器--
添加传感器
传感器1
传感器2
{{field.sensor}{{field.sensor1}{{{field.sensor2}}
var servlet_url = "...";
$http.get(servlet_url)
.then(function(response) {
$scope.myList = response.data;
$scope.data = { selected: $scope.myList[0] };
});
然后使用以下命令将
绑定到从servlet检索的数据: