Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpring下拉列表填充_Java_Spring_Jsp - Fatal编程技术网

JavaSpring下拉列表填充

JavaSpring下拉列表填充,java,spring,jsp,Java,Spring,Jsp,我在SpringMVC中工作,我正在做一些jsp,在一个页面中显示多个下拉列表 我看到了一个使用以下示例显示从数据库中下拉列表的示例 <%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <jsp:useBean id="state" scope="session" class="src.StateDA

我在SpringMVC中工作,我正在做一些jsp,在一个页面中显示多个下拉列表

我看到了一个使用以下示例显示从数据库中下拉列表的示例

    <%@ page import="java.util.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <jsp:useBean id="state" scope="session" class="src.StateDAO"/>

    <html>
      <head>
      <title></title>
      </head>
      <body>
        <form id="test" method="POST" action="">
          <input name="state" type="radio" value="Australia" id="state-aus">Australia
          <input name="state" type="radio" value="NewZealand" id="state-new">NewZealand
          <input name="state" type="radio" value="India" id="state-oth"  >India
          <Select name="othStates" size="1" id="oth-states">
          <c:forEach items="${state.stateList}" var="st">
                <option value="1"><c:out value="${st.name}"/></option>
          </c:forEach>
          </select>
          <br>
          <input type="Submit" name="cmdSub" value="SUBMIT">
          <input type="Reset" name="cmdReset" value="RESET">
        </form>
      </body>
    </html>

澳大利亚
新西兰
印度


使用SpringMVC在jsp中获取下拉列表的这种方法正确吗?

我认为更好的选择是使用Spring标记进行jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...
    <form:select path="country">
        <form:option value="NONE" label="--- Select ---" />
        <form:options items="${countryList}" />
    </form:select>

...
请参见此处的完整示例:

编辑:

$('#stateSelect').change(function() {
    $.ajax({
        type:"GET",
        url : "/getCitiesForState",
        data : { state: $('#stateSelect').val()},
        success : function(data) {
            $('#citySelect').empty(); //remove all child nodes
            for(var i = 0; i < data.length; i++){
                var newOption = $('<option value=data[i].value>data[i].text</option>');
                $('#citySelect').append(newOption);
            }   
        },
        error: function() {
            alert('Error occured');
        }
    });
});
@Controller
public class HelloController{

   @RequestMapping("/getCitiesForState")
   @ResponseBody
   public List<City> printHello(@RequestParam long state) {
      List<City> cities = //get from the some repository by state
      return cities;
   }

}

public class City{
   private String value;
   private String text;
   //getters setters
}
$('#stateSelect')。更改(函数(){
$.ajax({
键入:“获取”,
url:“/getCitiesForState”,
数据:{state:$('#stateSelect').val()},
成功:功能(数据){
$('#citySelect').empty();//删除所有子节点
对于(变量i=0;i
在服务器端,您需要一个端点,该端点响应url(/getCitiesForState,在本例中为),并返回具有值和文本属性的对象列表

编辑(添加控件):

$('#stateSelect').change(function() {
    $.ajax({
        type:"GET",
        url : "/getCitiesForState",
        data : { state: $('#stateSelect').val()},
        success : function(data) {
            $('#citySelect').empty(); //remove all child nodes
            for(var i = 0; i < data.length; i++){
                var newOption = $('<option value=data[i].value>data[i].text</option>');
                $('#citySelect').append(newOption);
            }   
        },
        error: function() {
            alert('Error occured');
        }
    });
});
@Controller
public class HelloController{

   @RequestMapping("/getCitiesForState")
   @ResponseBody
   public List<City> printHello(@RequestParam long state) {
      List<City> cities = //get from the some repository by state
      return cities;
   }

}

public class City{
   private String value;
   private String text;
   //getters setters
}
@控制器
公共类Hello控制器{
@请求映射(“/getCitiesForState”)
@应答器
公共列表printHello(@RequestParam long state){
List cities=//按状态从某些存储库获取
回归城市;
}
}
公营城市{
私有字符串值;
私有字符串文本;
//吸气剂二传手
}

我认为更好的选择是对jsp使用spring标记

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...
    <form:select path="country">
        <form:option value="NONE" label="--- Select ---" />
        <form:options items="${countryList}" />
    </form:select>

...
请参见此处的完整示例:

编辑:

$('#stateSelect').change(function() {
    $.ajax({
        type:"GET",
        url : "/getCitiesForState",
        data : { state: $('#stateSelect').val()},
        success : function(data) {
            $('#citySelect').empty(); //remove all child nodes
            for(var i = 0; i < data.length; i++){
                var newOption = $('<option value=data[i].value>data[i].text</option>');
                $('#citySelect').append(newOption);
            }   
        },
        error: function() {
            alert('Error occured');
        }
    });
});
@Controller
public class HelloController{

   @RequestMapping("/getCitiesForState")
   @ResponseBody
   public List<City> printHello(@RequestParam long state) {
      List<City> cities = //get from the some repository by state
      return cities;
   }

}

public class City{
   private String value;
   private String text;
   //getters setters
}
$('#stateSelect')。更改(函数(){
$.ajax({
键入:“获取”,
url:“/getCitiesForState”,
数据:{state:$('#stateSelect').val()},
成功:功能(数据){
$('#citySelect').empty();//删除所有子节点
对于(变量i=0;i
在服务器端,您需要一个端点,该端点响应url(/getCitiesForState,在本例中为),并返回具有值和文本属性的对象列表

编辑(添加控件):

$('#stateSelect').change(function() {
    $.ajax({
        type:"GET",
        url : "/getCitiesForState",
        data : { state: $('#stateSelect').val()},
        success : function(data) {
            $('#citySelect').empty(); //remove all child nodes
            for(var i = 0; i < data.length; i++){
                var newOption = $('<option value=data[i].value>data[i].text</option>');
                $('#citySelect').append(newOption);
            }   
        },
        error: function() {
            alert('Error occured');
        }
    });
});
@Controller
public class HelloController{

   @RequestMapping("/getCitiesForState")
   @ResponseBody
   public List<City> printHello(@RequestParam long state) {
      List<City> cities = //get from the some repository by state
      return cities;
   }

}

public class City{
   private String value;
   private String text;
   //getters setters
}
@控制器
公共类Hello控制器{
@请求映射(“/getCitiesForState”)
@应答器
公共列表printHello(@RequestParam long state){
List cities=//按状态从某些存储库获取
回归城市;
}
}
公营城市{
私有字符串值;
私有字符串文本;
//吸气剂二传手
}

此外,我需要根据州下拉列表获取城市的过滤下拉列表。wwe可以在spring标签中实现吗。还是我们需要用其他的方法?我对spring和j2ee非常陌生。这通常是通过ajax调用完成的。您可以提交状态下拉列表更改页面,根据所选选项获取所需的城市,并使用正确的城市填充模型。但这真的是老生常谈。春季mvc有什么更新吗?我搜索了一下,但没有得到批准。如果可以的话,spring或jsp中的示例将非常有用。ajax方式的示例?是的,如果可以的话。因此,我需要根据状态下拉列表获取城市的过滤下拉列表。wwe可以在spring标签中实现吗。还是我们需要用其他的方法?我对spring和j2ee非常陌生。这通常是通过ajax调用完成的。您可以提交状态下拉列表更改页面,根据所选选项获取所需的城市,并使用正确的城市填充模型。但这真的是老生常谈。春季mvc有什么更新吗?我搜索了一下,但没有得到批准。如果可以,spring或jsp中的示例将非常有用。ajax方式的示例?是的,如果可以的话