引子:http://blog.csdn.net/eiwang/archive/2007/12/10/1927019.aspx
三、开始工作
在 WebContent 下建立 index.jsp ,建立方式如图。
index.jsp 的内容如表,我们暂时不分析。
<%@ page contentType = "text/html; charset=UTF-8" %> < html > < head > <title>Example by Doer Liu@UTStarcom sz </title> </ head > < body > This is my JSP page. < br > < form name = "userInfoForm" action = "login.do" method = "post" > 用户名 : < input name = "username" type = "text" /> 密码 : < input name = "password" type = "password" > < input name = "sub" type = "submit" value = " 增加 " /> < input name = "res" type = "reset" value = " 重置 " /> </ form > </ body > </ html > |
此时就可以运行该工程,忙了这么久,看看效果吧。
运行方式:右键点击 index.jsp ,选择 Run/Debug As à Run on Server ,在弹出窗口中默认我们使用的 Tomcat Server ,点击 finish 完成。可以看到 eclipse 中内嵌的浏览器显示我们的网页。其中表单的输入在我们的工程中将得到输入数据(用户名和密码),这些数据会传给我们将要建立的 Action 处理。
现在来看看如何建立我们的 Action 。在 src 下新建一个 package (包)名为 action 用于保存响应 Web 请求的 Action 类。在 action 包下新建 Action 类 LoginAction ( action.LoginAction )如下,注意类的继承关系。
package action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; import org.springframework.web.struts.ActionSupport;
// 我们继承 spring 提供的 Action 衍生类 org.springframework.web.struts.ActionSupport public class LoginAction extends ActionSupport {
public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward( "success" );
} } |
但是现在 index.jsp 的内容怎么和 LoginAction 的数据匹配呢,我们看到 LoginAction 的 execute 方法有一个属性 ActionForm ,于是我们建立一个类 forms.UserInfoForm 如下,继承 ActionForm 。
package forms; import org.apache.struts.action.ActionForm; public class UserInfoForm extends ActionForm { private String username; private String password;
public String getUsername() { return username; } public void setUsername(String username) { this .username = username; }
public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } }
|
有了两个头,又有了保持内容的类,现在看看我们如何用 struts 把他们联系起来吧。
现在需要在 WEB-INF 下建立文件 struts-config.xml 。其中 form-beans 定义了表单是如何映射的,这里用我们刚刚定义的 forms. UserInfoForm 。
<? 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" >
< struts-config > < form-beans > < form-bean name = "userInfoForm" type = "forms.UserInfoForm" /> </ form-beans >
< action-mappings > < action attribute = "userInfoForm" path = "/login" input = "/index.jsp" type = "org.springframework.web.struts.DelegatingActionProxy" name = "userInfoForm" scope = "session" validate = "false" > < forward name = "success" path = "/success.html" /> </ action > </ action-mappings > </ struts-config > |
在 < action-mappings > 中定义了我们的 Action 。它的属性 attribute 指出 Action 的内容输入是我们自定义的 ActionForm , path 给 Action 赋予一个路径, input 指明只接受 index.jsp 的输入, < forward 标签定义了当 Action 返回 "success" 的时候,将定向到 /success.html 这个网页。 最重要的是 type ,它定义了这个处理这个请求的 Action 类,本来应该是我们自定义的 LoginAction ,但我们却用了 spring 的一个 Action ,为什么?因为我们要用 Spring 管理我们自定义的 Action 。看, struts 和 Spring 在这里就开始连接起来了。
但还有两个问题, Struts 和 Spring 又是如何知道对方的存在,如何沟通呢? Spring 如何知道把控制权交给我们自定义的 LoginAction 呢?
我们先来解决第一个问题, web.xml 是 Tomcat 这些应用服务器管理的,因此我们在这里将 struts 和 Spring 配置联系起来。这是整个 web.xml 。请看注释。
<? xml version = "1.0" encoding = |