String selected = req.getParameter("name") 回傳 null。我需要來自所選單選按鈕的值。我應該如何得到它?謝謝!
這是 HTML 檔案中的格式:
<form th:method="POST" th:action="@{/select}">
<ul th:each="o: ${orders}" style="list-style-type: none">
<input th:text="${o.getDescription()}" th:value="${o.getName()}" type="radio" name="color"><br>
</ul>
<br/>
<input value="Submit" type="submit">
</form>
這是我的 servlet:
@WebServlet(urlPatterns = "/select")
public class SelectServlet extends HttpServlet {
private final SpringTemplateEngine springTemplateEngine;
public SelectServlet(SpringTemplateEngine springTemplateEngine) {
this.springTemplateEngine = springTemplateEngine;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
WebContext context = new WebContext(req, resp, getServletContext());
String selected = req.getParameter("color");
context.setVariable("selectedOption", selected);
this.springTemplateEngine.process("selectSize.html", context, resp.getWriter());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/select");
}
}
我已經閱讀了許多其他類似問題的答案,但沒有一個能解決我的問題。
uj5u.com熱心網友回復:
當您想攔截 HTTP GET 請求時,您應該使用 doGet()。當您想攔截 HTTP POST 請求時,您應該使用 doPost()。在這個給定的問題中,錯誤是您正在發送一個發布請求,但在 doGet 方法中更改或實作您的代碼嘗試在 doPost 方法中撰寫相同的代碼。
uj5u.com熱心網友回復:
您發送從表單POST請求(方法= POST),但在servlet的你寫的代碼里面doGet(),所以你的代碼不會被servlet中執行命令作為servlet將執行的doPost()方法,所以你應該寫里面的代碼doPost()。我希望這能解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/336063.html
