ZhangLiHai.Com Blog


用struts开发MVC程序入门(一)

张利海 于 2003年05月06日 发表

一、struts的获取和安装

struts是jakarta组织开发的一种jsp的MVC的框架,MVC其实它的全称是Model-View_Controller(模型-视图-控制器),struts所起的作用其实是

一个控制器的作用,struts可以从http://jakarta.apache.org/struts/index.html获得详细信息,下载的地址为http://jakarta.apache.org/builds/jakarta-struts/release/v1.0.2/

最新版本是1.0.2,1.1的版本是beta版。下载完后按下面的步骤安装:

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在<web-app></web-app>中间加入如下的语句

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>application</param-name>

<param-value>ApplicationResources</param-value>

</init-param>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>validate</param-name>

<param-value>true</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<!-- Standard Action Servlet Mapping -->

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- Struts Tag Library Descriptors -->

<taglib>

<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>


<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>



<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

5)生成一个空白的struts的核心配置文件struts-config.xml,将它放到WEB-INF目录下

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">



<struts-config>

<action-mappings type="org.apache.struts.action.ActionMapping">

</action-mappings>

</struts-config>

经过上面的步骤,我们基本完成了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

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

</HEAD>

<BODY>

<FORM METHOD=POST ACTION="/sample.do">

username:<INPUT TYPE="text" NAME="theName"><BR>

password:<INPUT TYPE="password" NAME="thePassword"><br/>

<INPUT TYPE="submit">

</FORM>

</BODY>

</HTML>

下面就要写你提交后的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内容如下:

<?xml version="1.0" encoding="ISO-8859-1" ?>


<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config>

<form-beans>

<form-bean name="sampleForm" type="sample.SampleForm"/>

</form-beans>

<action-mappings type="org.apache.struts.action.ActionMapping">

<action path="/sample"

type="sample.SampleAction"

scope="request"

name="sampleForm"

validate="false">

<forward name="play" path="/success.jsp" redirect="true" />

</action>

</action-mappings>

</struts-config>

大家可以对照前面的struts-config.xml对应一下看多了哪些东西。我简单的讲一下这里面的含义,

<form-beans>

<form-bean name="sampleForm" type="sample.SampleForm"/>

</form-beans>

这是指定form的名字,以便在action里能找到,type是form的类文件。

<action path="/sample"

type="sample.SampleAction"

scope="request"

name="sampleForm"

validate="false">

<forward name="play" path="/success.jsp" redirect="true" />

</action>

是指定了一个名字叫sample的action,和sample.jsp里的<FORM METHOD=POST ACTION="/sample.do">sample对应,type是这个类的真实名字。

后面<forward name="play" path="/success.jsp" redirect="true" />是在SampleAction.java最后指定redirect到的jsp的地址,下面我们

创建success.jsp,

success.jsp如下:

<HTML>

<BODY>

username:<%=request.getAttribute("user")%><BR>

password:<%=request.getAttribute("password")%>

</BODY>

</HTML>

这个jsp很简单,就是显示值,看user和password是不是真的被struts得到了。

但创建完所有的文件后,大家重新启动自己的服务器,切记,每次改完struts-config.xml一定要重新启动服务器。

然后就可以在浏览器里输入http://localhost/sample.jsp

就可以执行了。从这个例子大家应该对struts有了一个了解了吧,但是这里并没有用model,模型,用了它,才算真正发挥了struts的威力,我在后面将会谈到。

在现在,还没有关于struts的中文文章,希望它能对初学者有用。

新版本Blog中有更多内容
Copyright (C)2002-2007 All Rights Reserved Powered By:ZhangLiHai.Com