在Java Spring MVC中的下拉列表和复选框中填写项目

时间:2018-07-19 13:23:02

标签: spring-mvc

我想按照我的尝试填充下拉列表和复选框中的项目,但无法将下拉列表和复选框中的项目填充为错误显示

  

由以下原因引起:java.lang.IllegalArgumentException:'items'不能为空

JSP页面:Student.jsp

<form:form method="post" modelAttribute="student_Master">
<form:input path="stud_id" type="hidden" id="stud_id"/>
 <tr>  
       <td><label for="stud_gender">Gender: </label></td>
        <td>
        <form:radiobutton path="stud_gender" value="Male" />Male
        <form:radiobutton  path="stud_gender" value="Female" />Female
        <form:radiobutton  path="stud_gender" value="Transgender" />Transgender
        </td>
        <td><form:errors path="stud_gender" cssClass="error"/></td>
    </tr>  

      <tr>  
       <td><label for="stud_language">Language: </label></td>
        <td><form:select path="stud_language" items="${addModel.stud_language}" />  
        </td>  
    </tr>

    <tr>  
        <td><label for="stud_religion">Religion: </label></td> 
        <td><form:checkboxes items="${addModel.stud_religion}"  path="stud_religion" />  
        </td>  
    </tr>    
</form:form>

Controller.java

-用于在下拉列表和复选框中添加项目的代码,stud_religion用于DropdownList,复选框用于stud_language-

    @RequestMapping("/getAddItem")
        public ModelAndView addItems(@ModelAttribute Student_Master student_Master,BindingResult result)
        {

            ArrayList<String> stud_religion=new ArrayList<String>();
            stud_religion.add("Hindu");
            stud_religion.add("Muslim");

            ArrayList<String> stud_language=new ArrayList<String>();
            stud_language.add("Hindi");
            stud_language.add("English");

            HashMap<String, ArrayList<String>> addModel=new HashMap<String, ArrayList<String>>();

            addModel.put("stud_religion", stud_religion);
            addModel.put("stud_language", stud_language);
            return new ModelAndView("AddItem","addModel",addModel);     
        }


--code for redirecting to Student.jsp , after getting to this page i should get item in dropdown list and Checkbox, but its not coming--



@RequestMapping(value={"/new"}, method=RequestMethod.GET)
    public String newStudent(ModelMap model)
    {
        Student_Master stud=new Student_Master();
        model.addAttribute("student_Master", stud);
        model.addAttribute("edit",false);
        return "Student";
    }

Student_Master.java(模型类)

public class Student_Master {


    @Column(name="stud_religion")
    public String stud_religion;

    @Column(name="stud_language")
    private String[] stud_language;

    }

我如何在newStudent方法中包括addItems方法,以便iems可以显示在下拉菜单或复选框中?

0 个答案:

没有答案