用struts开发MVC程序入门(一)2003-11-14 9:48:46
1)将lib/commons-*.jar复制到支持j2ee服务器的WEB-INF/lib目录下,如果没有,自己创建一个。 2)将lib/struts.jar复制到支持j2ee服务器的WEB-INF/lib目录下。 3)将lib/struts-*.tld 复制到支持j2ee服务器的WEB-INF/目录下。 4)修改WEB-INF/web.xml在 5)生成一个空白的struts的核心配置文件struts-config.xml,将它放到WEB-INF目录下 "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> 经过上面的步骤,我们基本完成了struts-config.xml,如果大家用什么问题,可以参考http://jakarta.apache.org/struts/userGuide/installation.html官方安装指南。 二、写一个自己的struts应用程序 struts它的核心是所有和用户交互式的都是有HTML里的表签form,大家想想是不是,只有在form里尼才可以实现WEB的交互,所以它也从form开始,在jsp里 所有的参数获取都是通过request.getParameter()的方法得到的,struts用了更先进的方法,你只要在你的form-bean里写set和get的方法就可以从form 里取参数了。下面是一个form-bean,内容如下: sampleForm.java源文件 package sample; import org.apache.struts.action.ActionForm; public class SampleForm extends ActionForm { protected String theName; protected String thePassword; public SampleForm() { } public String getTheName() { return theName; } public void setTheName(String theName) { this.theName = theName; } public String getThePassword() { return thePassword; } public void setThePassword(String thePassword) { this.thePassword = thePassword; } } 这里面的的theName,thePassword都是和你的jsp程序里的form标签是对应的,你的jsp里至少有两个文本框,名字为"theName"和"thePassword" 下面是你jsp的源程序: sample.jsp 下面就要写你提交后的action这个servlet了, SampleAction.java内容如下: package sample; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.*; // Referenced classes of package com.ilovejsp.action.form: // SampleForm public class SampleAction extends Action { public SampleAction() { } public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { if(form instanceof SampleForm) { SampleForm theForm = (SampleForm)form; String user = theForm.getTheName(); String password = theForm.getThePassword(); request.setAttribute("user", user); request.setAttribute("password", password); return mapping.findForward("play"); } else { return null; } } } 其中 SampleForm theForm = (SampleForm)form; String user = theForm.getTheName(); String password = theForm.getThePassword(); 这一段就是从form里取值,怎么取你更本不用管,反正现在user和password已经得到了,后面两句是放到session里,供后面的jsp调用,你当然也可以将这段话换成 向数据库里写值的程序段,最后面的return mapping.findForward("play");相当于servlet里的response.redirect("jsp"),这儿的转向是找play的字符串,关于这个字符串 在struts-config.xml里面配置。 下面编译SampleForm.java和SampleAction.java c:\>javac -classpath %path%\servlet.jar;%path%\WEB-INF\lib\struts.jar SampleForm.java c:\>javac -classpath %path%\servlet.jar;%path%\WEB-INF\lib\struts.jar SampleForm.java 如果你对javac的命令不熟的话,建议先看一下javac的文档。 编译成功后将它们的class文件复制到WEB-INF\classes\sample目录下,大家看到我这用了package sample;这句话了。 下面就是配置struts-config.xml,配置好的struts-config.xml内容如下: "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> type="sample.SampleAction" scope="request" name="sampleForm" validate="false"> 大家可以对照前面的struts-config.xml对应一下看多了哪些东西。我简单的讲一下这里面的含义, 这是指定form的名字,以便在action里能找到,type是form的类文件。 type="sample.SampleAction" scope="request" name="sampleForm" validate="false"> 是指定了一个名字叫sample的action,和sample.jsp里的 |
|
|