myeclipse 开发struts(三)

 建立一个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 我们也会用到的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值