2011-09-13 17 views
1

1つのアプリケーションを作成する際に問題があります。たとえば、リストや配列の文字列があります。これは質問です。私はサーブレットとJSPページを持っています。JSPページには「次へ」ボタンがあり、次のボタンが押されたときにJSPで1つの質問が印刷されます。同じJSPページでの質問

どうすればこの作業を行うことができますか教えてください。

私は疑問に置くために私のコード持っているStrutsアクションクラスがあります:JSPページで

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); 
    } 
} 

を私はそれらをカウントし、尋問が終了したとき、私はohter 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を使用している場合、私は例外

を取得responce後に転送することはできません、私はjQueryを使ってサーブレットにアクセスしようとした場合、それはすべてOKだけどRequestDispacherは仕事と私はありません

をコミットされていますサーブレットから他のJSPイベントにリダイレクトできません。

なぜでしょうか?

+0

試しましたか?あなたがしたことを示してください、そして、あなたを助けてくれるでしょう。 –

答えて

0

私はJSPページでのRequestDispatcherを使用している場合、私はビューテクノロジーとして例外

cannot forward after responce has been commited 

JSPを取得するには、暗黙的にHTTPレスポンスの一部です。 JSPのすべてのテンプレート行が応答に書き込まれます。応答を変更できるようにするには、応答を完全に変更せずに空白にする必要があります。しかし、これは必ずしもJSP内では当てはまりません。

Javaコードを、JSPよりはるか遠くまで実行するアクションまたはサーブレットクラスに移動します。 Javaコードdoesn'tは、とにかくJSPファイルに属します。

関連する問題