Struts由入门到精通(第一章)- 第一个struts应用程序(二)

本文介绍了Struts框架的基本配置过程及其在实际项目中的应用。包括配置文件struts-config.xml的详细解析,以及如何通过配置文件实现不同组件的组装与交互。

(2) 创建Struts框架的配置文件

正如前面提及的,Struts框架允许把应用划分成多个组件,提高开发速度。而Struts框架的配置文件struts-config.xml可以把这些组件组装起来,决定如何使用它们。下面这个实例的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.1//EN"

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

<!--

     This is the Struts configuration file for the "Hello!" sample application

-->

<struts-config>

    <!-- ======== Form Bean Definitions =================================== -->

    <form-beans>

        <form-bean name="SearchForm " type="MyStruts. SearchForm "/>

    </form-beans>

  <!-- ========== Action Mapping Definitions ============================== -->

  <action-mappings>

    <!-- Say Hello! -->

    <action    path      = "/search"

               type      = " MyStruts.SearchAction"

               name      = " SearchForm "

               scope     = "request"

               validate  = "true"

               input     = "/query.jsp"

     >  

        <forward name="success " path="/result.jsp" />

<forward name="failure" path="/query.jsp" />

    </action>

  </action-mappings>

  <!-- ========== Message Resources Definitions =========================== -->

  <message-resources parameter="hello.application"/>

</struts-config>

 

 

以上代码对MyStruts应用的SearchFormSearchAction和消息资源文件进行了配置,首先通过<form-bean>元素配置了一个ActionForm Bean,名叫searchForm,它对应的类为MyStruts. SearchForm

<form-bean name=" searchForm " type="MyStruts.SearchForm "/>

接着通过元素配置了一个Action组件:

<action    path  = "/search"

               type      = " MyStruts.SearchAction"

               name      = " SearchForm "

               scope     = "request"

               validate  = "true"

               input     = "/query.jsp"

     >  

        <forward name="success " path="/result.jsp" />

<forward name="failure" path="/query.jsp" />

    </action>

<action>元素的path属性指定请求访问Action的路径。

type属性指定Action的完整类名。

name属性指定需要传递给ActionActionForm Bean

scope属性指定ActionForm Bean的存放范围。

validate属性指定是否执行表单验证。

input属性指定当表单验证失败时的转发路径。

<action>元素还包含一个<forward>子元素,它定义了一个请求转发路径。

本例中的 <action>元素配置了SearchAction组件,对应的类为MyStruts.SearchAction,请求访问路径为"search",当Action类被调用时,Struts框架应该把已经包含表单数据的SearchForm Bean传给它。SearchForm Bean存放在request范围内,并且在调用Action类之前,应该进行表单验证。如果表单验证失败,请求将被转发到接收用户输入的网页query.jsp,让用户纠正错误。

struts-config.xml文件最后通过元素定义了一个Resource Bundle <message-resources parameter="hello.application"/>

<message-resources>元素的parameter属性指定Resource Bundle使用的消息资源文件。本例中parameter属性为"MyStruts.application",表明消息资源文件名为"application.properties",它的存放路径为WEB-INF/classes/MyStruts/application.properties

 

5. 运行第一个Struts应用程序

 

右键点击MyStruts工程根目录,点击菜单中的Tomcate project->update context definition,将工程部署进tomcat,成功后会提示操作成功。

  点击菜单栏中的雄猫图标启动tomcat,然后在IE地址栏中输入http://localhost:8080/ MyStruts /search.do,我们会看到query.jsp的页面内容,录入学号,会显示不同的结果。运行结果如下图:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  项目建立:

 

  正式开发前,需要在Tocmat(我的tomcat装在c:\tomcat)中建立此项目。比较快的一种建立方式为:在C:\tomcat\ webapps下新建目录test,再将C:\tomcat\webapps\struts-example下的WEB-INF目录拷贝到test目录下,然后将test\WEB-INF下的srcclasses目录清空,以及struts-config.xml文件中内容清空即可。这样,我们需要的 Struts类包及相关的配置文件就都齐了。

 

  开发时,将JSP文件放在test目录下,Java原文件放在test\WEB-INF\src下,编译后的类文件放在test\WEB-INF\classes下。

 

  注册页面:reguser.jsp

 

<%@ page contentType="text/html;charset=UTF-8" language="java"

%>

 

<%@ taglib uri="/WEB-INF/Struts-bean.tld" prefix="bean"

%>

 

<%@ taglib uri="/WEB-INF/Struts-html.tld" prefix="html"

%>

 

<html:html locale="true">

 

<head>

 

<title>RegUser</title>

 

<html:base/>

 

</head>

 

<body bgcolor="white">

 

<html:errors/>

 

<html:form action="/regUserAction" focus="logname">

 

 

<table border="0" width="100%">

 

<tr>

 

<th align="right">

 

Logname:

 

</th>

 

<td align="left">

 

<html:text property="logname" size="20" maxlength="20"/>

 

 

</td>

 

</tr>

 

<tr>

 

<th align="right">

 

Password:

 

</th>

 

<td align="left">

 

<html:password property="password" size="20" maxlength="20"/>

 

 

</td>

 

</tr>

 

<tr>

 

<th align="right">

 

E-mail:

 

</th>

 

<td align="left">

 

<html:password property="email" size="30" maxlength="50"/>

 

 

</td>

 

</tr>

 

<tr>

 

<td align="right">

 

<html:submit property="submit" value="Submit"/>

 

</td>

 

<td align="left">

 

<html:reset/>

 

</td>

 

</tr>

 

</table>

 

</html:form>

 

</body>

 

</html:html>

 

  此JSP页面不同于普通的JSP页,因为它大量运用了taglib,这些taglib对初学者而言,可能难于掌握,可这却是Struts的精华之一。灵活运用,将大大提高开发效率。

Struts-config.xml

 

<Struts-config>

<form-beans>

<form-bean name="regUserForm" type="org.cjea.Struts.example. RegUserForm "/>

 

</form-beans>

<action-mappings>

<action path="/regUserAction" type=" org.cjea.Struts.example.RegUserAction "

attribute=" regUserForm " scope="request" validate="false">

<forward name="failure" path="/ messageFailure.jsp"/>

<forward name="success" path="/ messageSuccess.jsp"/>

  Struts的核心是Controller,即ActionServlet,而ActionServlet 的核心就是Struts-config.xmlStruts-config.xml集中了所有页面的导航定义。对于大型的WEB项目,通过此配置文件即可迅速把握其脉络,这不管是对于前期的开发,还是后期的维护或升级都是大有裨益的。掌握Struts-config.xml是掌握Struts的关键所在。

 

FormBeanRegUserForm

 

package org.cjea.Struts.example;

import javax.Servlet.http.HttpServletRequest;

import org.apache.Struts.action.ActionForm;

import org.apache.Struts.action.ActionMapping;

 

public final class RegUserForm extends ActionForm{

 

private String logname;

private String password;

private String email;

 

public RegUserForm(){

logname = null;

password = null;

email = null;

}

 

public String getLogName() {

return this.logname;

}

public void setLogName(String logname) {

this.logname = logname;

}

public void setPassWord(String password) {

this.password = password;

}

public String getPassWord() {

return this.password;

}

public void setEmail(String email) {

this.email = email;

}

public String getEmail() {

return this.email;

}

 

public void reset(ActionMapping mapping, HttpServletRequest request)

{

logname = null;

password = null;

email = null;

}

}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值