利用MyEclipse开发Struts+Hibernate的应用(二)

发表于:2007-07-01来源:作者:点击数: 标签:
9、切换到struts-config.xml文件的编辑界面,通过大纲视图选择action-mappings,点击右键-New Form,Action and JSP: 首先是创建FormBean的配置信息,具体配置如图,同时添加vipname和viptitle两个String类型的属性: 点击下一步进入Action的配置,如图所示
 

9、切换到struts-config.xml文件的编辑界面,通过大纲视图选择action-mappings,点击右键-New Form,Action and JSP:

首先是创建FormBean的配置信息,具体配置如图,同时添加vipname和viptitle两个String类型的属性:

点击下一步进入Action的配置,如图所示:

切换Optional Details的标签到Forwards,加入suclearcase/" target="_blank" >ccess的跳转到AddVipData.jsp页面:

具体设置如图:

设置好了以后单击 完成,这样就完成了Struts中的FormBean 和Action的创建。

在struts-config.xml的编辑界面中,出现如下图所示图片:

可以清楚的看到jsp 、form 、action之间的关系。

10、修改struts的资源文件com.xiebing.struts.ApplicationResources的内容为:

errors.footer=</UL><HR>

errors.header=<H3><FONT color="red">Validation Error</FONT></H3>You must correct the following error(s) before proceeding:<UL>

error.vipname.required=<LI>Need a vipname</LI>

error.viptitle.required=<LI>Need a viptitle</LI>

11、接下来修改action类:AddVipdata,和Formbean类:VipdataForm修改后的代码如下:

com.xiebing.action.AddVipdata类

package com.xiebing.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.xiebing.formbean.VipdataForm;

import com.xiebing.hibernate.Vipdata;

import com.xiebing.hibernate.VipService;

public class AddVipdata extends Action {

       /**

        * Method execute

        * @param mapping

        * @param form

        * @param request

        * @param response

        * @return ActionForward

        */

       public ActionForward execute(

              ActionMapping mapping,

              ActionForm form,

              HttpServletRequest request,

              HttpServletResponse response) {

              VipdataForm AddVipdataForm = (VipdataForm) form;

              if (AddVipdataForm.getVipname() != null)

              {

                     Vipdata vipdata = new Vipdata();

                     vipdata.setViptitle(AddVipdataForm.getViptitle());

                     vipdata.setVipname(AddVipdataForm.getVipname());

                     VipService.getInstance().addVipdata(vipdata);

                     AddVipdataForm.clear();

              }

              return mapping.findForward("success");

       }

}

com.xiebing.formbean.VipdataForm

package com.xiebing.formbean;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

public class VipdataForm extends ActionForm {

       /** vipname property */

       private String vipname;

       /** viptitle property */

       private String viptitle;

       // --------------------------------------------------------- Methods

       /**

        * Method validate

        * @param mapping

        * @param request

        * @return ActionErrors

        */

       public ActionErrors validate(

              ActionMapping mapping,

              HttpServletRequest request) {

              ActionErrors errors = new ActionErrors();

              if (vipname == null || vipname.length() < 1)

              {

                     errors.add("vipName", new ActionError("error.vipname.required"));

              }

              if (viptitle == null || viptitle.length() < 1)

              {

                     errors.add("vipTitle", new ActionError("error.viptitle.required"));

              }

              return errors;

       }

       public void reset(ActionMapping mapping, HttpServletRequest request) {

              clear();

       }

       public String getVipname() {

              return vipname;

       }

       public void setVipname(String vipname) {

              this.vipname = vipname;

       }

       public String getViptitle() {

              return viptitle;

       }

       public void setViptitle(String viptitle) {

              this.viptitle = viptitle;

       }

       public void clear()

       {

              viptitle = null;

              vipname  = null;

       }

}

至此,所以的编码工作已经全部完成,接下来要用MyEclipse来发布web应用

12、点击发布J2EE应用的工具栏图标:

弹出如下界面,点击Add按钮:

在接下来的窗口中,Server选择配置好的服务器,我这里选择:Tomcat5,然后点击完成.这样我们就完成了程序的发布,很简单,也很方便.

然后启动Tomcat5来运行我们的程序:


之后就可以通过浏览器来访问我们的程序了.还不赶快点呀!

原文转自:http://www.ltesting.net