CSDN博客

img patrickip

JBoss-IDE 1.2.2 簡介 (Struts) 2

发表于2004/2/29 13:25:00  922人阅读

分类: JBoss-IDE

JBoss-IDE 1.2.2 簡介 (Struts) 2:

 

步驟:

5. 完成後我們會有兩個檔案, 先做 ComputeForm , Package Explorer 擴張 ‘src’ -> ‘tutorial.web’, 雙擊 ‘ComputeForm’, 在右手邊視窗 ComputeForm ‘public class ComputeForm’ comment 中打 ‘@’, 然後等一會便可看到剛才輸入的 Struts Form Template, 選了它之後按 enter, JBoss-IDE 便會自動幫我們填入所需的 Template, 真的是十分好用!

然後可刪除 ‘extends’ ‘implements’, 現在我們不需要, 只留下 ‘name’, 填上 ‘ name=“computeForm” ’, 而我們會檢查輸入資料, 所以在加上 num attribute 後的 comment 中要加上 ‘@struts.validator type=“required, integer”’, 詳細程式如下:

package tutorial.web;

 

import org.apache.struts.validator.ValidatorForm;

 

/**

 * @author patrick.ip

 *

 * @struts.form name="computeForm"

 */

public class ComputeForm extends ValidatorForm

{

  private String num;

 

  /**

   * Sets the query attribute of the ComputeForm object

   *

   * @struts.validator type="required,integer"

   */

  public void setNum(String num){

      this.num = num;

  }

  public String getNum(){

      return num;

  }

}

ComputeAction 也是差不多, 程式如下:

package tutorial.web;

 

import java.io.IOException;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForm;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * @author patrick.ip

 *

 * @struts.action name="computeForm"

 *                path="/computeFibo"

 *                scope="request"

 *                validate="true"

 *                parameter=""

 *                input="/index.jsp"

 *

 * @struts.action-forward name="success"

 *                        path="/Compute"

 */

public class ComputeAction extends Action{

 

public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response)

        throws IOException, ServletException{

        ComputeForm cf = (ComputeForm)form;

        String num = request.getParameter("num");

        request.getSession().setAttribute("num", num);

        return mapping.findForward("success");

    }

}

然後我們修改一下 ComputeServlet 中的 doPost 方法:

try {

  Fibo bean = home.create();

  int limit = 0;

  String value = request.getParameter("limit");

 

  // added by pat

  if (( null == value) || ("".equals(value))) {

      value = (String) request.getSession().getAttribute("num");

  }

 

  if (value != null) {

      try {

          limit = Integer.parseInt(value);

      }

      catch (Exception e) {

      }

}

最後 compile 一下 project, 右擊 tutorial project -> ‘Build Project’

0 0

相关博文

我的热门文章

img
取 消
img