下载列表没有在使用jsp的spring MVC中显示

时间:2017-08-29 08:32:24

标签: jsp spring-mvc jsp-tags

我有一个JSP页面,我试图在其中显示下拉列表,但它给了我一个例外。

ExpenseController.java

@Controller
public class ExpenseController {
    final static Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private ExpenseService expenseService;

    @Autowired
    private CategoryDao categoryDao;

    @RequestMapping(value = "/addDetails", method = RequestMethod.GET)
    public String getExpenseEntryPage(Model model) {
        ExpenseCreationBean expenseCreationBean = new ExpenseCreationBean();
        model.addAttribute("expenseCreationBean", expenseCreationBean);
        return "addDetails";
    }

    @RequestMapping(value = "savedata", method = RequestMethod.POST)
    public String addExpense(@ModelAttribute("expenseCreationBean") ExpenseCreationBean expenseCreationBean, Model model) {
        expenseService.createExpense(expenseCreationBean);
        return "expenseAdded";
    }

    @PostConstruct
    public void init(){
        logger.debug("ExpenseController Bean has been Initialised.");
    }

    @PreDestroy
    public void destroy(){
        logger.debug("ExpenseController Bean has been Destroyed.");
    }

    @ModelAttribute("categoryList")
       public Map<String, String> getCategoryList()
       {
          Map<String, String> categoryList = new HashMap<String, String>();
          categoryList.put("US", "United States");
          categoryList.put("CH", "China");
          categoryList.put("SG", "Singapore");
          categoryList.put("MY", "Malaysia");
          return categoryList;
       }
}

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"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<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 id="addExpense" modelAttribute="expenseCreationBean"
        action="savedata" method="post">
        <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>Category: <form:select path="category">
                        <form:option value="NONE" label="--- Select ---" />
                        <form:options items="${expenseCreationBean.category}" />
                    </form:select>
                </td> --%>
                <td><form:label path="country">Country</form:label></td>
                <td><form:select path="country">
                        <form:option value="NONE" label="Select" />
                        <form:options items="${categoryList}" />
                    </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>

ExpenseCreationBean.java

public class ExpenseCreationBean {
    private User amountPaidBy;
    private double amountPaid;
    private Category category;
    private Unit unit;
    private double totalAmt;
    private Date dateOfPurchase;
    private String itemName;
    private Set<User> excludedUsers;
    private String comments;
    private double quantityPurchased;
}

例外: -

org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/addDetails.jsp] at line [43]

40:                 <td><form:label path="category">Category</form:label></td>
41:                 <td><form:select path="category">
42:                         <form:option value="NONE" label="Select" />
43:                         <form:options items="${categoryList}" />
44:                     </form:select></td>
45:                 <td>Paid By: <select name="paid_by"></td>
46:                 <td>Amount Paid:<input type="text" name="total_price"

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)

当我转到URL - / addDetails时,我得到以下异常。任何人都可以告诉我我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

Category是userDefined类,在从Category中检索数据时,JSTL无法检索数据。