当前位置: 首页 > 知识库问答 >
问题:

从Bootstrap列表框中选择后,在Spring Controller中返回Null值

亢建木
2023-03-14
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contract-wise Report Selection</title>
<link href="<c:url value='/static/css/bootstrap.css' />"
    rel="stylesheet"></link>
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    function myFunction() {
        var selectedvalue = $("#mySelect option:selected").val();
    }
</script>
</head>

<body>
    <div class="generic-container">
        <%@include file="authheader.jsp"%>

        <div class="well lead">Contract-wise Report Selection</div>
        <form:form method="POST" modelAttribute="reports01"
            action="reportDetailed01" class="form-horizontal">
            <form:input type="hidden" path="id" id="id" />


            <div class="row">
                <div class="form-group col-md-12">
                    <label class="col-md-3 control-lable" for="contractMap">Contracts
                        to Select</label>
                    <div class="col-md-7">
                        <form:select id="mySelect" path="contractMap"
                            onChange="myFunction" items="${contractList}" multiple="true"
                            class="form-control input-sm" />
                        <div class="has-error">
                            <form:errors path="contractMap" class="help-inline" />
                        </div>
                    </div>
                </div>
            </div>

            <button type="button" onclick="myFunction()">Try it</button>

            <div class="row">
                <div class="form-actions floatRight">
                    <input type="submit" value="Print" class="btn btn-primary btn-sm" />
                    or <a href="<c:url value='/' />">Cancel</a>
                </div>
            </div>

            <div class="well">
                <a href="<c:url value='/' />">Back to Menu</a>
            </div>

        </form:form>
    </div>
</body>
</html>
/**
     * This method will list all contracts for selection.
     */
    @RequestMapping(value = { "/reportDetailed01" }, method = RequestMethod.GET)
    public String showContractsForReports01(ModelMap model) {

        ReportForm01 rf01 = new ReportForm01();

        Map<Integer, String> contractList = contractService.findAllContracts01();

        System.out.println(contractList);
        System.out.println("========= GET ================");

        model.addAttribute("reports01", rf01);
        model.addAttribute("contractList", contractList);
        model.addAttribute("loggedinuser", getPrincipal());
        return "reports01";
    }

/***此方法将获取选择后的所有合同。*/@RequestMapping(值={“/ReportDetailed01”},方法=RequestMethod.Post)//public ModelAndView getContractsForReports01(@ModelAttribute(值=“Reports01”)ReportForm01,BindingResult BindingResult){public String getContractsForReports01(@ModelAttribute ReportFor01,BindingResult,HttpServletRequest request){

System.out.println("=====================xxx=================");
System.out.println(reportForm01.getContractMap());


return "redirect:/reportDetailed01";

}

这是表单的域模型:

public class ReportForm01 implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer Id;


    private Map<Integer,String> contractMap = new HashMap<Integer, String>();

    private Map<Integer, String> selectedContractMap = new HashMap<Integer, String>();


    public Map<Integer,String> getContractMap() {
        return contractMap;
    }

    public void setContractMap(Map<Integer,String> contractMap) {
        this.contractMap = contractMap;
    }

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public Map<Integer, String> getSelectedContractMap() {
        return selectedContractMap;
    }

    public void setSelectedContractMap(Map<Integer, String> selectedContractMap) {
        this.selectedContractMap = selectedContractMap;
    }

}

但似乎什么都不起作用。

我怀疑我可能缺少一个处理用户选择的脚本。任何帮助都将不胜感激。

共有1个答案

董高洁
2023-03-14

在进行了更深入的搜索之后,以下链接被证明具有重要意义:

如何将javascript变量传递给Spring mvc控制器

将javascript变量值传递到输入类型隐藏值中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contract-wise Report Selection</title>
<link href="<c:url value='/static/css/bootstrap.css' />"
    rel="stylesheet"></link>
<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script>
    function getValue() {
        var x = document.getElementById("sel");
        var completeRange = "";
        for (var i = 0; i < x.options.length; i++) {
            if (x.options[i].selected) {
                var selectedString = x.options[i].text;
                completeRange = completeRange + ", " + selectedString;
            }
        }
        document.getElementById('contractListJS').value = completeRange
                .substring(2);
        document.getElementById("reportDetailed01").submit();
    }
</script>


</head>

