验证或搜索后,在搜索中保留复选框的状态

时间:2014-02-07 15:49:12

标签: html forms jsp checkbox struts

我有一个搜索页面,其中包含各种搜索条件选项,包括一个复选框。 我想保留搜索条件的状态,以便使用该应用程序的人知道他们搜索的内容。 我使用<input type="text" name="foo" value="${param.foo}">作为文本框,但我不知道如何选择复选框。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我试过像这样的JSTL标记库,

请参阅<c:if>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Employee Search</title>
</head>
<body>

    <font size="+1"> Search </font>
    <form action="search.jsp" method="GET">
        <table>
            <tr>
                <td align="right">Name:</td>
                <td><input type="text" name="name" value="${param.name}" /></td>
            </tr>
            <tr>
                <td align="right">Location:</td>
                <td><input type="checkbox" name="location" value="france"
                    <c:if test="${param.location != null && param.location=='france' }">
                checked='checked'
                </c:if>>france<br>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

注意:不要忘记将jstl.jar放在WEB-INF / lib中。您可以从here

下载

现在,如果您不想使用JSTL标签[我认为这是您正在寻找的标签],您可以这样做,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Employee Search</title>
</head>
<body>
    <font size="+1"> Search</font>
    <form action="search.jsp" method="GET">
        <table>
            <tr>
                <td align="right">Name:</td>
                <td><input type="text" name="name" value="${param.name}" /></td>
            </tr>
            <tr>
                <td align="right">Location:</td>
                <td><input type="checkbox" name="location" value="france"
                ${param.location=='france' ? 'checked=checked' : ''} 
                >france<br>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

希望这会有所帮助。请告诉我。