【问题标题】:Drop down list is not getting displayed with spring mvc and jspspring mvc和jsp不显示下拉列表
【发布时间】:2018-02-07 19:03:46
【问题描述】:

我有一个应用程序,我在 JSP 页面中显示一个下拉列表,但我的代码出现异常。

public class ExpenseCreationBean {
    private String color;

public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

控制器类:-

@RequestMapping(value = "/addDetails", method = RequestMethod.GET)
    public String getExpenseEntryPage(Model model) {
        ExpenseCreationBean expenseCreationBean = new ExpenseCreationBean();
        model.addAttribute("expenseCreationBean", expenseCreationBean);
        List<String> coloursList = new ArrayList<String>();
        coloursList.add("red");
        coloursList.add("green"); 
        coloursList.add("yellow");
        coloursList.add("pink");
        coloursList.add("blue");
        model.addAttribute("colours", coloursList);
        System.out.println("I was here!!");
        return "addDetails";
    }

addDetails.jsp 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>Add Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
    href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
    $(function() {
        $("#datepicker").datepicker({
            showOn : "button",
            buttonImage : "images/calendar.png",
            buttonImageOnly : true,
            buttonText : "Select date"
        });
    });
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <h1>Expense Entry Details</h1>
    <form:form method="post" action="savedata" modelAttribute="expenseCreationBean">
        <table border="6px" cellspacing="10px" cellpadding="10px">
            <tr>
                <td>Date Of Purchase: <input type="text" id="datepicker"
                    name="date_of_purchase"></td>
                <td>Item Name:<input type="text" name="description"></td>
                <td>Please select:</td>
                <td><form:select path="color">
                      <form:option value="" label="...." />
                      <form:options items="${colours}" />
                       </form:select>
                                </td>
                <td>Paid By: <select name="paid_by"></td>
                <td>Amount Paid:<input type="text" name="total_price"
                    id="total_price"></td>
                <td>Quantity:<input type="text" name="quantity_purchased"></td>
                <td>Unit:<input type="text" name="unit"></td>
            </tr>
            <tr>
            <tr>
            <tr>
            <tr>
                <td>Exclude:</td>
                <td><input TYPE="checkbox" name="exclude">
            </tr>
            <tr>
                <td>Comments:<textarea rows="3" cols="25" name="comments"></textarea>
                </td>
            </tr>
            <tr>
                &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
                <td><input type="submit" value="Save" align="middle"></td>
        </table>
    </form:form>
</body>
</html>

我收到以下异常:-

javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
    org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:143)
    org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:157)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)

这只是一个 Spring MVC Web 应用程序,我试图在其中显示预先填充了颜色数据的下拉列表。

非常感谢任何帮助。

【问题讨论】:

  • 你能通过gitstorage 分享这个项目吗?
  • 我已经检查过了,没有spring配置,没有java源,请你检查一下并将项目文件添加到repo中吗?
  • @Sergii : 请检查 DevBranch
  • 类似问题的答案似乎是here,但这个答案并不能帮助我解决问题。如果您能找到正确的解决方案,请回答您的问题。

标签: java spring jsp spring-mvc jstl


【解决方案1】:

我在 addDetails.jsp 文件的顶部添加了以下行并且它起作用了:-

【讨论】:

    【解决方案2】:

    尝试添加到 Map,而不是 ArrayList。

    Map<String,String> coloursList = new HashMap<String,String>();
    coloursList.put("R","red");
    coloursList.put("R","green");
    coloursList.put("Y","yellow");
    coloursList.put("P","pink");
    

    【讨论】:

    • 添加我的代码后,同样的异常或不同
    • 还有一件事,至少检查 pom.xml 文件,JSTL 版本 (1.2) 和 web.xml (2.5)。
    • 我在 github 中看到了你的 pom.xml,我没有找到 JSTL maven 依赖项,而且在 web.xml 中,版本是 2.3。改变这两件事。因为如果您没有添加 JSTL 库意味着,JSP 页面将不会评估 EL 表达式
    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多