JSTL 和 Servlet 中的单选按钮
Radio button in JSTL and Servlet
我在 JSP 文件中有一些值,将单选按钮推送到 Servlet 并输出它时出现问题。
输入文件在这个jsp:
<label for="phone">Phone: </label>
<input type="text" name="phone" id="phone" value="${phone}"><br/><br/>
<input type="radio" name="sex" id="sex" value="${male}">Male
<input type="radio" name="sex" id="sex" value="${female}"/>Female<br/><br/>
Servler 在这里:public static RequestNewEmployee fromRequestParameters(HttpServletRequest request) {
return new RequestNewEmployee(
request.getParameter("firstName"),
request.getParameter("lastName"),
request.getParameter("email"),
request.getParameter("phone"),
request.getParameter("sex"));
}
public void setAsRequestAttributes(HttpServletRequest request) {
request.setAttribute("firstName", firstName);
request.setAttribute("lastName", lastName);
request.setAttribute("email", email);
request.setAttribute("phone", phone);
request.setAttribute("sex", sex);
}
但是,我在性领域没有收到任何 praramentr(尽管所有其他人都正常工作)。可能是什么问题?
理论
${} 称为 EL 表达式。在里面你可以有变量。来自 the oracle tutorial:
The web container evaluates a variable that appears in an expression
by looking up its value according to the behavior of
PageContext.findAttribute(String). For example, when evaluating the
expression ${product}, the container will look for product in the
page, request, session, and application scopes and will return its
value. If product is not found, null is returned. A variable that
matches one of the implicit objects described in Implicit Objects will
return that implicit object instead of the variable's value.
现在让我们谈谈您的问题。
- 您不能有两个具有相同 ID 的 html 标签。
- 您收到空参数,因为显然它是空的(未定义)。您在哪里定义
male
和 female
变量?
我认为这就是您要实现的目标:
form.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/formServlet">
<input type="radio" name="sex" id="sexChoice1" value="male">Male
<input type="radio" name="sex" id="sexChoice2" value="female"/>Female<br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
FormServlet
public class FormServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(req.getParameter("sex"));
}
}
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>FormServlet</servlet-name>
<servlet-class>com.artmal.controller.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
</web-app>
选择 'male' 选项并提交后的控制台输出:
male
我在 JSP 文件中有一些值,将单选按钮推送到 Servlet 并输出它时出现问题。
输入文件在这个jsp:
<label for="phone">Phone: </label>
<input type="text" name="phone" id="phone" value="${phone}"><br/><br/>
<input type="radio" name="sex" id="sex" value="${male}">Male
<input type="radio" name="sex" id="sex" value="${female}"/>Female<br/><br/>
Servler 在这里:public static RequestNewEmployee fromRequestParameters(HttpServletRequest request) {
return new RequestNewEmployee(
request.getParameter("firstName"),
request.getParameter("lastName"),
request.getParameter("email"),
request.getParameter("phone"),
request.getParameter("sex"));
}
public void setAsRequestAttributes(HttpServletRequest request) {
request.setAttribute("firstName", firstName);
request.setAttribute("lastName", lastName);
request.setAttribute("email", email);
request.setAttribute("phone", phone);
request.setAttribute("sex", sex);
}
但是,我在性领域没有收到任何 praramentr(尽管所有其他人都正常工作)。可能是什么问题?
理论
${} 称为 EL 表达式。在里面你可以有变量。来自 the oracle tutorial:
The web container evaluates a variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String). For example, when evaluating the expression ${product}, the container will look for product in the page, request, session, and application scopes and will return its value. If product is not found, null is returned. A variable that matches one of the implicit objects described in Implicit Objects will return that implicit object instead of the variable's value.
现在让我们谈谈您的问题。
- 您不能有两个具有相同 ID 的 html 标签。
- 您收到空参数,因为显然它是空的(未定义)。您在哪里定义
male
和female
变量?
我认为这就是您要实现的目标:
form.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/formServlet">
<input type="radio" name="sex" id="sexChoice1" value="male">Male
<input type="radio" name="sex" id="sexChoice2" value="female"/>Female<br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
FormServlet
public class FormServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
System.out.println(req.getParameter("sex"));
}
}
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>FormServlet</servlet-name>
<servlet-class>com.artmal.controller.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/formServlet</url-pattern>
</servlet-mapping>
</web-app>
选择 'male' 选项并提交后的控制台输出:
male