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’