创建Struts 配置文件 (struts-config.xml)
来源:互联网 发布:计算机视觉python 编辑:程序博客网 时间:2024/06/08 18:34
struts-config.xml 文件包含了ActionServlet 需要用来处理对应用请求的详细信息。为了练习,我们创建一个空壳的struts-config.xml 文件。
文件存储在<BaseDirectory>/webapps/register/WEB-INF/目录下,需要改变的是:
首先,添加/register 到<action>元素的 path 属性。ActionServlet 使用Web容器转发给它的URI来选择正确的Action 类。URI 和ActionMapping 的path 属性匹配。这里,请求给出的路径必须在去除前缀和后缀后和/register 匹配。前缀或后缀通常是/do/ 或者 .do。 我们的练习中,将后缀设置为.do。当URI 具有一个.do 扩展名,容器就知道将请求转发给ActionServlet。Struts会自动去除 扩展名,所以我们在配置时不必加上它们。
下一步添加:registerForm 到<action> 元素的 name 属性。<action> 元素使用name 属性来识别哪个ActionForm 将被创建,并将提交的表单组装给他。
然后,添加 app.RegisterAction 到<action> 元素的 type 属性。ActionServlet 使用这个属性来识别将用来处理请求的Action 类。
接下来,在<forward> 元素下,添加 success 到 name 属性,并且 /success.html 到 path 属性。最后,再在另一个<forward>下添加 failure 到 name 属性, /failure.html 到 path 属性。 这些元素将创建ActionForward 对象,我们将用它来选择程序的控制流。<forward> 元素定义了在RegisterAction中使用的逻辑名称之间的关联。
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="registerForm" type="app.RegisterForm" /></form-beans><action-mappings><action path="/register" type="app.RegisterAction"name="registerForm"><forward name="success" path="/success.html" /><forward name="failure" path="/failure.html" /></action></action-mappings></struts-config>
Struts框架将struts-config.xml 文件视为部署描述符使用。它使我们可以创建和改变ActionMapping 和路径的关联而不用重新编译java类。我们也可以改变页面之间的连接,而不改变JSP模板。
步骤4 创建success.html, failure.html, 以及register.jsp 页面存放在<Base Directory>/webapps/register目录下。
success.html
<HTML><HEAD><TITLE>成功</TITLE></HEAD><BODY>
恭喜你,注册成功了
<P><A href="register.jsp">继续注册吗?</A></P></BODY></HTML>failure.html<HTML><HEAD><TITLE>对不起</TITLE></HEAD><BODY>注册失败!<P><A href="register.jsp">还要试一试吗?</A></P></BODY></HTML>Register.jsp<%@ taglib uri="/WEB-INF/struts-form.tld" prefix="form"%><form:form action="register.do"> 用户名:<form:text property="username" /><br> 输入密码:<form:password property="password1" /><br> 再输一次:<form:password property="password2" /><br><form:submit value="Register" /></form:form>
整个程序的流程图如下
现在,试一下运行如何。 如果Tomcat没有运行,启动它。
在浏览器中输入以下地址:
http://localhost:8080/register/register.jsp
- 创建Struts 配置文件 (struts-config.xml)
- struts配置文件 struts-config.xml
- struts-config.xml配置文件
- struts-config.xml配置文件
- struts-config.xml配置文件
- struts-config.xml配置文件
- Struts的核心struts-config.xml配置文件
- Struts的核心struts-config.xml配置文件
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- Struts-config.xml配置文件讲解
- struts-config.xml配置文件详解
- 创建 RegisterAction
- POJ 1088 动态规划
- 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏
- 复习Struts2之控制标签与数据标签
- java.sql.SQLException: 关闭的连接
- 创建Struts 配置文件 (struts-config.xml)
- Android库项目的导出及其引用资源
- Data type conversions for API calls from Visual Basic
- 谈一谈我对java单继承和多继承的理解。
- Iphone之UITableView
- 跨域的理解与实现
- 【iOS-Cocos2d游戏开发之九】讲解CCSpriteBatchNode与TP工具的".pvr.ccz",".plist"共用的终极精灵优化及注意事项!
- ARRAYLIST VECTOR LINKEDLIST 区别与用法
- android系统学习(1)-ubuntu 11.10环境搭建