2012-01-01 3 views
0

私は、ユーザーインターフェイスから入力したエントリに基づいてテキストファイルを作成するアプリケーションを用意しています。 私はこのためにStruts1を選択しました。私はほとんどの機能を完了することができました。しかし、 テキストファイルを私のJSPのユーザーエントリに基づいて保存する部分は、私が苦労していることです。 SCANDATAを入力した後http://www.image-share.com/ijpg-1178-105.html 3.'Submitページ1' のエントリに基づいてスキャンデータのためのhttp://www.image-share.com/ijpg-1178-104.html 2.'Askユーザーインターフェース上のページの流れrequest.getAttributeの値を使用してJSPをリロードする方法

1.'Acceptのユーザーエントリは以下のとおりです。 'http://www.image-share.com/ijpg-1178-106.html (私はセッション変数を使って画像のヌル値を真っ直ぐにできました.Daveに感謝します) メッセージは、このポスト検証のようなヌルエントリで見られます。 私の質問は次のとおりです。 ユーザーが2ページの「スキャンデータ」を入力し、同じJSPを使用してさらに多くのスキャンデータ値を入力するというシナリオがあるように、私は、ボタンクリックでJavaScript を使用してページをリロードすることについて考えていました。それは正しいアプローチですか?

このため、関連するコードは、私がLoginAction.java

HttpSession session = request.getSession(); 
session.setAttribute("message", textFile); 
session.setAttribute("itemname",loginForm.getItemName().trim()); 
session.setAttribute("lotnumber",loginForm.getLotNumber().trim());  
session.setAttribute("godownname",loginForm.getStockGodown().trim());  
+0

'TextWriter.java'はアクションクラスではありません。フォームです。それがどう関係しているのかは不明です。 –

+0

正直なところ、これをすべてやり遂げるのはかなり難しく、根本的な問題が何であるかは完全にわかりません。 –

+0

@DaveNewton申し訳ありませんが、私の間違いだと指摘してくれてありがとうございます。私は正しいアクションクラスを追加しました。私が提供してくれたスクリーンプリントを見ることができるようになることを願っています。明確な質問に必要なものについて私に指示してください。 –

答えて

0

(未解答の内部でこれを使用

<html:form action="txtwriter">  
    <% String itemname = (String)session.getAttribute("itemname"); %> 
<% String lotnumber = (String)session.getAttribute("lotnumber"); %> 
<% String godownname = (String)session.getAttribute("godownname"); %> 
<br/> 
<% String message = (String)session.getAttribute("message");  
session.setAttribute("theFileName", message); %> 
    Filename : <%= message %> 
<br/> Item Name :<%= itemname %> 
    <br/> Lot Number :<%= lotnumber %> 
    <br/> Godown Name :<%= godownname %> 
<br/> <bean:message key="label.scandata"/> 
<html:text property="scanData" ></html:text>  
<html:errors property="scanData"/> 
<br/>  
<html:submit/> 
    /* How should the submit button handle the onClick event so that when the users click 
    after entering the text. 
1. The entered text must be populated in the text file using a different action class. (I have this part working) 
    2.They must be on the same jsp with the scanData text box cleared waiting for the next user  entry into that text 
box so that this subsequest entry can also be entered into the text file. 
Is there a way i can empty the 'scanData' textbox by accessing it by name inside my action so that i can empty it from my action class? 
(I am looking for this  answer) 

*/


であり、リファクタリングが、助けようとしている他の人のためのテストされていないコード)

これはリファクタリングされたアクションで、実際に何が起こっているのかを簡単に確認できます。元のコードは推論するのが難しいです。 trim()機能は、冗長性を避けるために、アクションフォームの設定者に移動されます。

public class LoginAction extends Action { 

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
     LoginForm loginForm = (LoginForm) form; 
     if (invalidForm(loginForm)) { 
      return mapping.findForward("failure"); 
     } 

     String fileName = createFile(loginForm); 

     request.setAttribute("message", fileName); 
     request.setAttribute("itemname", loginForm.getItemName()); 
     request.setAttribute("lotnumber", loginForm.getLotNumber()); 
     request.setAttribute("godownname", loginForm.getStockGodown()); 

     return mapping.findForward("success"); 
    } 

    private String createFile(LoginForm loginForm) throws IOException { 
     ServletContext context = getServlet().getServletContext(); 
     String driveName = context.getInitParameter("drive").trim(); 
     String folderName = context.getInitParameter("foldername").trim(); 

     String pathName = driveName + ":/" + folderName; 
     new File(pathName).mkdirs(); 

     String fileNamePath = pathName + createFileName(loginForm); 
     ensureFileExists(fileNamePath); 

     return fileNamePath; 
    } 

    private void ensureFileExists(String fileNamePath) throws IOException { 
     boolean fileExists = new File(fileNamePath).exists(); 
     if (!fileExists) { 
      File file = new File(fileNamePath); 
      file.createNewFile(); 
     } 
    } 

    private boolean invalidForm(LoginForm loginForm) { 
     return loginForm.getItemName().equals("") 
       || loginForm.getLotNumber().equals("") 
       || loginForm.getStockGodown().equals(""); 
    } 

    private String createFileName(LoginForm loginForm) { 
     return loginForm.getItemName() + "_" 
       + loginForm.getLotNumber() + "_" 
       + loginForm.getStockGodown() + "_" 
       + createFileNameTimeStamp() + ".txt"; 
    } 

    private String createFileNameTimeStamp() { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' hh.mm.ss z"); 
     String dateTime = sdf.format(Calendar.getInstance().getTime()); 
     String[] tempDateStore = dateTime.split("AD at"); 
     return tempDateStore[0].trim() + "_" + tempDateStore[1].trim(); 
    } 

} 
関連する問題