使用jsp和spring boot的动态下拉表单

yr9zkbsy  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(172)

我试图使用spring框架实现动态下拉列表,但是我不能正确地实现它。我使用ajax调用为第二个下拉列表加载依赖数据,但是在提交表单时,下拉列表值没有绑定到bean。我能够在ui上成功加载组值,但是选择并提交表单分支并没有绑定到用户的accesstype属性。任何帮助都将不胜感激。
用户.java

public class User {

    private String LanID;
    private String accessType;
    private String employeeId;
    private String name;
    private String mailId;
    private String sn;
    private String branch;

    public User() {

    }

    public User(String lanID, String accessType, String employeeId, String name, String mailId, String sn, String branch) {
        super();
        LanID = lanID;
        this.accessType = accessType;
        this.employeeId = employeeId;
        this.name = name;
        this.mailId = mailId;
        this.sn = sn;
        this.branch = branch;
    }

    @Override
    public String toString() {
        return "User [LanID=" + LanID + ", accessType=" + accessType + ", employeeId=" + employeeId + ", name=" + name
                + ", mailId=" + mailId + ", sn=" + sn + ", branch=" + branch + "]";
    }

    public String getLanID() {
        return LanID;
    }
    public void setLanID(String lanID) {
        LanID = lanID;
    }
    public String getAccessType() {
        return accessType;
    }
    public void setAccessType(String accessType) {
        this.accessType = accessType;
    }
    public String getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(String d) {
        this.employeeId = d;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMailId() {
        return mailId;
    }
    public void setMailId(String mailId) {
        this.mailId = mailId;
    }
    public String getSn() {
        return sn;
    }
    public void setSn(String sn) {
        this.sn = sn;
    }
    public String getBranch() {
        return branch;
    }
    public void setBranch(String branch) {
        this.branch = branch;
    }

}

我的控制器.java

package com.example.controller;

import com.example.bean.*;
import com.google.gson.Gson;

import java.util.List;

import javax.naming.NamingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ComponentScan("com.example.bean")
@RequestMapping("/")
public class MyController {

    @Autowired 
    private LdapProcessorImpl ldapProcessor;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/addUser")
    public String showAddUserForm(User user, Model model) {
        model.addAttribute("user", user);
        ldapProcessor.newConnection();
        List<String> branchNames = ldapProcessor.getBranches(); 
        model.addAttribute("branchTypeList", branchNames);

        return "addUser";
    }

    @GetMapping(value = "/loadGroupNames/{branchId}", headers = "Accept=*/*", produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
        String loadGroups(@PathVariable("branchId") String branch) throws IllegalStateException {
            ldapProcessor.newConnection();          
            List<String> names = ldapProcessor.getGroups(branch);   
            Gson gson = new Gson();         
            String jsonCartList = gson.toJson(names);
            return jsonCartList;
    }

    @PostMapping("/addUser")
    public String submitAddUserForm(@ModelAttribute("user") User user, Model model) {
        boolean userAdded = false;
        model.addAttribute(user.toString());
        if(!user.equals(null)) {
            ldapProcessor.newConnection();
            userAdded = ldapProcessor.addUserInGroup(user);
        }

        if(userAdded)
            return "showMessage";
        else
            return "showFailure";
    }

}

添加用户.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add User</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
    <link href="<c:url value="/css/addUser.css"/>" rel="stylesheet">    
</head>
<body>
<div align="center">
        <h1>Add User</h1>
<form:form action="addUser" method="post" modelAttribute="user">
            <form:label path="LanID">Lan Id:</form:label>
            <form:input path="LanID"/><br/>

            <form:label path="name">Full Name:</form:label>
            <form:input path="name"/><br/>

            <form:label path="employeeId">Employee Id:</form:label>
            <form:input path="employeeId"/><br/>      

            <form:label path="mailId">Email Id:</form:label>
            <form:input path="mailId"/><br/> 

            <form:label path = "branch">Branch:</form:label>
            <form:select path = "branch" id="branchType">
                 <form:option value = "NONE" label = "Select"/>
                 <form:options items = "${branchTypeList}" />
            </form:select><br/>

            <form:label path = "accessType" style="width:200px">Group:</form:label>
            <form:select path = "accessType"  id="groupName">
                <form:option value = "" label = "Select"/> 
                  <form:options items = "${result}" />
                  </form:select><br/>
            <form:button>Register</form:button>
            </form:form>
               </div>
        <script>   
        $(document).ready(function(){

            $('#branchType').on('change', function(){
                var branchId = $(this).val();
                $.ajax({
                    type: 'GET',
                    url: 'http://localhost:8080/ldap/loadGroupNames/' + branchId,
                    success: function(result) {

                        var s = '';
                        for(var i = 0; i < result.length; i++) {
                            s += '<option value="">' + result[i] + '</option>';
                            $('#groupName').html(s);
                        }

                    }
                });
            });
            $('#groupName').on('change', function(){
                var name =  $(this).val();
                alert(name)
            });
        });
    </script>
</body>
</html>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题