我如何从Servlet获取对象作为参数

时间:2018-04-17 07:11:21

标签: java jsp servlets

我无法理解如何将getParameter()与对象一起使用,因为据我所知,这是不可能的。我应该怎么做?

货币货币= req.getParameter("货币"); - 它不起作用。

我的servlet:

public class AddPurseServlet extends HttpServlet {
private PurseDao purseDao;
private CurrencyDao currencyDao;

@Override
public void init() throws ServletException {
    purseDao = (PurseDao) getServletContext().getAttribute("purseDao");
    currencyDao = (CurrencyDao) getServletContext().getAttribute("currencyDao");
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    List<Currency> currencies = currencyDao.getAll();

    req.setAttribute("currencies", currencies);

    req.getRequestDispatcher("WEB-INF/view/addPurse.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    User user = (User) session.getAttribute("user");
    String amount = req.getParameter("amount");
    Currency currency = req.getParameter("currencies");

    Purse purse = new Purse(user, currency, new BigDecimal(amount), new Timestamp(System.currentTimeMillis()));

    purseDao.insert(purse);

    resp.sendRedirect("userPage");
}
}

我的jsp页面,我需要货币和金额参数:

<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Hello USER!</h1>
<div class="container">
<h1>Save User</h1>
<form method="post" action="addPurse">
    <div class="form-group">
        <label>Currency</label>
        <select>
            <c:forEach var="currency" items="${currencies}">
                <option value="${currency.id}">${currency.name}</option>
            </c:forEach>
        </select>
    </div>
    <div class="form-group">
        <label>Amount</label>
        <input class="form-control" name="amount" placeholder="Amount">
    </div>
    <input class="btn btn-default btn-xs" type="submit" value="Save">
    <a class="btn btn-default btn-xs" href="usersList" role="button">cancel</a>
</form>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

参数是字符串。

如果您的货币是枚举

final String currencyStr = request.getParameter("currency");
final Currency currency = Stream.of(Currency.values()).filter(currency -> currency.code.equals(currencyStr)).findFirst().orElse(null));

答案 1 :(得分:1)

因此,我从您的代码中了解到,您正在尝试填充货币列表,然后尝试为货币对象设置一些金额。

为此,您的货币由唯一ID标识。这很完美。现在您需要了解的是,您并不真正需要JSP页面中的对象。您所需要的只是货币的ID。

所以我会用这样的东西来修改你的JSP:

<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Hello USER!</h1>
<div class="container">
<h1>Save User</h1>
<form method="post" action="addPurse">
    <div class="form-group">
        <label>Currency</label>
        <select id="currency" name="currency">
            <c:forEach var="currency" items="${currencies}">
                <option value="${currency.id}">${currency.name}</option>
            </c:forEach>
        </select>
    </div>
    <div class="form-group">
        <label>Amount</label>
        <input class="form-control" name="amount" placeholder="Amount">
    </div>
    <input class="btn btn-default btn-xs" type="submit" value="Save">
    <a class="btn btn-default btn-xs" href="usersList" role="button">cancel</a>
</form>
</div>
</body>
</html>

现在,来到你的servlet。看到关键字“DAO”和“CurrencyDAO”,我假设您正在使用JPA / Hibernate。因此,您需要使用以下内容修改代码:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    User user = (User) session.getAttribute("user");
    String amount = req.getParameter("amount");
    String currencyid = req.getParameter("currency");
//Get your currency object from database
    Currency currencyToBeEdited = currencyDao.findById(Integer.parseInt(currencyid));

//Add the code to set the amount and save it back to the database

    Purse purse = new Purse(user, currency, new BigDecimal(amount), new Timestamp(System.currentTimeMillis()));

    purseDao.insert(purse);

    resp.sendRedirect("userPage");
}

注意:我不是百分之百确定你的目标,但我提供的建议有望带你走向正确的方向