myeclipse 开发struts(三)

来源:互联网 发布:什么是大非农数据 编辑:程序博客网 时间:2024/05/22 03:36

 建立一个FORM,为了很好的理解struts 我们不用eclipse来自动生成 我们手写代码!(当然参考了struts in action)

 首先建立一个form:

我们只想建立一个欢迎页面 在password 输入 www并提交后页面跳转到一个成功页面(这应用也太简陋了点。。。)

 

建立FORM:

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 04-04-2009
 *
 * XDoclet definition:
 * @struts.form name="lianxiForm"
 */
public class LianxiForm extends ActionForm {
 /*
  * Generated fields
  */

 /** password property */
 private String password;

 /** username property */
 private String username;

 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }

 /**
  * Returns the password.
  * @return String
  */
 public String getPassword() {
  return password;
 }

 /**
  * Set the password.
  * @param password The password to set
  */
 public void setPassword(String password) {
  this.password = password;
 }

 /**
  * Returns the username.
  * @return String
  */
 public String getUsername() {
  return username;
 }

 /**
  * Set the username.
  * @param username The username to set
  */
 public void setUsername(String username) {
  this.username = username;
 }
}

 

我没有给出package 建立类可以使用myeclips自动建立

 

建立后就去strus-config.xml里面配置这个form:

<form-bean name="lianxiForm" type="com.yourcompany.struts.LianxiForm" />

注意name属性的命名 在后面我们会用到.

 

 

form建好后在建action。:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yourcompany.struts.LianxiForm;
public class LianxiAction extends Action{

 public ActionForward execute(ActionMapping mapping,
   ActionForm form,
   HttpServletRequest req,
   HttpServletResponse res){
  LianxiForm rf = (LianxiForm) form;
  String username = rf.getUsername();
  String password = rf.getPassword();
  ActionForward forward = new ActionForward();
  if (password.equals("www")) {

   forward = mapping.findForward("success");
   }
   
    else{
   forward = mapping.findForward("failure");
   }
  return (forward);
 }
}

 

逻辑有点过于简单了..但是用于练习就不管那么多了

注意 里面的两个参数

  success 和failure 这是两个forward 我们也会用到的。

 

原创粉丝点击