<body>
    <div class="generic-container">
        <%@include file="authheader.jsp"%>

        <div class="well lead">Contract-wise Report Selection</div>
        <form:form method="POST" modelAttribute="reports01"
            action="reportDetailed01" id="reportDetailed01" class="form-horizontal">

            <div class="row">
                <form:input path="contractList" type="hidden" id="contractListJS"
                    value="" />
            </div>

            <div class="row">
                <div class="form-group col-md-12">
                    <label class="col-md-3 control-lable" for="contractMap">Contracts
                        to Select</label>
                    <div class="col-md-7">
                        <form:select id="sel" path="contractMap" items="${contractMap}"
                            multiple="true" class="form-control input-sm" />
                        <div class="has-error">
                            <form:errors path="contractMap" class="help-inline" />
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="form-actions floatRight">
                    <input type="submit" value="Print" onclick="getValue()"
                        class="btn btn-primary btn-sm" /> or <a
                        href="<c:url value='/' />">Cancel</a>
                </div>
            </div>

            <div class="well">
                <a href="<c:url value='/' />">Back to Menu</a>
            </div>
        </form:form>
    </div>
</body>
</html>
@Controller
@RequestMapping("/")
public class ReportsController01 {

    @Autowired
    ContractService contractService;

    /**
     * This method returns the principal[user-name] of logged-in user.
     */
    private String getPrincipal() {
        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((UserDetails) principal).getUsername();
        } else {
            userName = principal.toString();
        }
        return userName;
    }

    /**
     * This method will list all contracts for selection.
     */
    @RequestMapping(value = { "/reportDetailed01" }, method = RequestMethod.GET)
    public String showContractsForReports01(ModelMap model) {

        ReportForm01 rf01 = new ReportForm01();

        Map<Integer, String> contractList = contractService.findAllContracts01();

        System.out.println(contractList);
        System.out.println("========= GET ================");

        model.addAttribute("reports01", rf01);
        model.addAttribute("contractMap", contractList);
        model.addAttribute("loggedinuser", getPrincipal());
        return "reports01";
    }




    /**
     * This method will get all contracts after selection.
     */
    @RequestMapping(value = { "/reportDetailed01" }, method = RequestMethod.POST)
    public String getContractsForReports01(@ModelAttribute ReportForm01 reportForm01, BindingResult bindingResult, HttpServletRequest request) {
        System.out.println("=================== POST ===============");
        System.out.println(reportForm01.getContractList());
        System.out.println("=====================xxx=================");
        return "redirect:/";
    }

}
public class ReportForm01 implements Serializable {
    private static final long serialVersionUID = 1L;

    private String contractList;

    private Map<Integer,String> contractMap = new HashMap<Integer, String>();


    public Map<Integer,String> getContractMap() {
        return contractMap;
    }

    public void setContractMap(Map<Integer,String> contractMap) {
        this.contractMap = contractMap;
    }


    public String getContractList() {
        return contractList;
    }

    public void setContractList(String contractList) {
        this.contractList = contractList;
    }


}
 类似资料:
  • 我最近开始学习oracle和SQL。在学习过程中,我遇到了几个问题,我的朋友在一次采访中被问到。 此查询生成Employees表中的所有行。至于我所理解的Oracle在列中搜索数据,那么,NULL,这里是作为列名处理的吗?我说Oracle在列中搜索数据是正确的吗?Oracle怎么会给出这个查询中的所有行呢?在WHERE子句中,条件的左手边不一定是列名吗?它不应该抛出一个错误吗? 不给出选定的行。

  • 问题内容: 是否可以执行PIVOT并从表中选择列表,而不是使用单个值? 像这样(不正确的语法错误): 这个可以编译,但是对我不起作用: PS:我不想使用动态SQL,有没有一种方法可以不使用动态SQL? 问题答案: 如果没有动态SQL,那么恐怕答案是否定的,那是不可能的。解析器需要预先知道值才能执行到列的数据透视。

  • 问题内容: 我有一个自定义对话框,当我尝试获取EditText的值时,它返回null。 该行返回null 这是完整的代码。 问题答案: 尝试这个: 你必须告诉在哪个视图中找到ID。否则,它将尝试通过xml布局(通常在中声明)夸大视图中的ID。

  • 问题内容: 我希望下面的代码向我显示表中交换列为null但结果集显示0行的所有记录。知道为什么吗? 问题答案: 也许您将其解释为不相同,但请尝试此操作 但如果仍然无法获得价值,也许它上面有空格,那么您应该这样做,

  • 问题内容: 可以说我有以下熊猫数据框: 我可以根据特定的值进行子集化: 但是如何根据值列表进行子集设置呢?-这样的东西: 问题答案: 您可以使用方法: 并得到相反的用法:

  • 问题内容: 上面的代码创建一个带有两个列表框的窗口。但是,如果要从这两个值中检索值,就会出现问题,因为一旦在一个值中选择一个值,它就会取消选择在另一个值中选择的值。 这仅仅是开发人员必须忍受的限制吗? 问题答案: 简短答案:将所有列表框小部件的属性值设置为False或零。 从列表框小部件的pythonware概述中: 默认情况下,选择被导出到X选择机制。如果您在屏幕上有多个列表框,这确实会使可怜的