无法单击JSP中选中的单选按钮以外的单选按钮

时间:2015-02-19 16:55:39

标签: jsp servlets for-loop radio-button

这是我的jsp页面

    <%!
    // Table row colors
    static final String[] COLORS = { "#E0E0E0", "#F0F0F0" };

    // Array of tab codes, labels and JSP names
    static final String[][] TABS = {
                                    {"HD", "Headers", "ShowRequestHeaders.jsp"} ,
                                    {"PM", "Parameters", "ShowParameters.jsp"},
                                    {"SR", "ServletRequest Methods", "ShowServletRequestMethodValues.jsp"},
                                    {"HR", "HttpServletRequest Methods", "ShowHttpServletRequestMethodValues.jsp"} 
    };


%>
    <html>
<head>
    <title>Show Request</title>
</head>
<body>
    <h2>Show Request</h2>
    <form >
        <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <td align="left">
                    <%
                        String which = request.getParameter("which");
                        if (which == null)
                            which = TABS[0][0]; //HD
                        String jspToRun = null;
                        for (int i = 0; i < TABS.length; i++) {

                            String tabCode = TABS[i][0];
                            String tabLabel = TABS[i][1];
                            String tabJSP = TABS[i][2];
                            String CHECKED = "";
                            if (which.equals(tabCode)) {

                                CHECKED = "CHECKED";
                                jspToRun = tabJSP;
                            }
                    %> <input name="which" type="RADIO" value= "<%=tabCode%>"
                                                                <%=CHECKED%> onClick="this.form.submit()"
                                                                 >
                                                                <%=tabLabel%>

                    <%
                        }
                    %>
                    <p>
            </td>
        </tr>

        <tr>
            <td align="center" valign="top">

                <!-- Page showing details of the request -->
                <jsp:include page="<%= jspToRun %>" flush="true"></jsp:include>

根据单击的单选按钮,包含不同的jsp。当我试图检查除选中的按钮以外的单选按钮时,没有任何变化。请帮助我。

1 个答案:

答案 0 :(得分:1)

使用下面的JSP文件对我来说很好。当我单击一个单选按钮时,表单将被提交,并在刷新页面时包含相应的JSP文件。

请注意,我已调用 JSP文件index.jsp,并将其他JSP文件包含在同一目录中。

<%!// Table row colors
static final String[] COLORS = { "#E0E0E0", "#F0F0F0" };

// Array of tab codes, labels and JSP names
static final String[][] TABS = {
        { "HD", "Headers", "ShowRequestHeaders.jsp" },
        { "PM", "Parameters", "ShowParameters.jsp" },
        { "SR", "ServletRequest Methods",
                "ShowServletRequestMethodValues.jsp" },
        { "HR", "HttpServletRequest Methods",
                "ShowHttpServletRequestMethodValues.jsp" } };%>
<html>
<head>
<title>Show Request</title>
</head>
<body>
<h2>Show Request</h2>
<form>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <td align="left">
                <%
                    String which = request.getParameter("which");
                    if (which == null)
                        which = TABS[0][0]; //HD
                    String jspToRun = null;
                    for (int i = 0; i < TABS.length; i++) {

                        String tabCode = TABS[i][0];
                        String tabLabel = TABS[i][1];
                        String tabJSP = TABS[i][2];
                        String CHECKED = "";
                        if (which.equals(tabCode)) {

                            CHECKED = "CHECKED";
                            jspToRun = tabJSP;
                        }
                %> <input name="which" type="RADIO" value="<%=tabCode%>"
                <%=CHECKED%> onClick="this.form.submit()"> <%=tabLabel%>     <%
}
 %>
                <p>
            </td>
        </tr>

        <tr>
            <td align="center" valign="top">
                <!-- Page showing details of the request --> <jsp:include
                    page="<%=jspToRun%>" flush="true"></jsp:include>

            </td>
        </tr>
    </table>
</form>
</body>
</html>

生成的HTML。请注意,我只使用文件名作为包含JSP文件的内容:

<html>
<head>
<title>Show Request</title>
</head>
<body>
<h2>Show Request</h2>
<form>
    <table border="1" cellpadding="5" cellspacing="5">
        <tr>
            <td align="left">
                 <input name="which" type="RADIO" value="HD"
                CHECKED onClick="this.form.submit()"> Headers  <input name="which" type="RADIO" value="PM"
                 onClick="this.form.submit()"> Parameters  <input name="which" type="RADIO" value="SR"
                 onClick="this.form.submit()"> ServletRequest Methods  <input name="which" type="RADIO" value="HR"
                 onClick="this.form.submit()"> HttpServletRequest Methods 
                <p>
            </td>
        </tr>

        <tr>
            <td align="center" valign="top">
                <!-- Page showing details of the request --> ShowRequestHeaders.jsp

            </td>
        </tr>
    </table>
</form>
</body>
</html>