Struts-config.xml中的配置信息:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="com.bjpowernode.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
</action-mappings>
</struts-config>
<form-beans>配置的是装载表单数据的java类的路径。<action-mapping>标签中所path属性值为请求路径 ,type表示真正的请求对象,也就是上文中提到的RealAction。name 为装载表单数据的AcitonForm类,对应于相应的<form-beans>。<forward>标签对应便是各种条件下的跳转页面了。
转载页面表单信息的LoginActionForm(即上文中提到的ActionForm):
package com.bjpowernode.struts;
import org.apache.struts.action.ActionForm;
@SuppressWarnings("serial")
public class LoginActionForm 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统一控制的LoginAction(即上文中提到的RealAction)中的代码,该java类具有一个execute函数,所有的业务逻辑操作以及操作的返回结果及决定跳转界面的操作均在该函数中进行:
package com.bjpowernode.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
if("admin".equals(username) && "admin".equals(password)){
//登录成功
return mapping.findForward("success");
}else{
//登录失败
return mapping.findForward("error");
}
}
}
最后是客户端代码:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input tyep="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
在客户端中我们指定的action是“login.do”,以“.do”结尾的页面就是我们在web.xml文件中规定的哪些请求会被提交到ActionServlet上。在上文中空谈了很多Struts的执行机制大家可能还不能很好的理解,下面以这个登录例子为例我们来具体解释下Struts到底是怎样一个执行流程。
1. 当Tomcat启动的时候根据配置文件中的<loadon-on-startup>标签中的值,ActionServlet伴随着Tomcat的启动而被实例化。
2. ActionServlet被实例化后会加载struts-config.xml文件,将该文件中的配置信息以键值对的形式读到内存中。
3. 当login页面被提交的时候由于该页面的action结尾是以.do结束的(也就说它符合在web.xml文件中规定的受struts规范的条件,该条件简单的来说就是只要请求是以.do结尾的就都受struts约束),该请求会被提交到ActionServlet。
4. 请求被提交到ActionServlet后,ActionServlet会首先对该URL进行截取(将.do去掉),然后根据截取后的path在已经加载到内存中的struts-config.xml找到对应的path,同时实例化配置文件中配置好的具体的ActionForm对象和RealAction对象,同时将表单信息赋给ActionForm对象。
5.根据配置文件中的type属性找到真正的请求类LoginAction后,由该类完成业务逻辑的各种操作,最后返回ActionForward对象给ActionServlet,最后ActionServlet根据返回情况从struts-config.xml文件中找到对应的跳转页面实现页面导航的统一控制。
