Spring jsp 下拉列表

时间:2021-08-30 20:32:10

完整代码

<%--
  Created by IntelliJ IDEA.
  User: wsh
  Date: 2019/7/21
  Time: 11:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>Hello Spring boot jsp</h2>
<form:form modelAttribute="user" action="save" method="post" >
    用户名:<form:input path="username"></form:input>
           <form:errors path="username" />  <br>
    密码:  <form:password path="password"></form:password><br>
    性别:  <form:radiobutton path="xb" value="男"/>男
           <form:radiobutton path="xb" value="女"/>女<br>
    技能:  <form:checkboxes path="jineng" items="${jinengItme}" /><br>
    故乡:  <form:select path="guxiang" >
           <form:option value="">选择值</form:option>
           <form:options items="${guxiang}" />
           </form:select><br>
    <button type="submit">提交</button>
</form:form>
</body>
</html>

1.jsp代码

 故乡:  <form:select path="guxiang" >
           <form:option value="">选择值</form:option>
           <form:options items="${guxiang}" />
           </form:select><br>

2.1Spring 数据代码

    private Map<String,String> getGuxiang(){
        Map<String,String> map = new HashMap<>();
        map.put("chenzhou","郴州");
        map.put("guiyang","桂阳");
        map.put("leiyang","耒阳");
        map.put("changsha","长沙");
        return map;
    }

2.2Spring 模型代码

    @GetMapping("/")
    public String getIndexPage(Model model){
        model.addAttribute("guxiang",getGuxiang());
        return "hello";
    }