在同一个JSP页面提问

问题描述 投票:0回答:1

我在创建一个应用程序时遇到问题。例如,我有一个字符串行列表或数组 - 这些都是问题。我有一个servlet和JSP页面,在JSP页面中有一个“下一个”按钮,按下下一个按钮时,必须在JSP中打印一个问题。

请给我一些建议,我怎样才能完成这项工作。

我有一个 struts 动作类,其中有我的代码来放置问题:

public class SecondWriteAction extends Action {

    /**
     * 
     */
    private static final String SUCCESS = "success";

    /**
     *
     */
    protected int count = 0;

    /**
     * 
     */
    protected Collection<QuestionsBean> questionsBeansCollection = new ArrayList<QuestionsBean>();

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();

        if (session.getAttribute("questioningTimeCount") == null){
            session.setAttribute("questioningTimeCount", request.getParameter("timeCount"));
        }

        int timeCount = Integer.parseInt(session.getAttribute("questioningTimeCount").toString());

        WriteFormBean formBean = (WriteFormBean) form;
        QuestionsBean questionsBean = new QuestionsBean();

        questionsBean.setSentence(formBean.getQuestion());
        questionsBeansCollection.add(questionsBean);

        if (session.getAttribute("countFromJsp") != null) {
            int countFromJsp = Integer.parseInt(session.getAttribute("countFromJsp").toString());

            if (countFromJsp == 1){
                session.setAttribute("questionsBeansCollection", questionsBeansCollection);
            }

            if (countFromJsp <= 0) {
                questionsBeansCollection.clear();
                questionsBeansCollection = new ArrayList<QuestionsBean>();
                count = 0;
            }
        }

        QuestionsHandler handler = QuestionsHandler.getInstance(request.getSession().getServletContext().getRealPath("/data/sentences_" + timeCount));

        count = count + 1;
        request.setAttribute("question", handler.getQuestions().get(count));
        request.setAttribute("count", count);

        RequestDispatcher rd = request.getRequestDispatcher("/student/secondWrite.jsp");
        rd.forward(request, response);

        return mapping.findForward(SUCCESS);
    }
}

在 JSP 页面中,我对它们进行计数,当提问完成后,我尝试转发到其他 JSP 页面。

<%
    if (request.getAttribute("count") != null){

        int count = 11 - Integer.parseInt(request.getAttribute("count").toString());
        out.print("<p>Liko klausimų: " + count + "</p>");
        session.setAttribute("countFromJsp", count);
        if (count == 0){                                            
            if (Integer.parseInt(session.getAttribute("questioningTimeCount").toString()) > 1){                                                
                %>
                <script type="text/javascript">
                        $.post('update', function(data) {});
                </script>                                                
                <%                                                                                                
            } else {   
                //response.sendRedirect("content/informantForm.jsp");                                                                                          
                //RequestDispatcher rd = request.getRequestDispatcher("/content/informantForm.jsp");
                //rd.forward(request, response);
            }
        }
    }
%>

问题是我不能。如果我在 JSP 页面中使用

RequestDispatcher
,则会出现异常

提交回复后无法转发

如果我尝试使用 jQuery 访问 servlet,一切都可以,但是

RequestDispacher
不起作用,并且我无法从 servlet 重定向到其他 JSP 事件。

有什么想法吗?

jsp servlets struts-1
1个回答
0
投票

如果我在 JSP 页面中使用 RequestDispatcher,则会出现异常

cannot forward after responce has been commited

JSP 作为一种视图技术,隐含地是 HTTP 响应的一部分。 JSP 中的每个模板行都会写入响应。为了能够更改响应,响应应该完全保持不变且空白。但 JSP 内部的情况不一定如此。

将该 Java 代码移至将在 JSP 之前运行的操作或 servlet 类。无论如何,Java 代码属于 JSP 文件。

© www.soinside.com 2019 - 2024. All rights reserved.