在JSP

时间:2017-01-10 20:12:26

标签: forms jsp checkbox

我有一个带有radiobuttons形式的jsp文件。使用POST我在提交后会阅读此表单。

然而,在提交表格之后,表格中的答案会消失,我想让它们显示出来。

怎么做?我一直在网上走路,似乎无法找到解决方案。

我一直在循环中尝试req.setAttribute("q" + i, "yes");,因为从这个表单获取的值我保留在一个数组中。

我也想过JS但没有经验。

没有成功。你能帮忙吗?我只需要一些提示或者其他东西开始。

编辑:

在我的JSP(keywords.jsp)文件中,有一个搜索表单:

<form method="POST" action="keywords">
                    <p>
                        Are you looking for an urgent email?
                        <label><input type="radio" name="q0" value="yes" class="q0">Yes</label>
                        <label><input type="radio" name="q0" value="no" class="q0">No</label><br/>
                    </p>
                    <p>
                    Are you looking for a business email?
                        <label><input type="radio" name="q1" value="yes" class="q1">Yes</label>
                        <label><input type="radio" name="q1" value="no"  class="q1">No</label><br/>
                    </p>
input class="btn btn-warning" type="submit" value="Search Keywords">

因此,当我点击提交时 - 在doPost中,我获得了这些无线电按钮的值。 t页面重新加载,表格再次与搜索结果一起显示。重新加载后,在表单中,不会检查答案。

我想要的是我希望在重新加载表单后检查表单中的答案。重新加载之前给出的答案应该在重新加载后显示在ArrayList

我是我的Servlet:

for(int i = 0; i < keywords.getAnswersIDs().size(); i++) {
    if("1".equalsIgnoreCase(keywords.getAnswersIDs().get(i))) {
        req.setAttribute("q" + i, "checked");
        LOGGER.info(MARKER, "For element " + i + " is yes.");
    } else {
        req.setAttribute("q" + i, "no");
        LOGGER.info(MARKER, "For element " + i + " is no.");
    }
}

最后我也有这个:

RequestDispatcher dispatcher = req.getRequestDispatcher("/keywords.jsp");
        LOGGER.info(MARKER, "Dispatcher to keywords.jsp");
        try {
            dispatcher.forward(req, response);
        } catch (ServletException e) {
            LOGGER.debug(MARKER, "Caught ServletException " + e);
            e.printStackTrace();
        } catch (IOException e) {
            LOGGER.debug(MARKER, "Caught IOException " + e);
            e.printStackTrace();
        }

我希望这会有所帮助。

1 个答案:

答案 0 :(得分:0)

您在帖子处理程序中设置了一些属性,但在JSP中没有做任何事情来表明它们已被选中。您需要添加一些标记以将相关选项设置为被检查(可能通过使用JSP EL表达式 - 请参阅底部的引用):

Assign an initial value to radio button as checked

所以:

<input type="radio" name="q1" value="yes" ${someAttributeTrue ? 'checked' :''}>

让我们通过创建一些类来保存问题数据,使整个事情变得更加清晰。

注意我没有编译和运行它所以它可能不完美但你得到了一般的想法。你需要通过Servlet来渲染初始表单而不是点击关键字.jsp直接。

某些模型类:

public class Questionnaire{
    private List<Question> questions;

    public Questionnaire(){
        questions = new ArrayList<>();
        questions.add(new Question("Are you looking for an urgent email?", false));
        questions.add(new Question("Are you looking for a business email?", true));
    }

    public List<Question> getQuestions(){
        return questions;
    }
}

public class Question{
    private String question;
    private boolean selected;

    // boolean specifies the default on initial page load
    public Question(String question, boolean selected){
        this.question = question;
        this.selected = selected;
    }

    public String getQuestion(){
        return question;
    }

    public boolean isSelected(){
        return selected;
    }
}

<强>的Servlet

public class MyServlet extends HttpServlet{

    public void doGet(HttpServletReqest request, HttpServletResponse response){
        //set a default questionnaire for initial render of JSP
        request.setAttribute("questionnaire", new Questionnaire());

        RequestDispatcher dispatcher = req.getRequestDispatcher("/keywords.jsp");
        dispatcher.forward(request, response);
    }

    public void doPost(HttpServletReqest request, HttpServletResponse response){
        Questionniare questionnaire = new Questionnaire();

        for(int i = 0; i < questionnaire.getQuestions().size(); ++ i){
            //set selected based on incoming parameter
            boolean selected = request.getParameter("q"+i).equals("yes")
            questionnaire.getQuestions().get(i).setSelected(selected);  
        }

        //set the updated form as a request attribute for re-render of JSP
        request.setAttribute("questionnaire", questionnaire);
        RequestDispatcher dispatcher = req.getRequestDispatcher("/keywords.jsp");
        dispatcher.forward(request, response);
    }
}

JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<form method="POST">
    <!--Iterate all questions and render the inputs-->
    <!--On initial load should show the defaults -->
    <!--After post should show the selections!-->
    <c:forEach items="${questionnaire.questions"} var="question" varStatus="loop">
        <p>
            ${question.question}
            <label><input type="radio" name="q{loop.index}" value="yes" 
                class="q0" ${question.selected ? 'checked' : ''}/>Yes</label>
            <label><input type="radio" name="q{loop.index}" value="no" 
                class="q0" ${! question.selected ? 'checked' : ''}/>No</label>
            <br/>
        </p>
    </c:forEach>
    <input class="btn btn-warning" type="submit" value="Search Keywords"/>
</form>

参考文献:

Java标准标记库(JSTL):http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html

JSP表达式语言(EL) http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html