Struts运行原理



先介绍最基本的Struts组件:ActionServlet,这里相当于总控制器

ActionForm,表单,用于存放数据

action,相当于分控制器,用于执行具体操作,比如有loginAction,loginCheckAction,这里其实就是以前写过的servlet

model,和传统mvc中的Model大体相同,主要利用各种service执行逻辑操作



接下来假设有一台浏览器发出http请求,比如URL是http://localhost:8080/webApplication/Action.

浏览器首先到自己电脑hosts中寻找localhost匹配ip地址,这里当然是127.0.0.1,然后将请求发到web服务器,我们假设服务器是Tomcat.服务器接收到请求后首先解析主机名,再解析web应用名称,再解析资源,也就是要寻找的页面。在以往普通servlet的服务器中,web服务器会直接把任务请求发送给相应的servlet页面或jsp页面,而struts会把请求先发给ActionServlet总控制器(这个类是由struts框架提供的,我们只需配置即可,配置由struts-config.xml文件进行配置ActionForm、action以及它们的对应关系).ActionServlet查询该配置文件把浏览器的用户数据填给表单ActionForm,要十分注意的是:这里的表单类(继承自ActionForm)的填充数据是从浏览器请求页面获得的,利用get方法返回,get方法的名字需要确定,比如从请求页面属性名为userName,则该表单类的get方法必须为getuserName().   然后调用指定的的Action,Action拿到任务后会从表单中获取数据并调用相应的model来完成具体的任务并把结果返回给ActionServlet.ActionServlet拿到返回结果后再根据此结果去查询struts-config.xml,然后跳转到相应查询到的页面,该页面送给web服务器,服务器以http响应的方式送还给浏览器,从而完成一次任务的处理。


更为具体的过程:服务器启动后,根据web.xml加载ActionServlet读取struts-config.xml文件内容到内存。 
以登陆为例:第一次进login.jsp会先将请求(post形式,请求URL为login.do,因为通常ActionServlet的过滤形式就是.do,所以在请求到达相应资源之前都会由ActionServlet调度) 发送给web服务器,服务器根据web.xml找到并转发给ActionServlet,ActionServlet根据struts-config.xml找到<form-beans></form-beans>里面有<form-bean name='userForm' type='userForm的存放位置'>,再根据<action-mappings></action-mappings>里面的<action path='表单发送的url地址,比如表单提交的是Login.do,则这里的path路径必须为/Login,这是必须的,否则会出现找不到Action的错误,name='userForm(用此和from-bean联系起来,因为form-bean非常可能配置多个)  type='要去处理的Action资源位置''><forward name='ok(自定义的名字)' type='要转向的页面)' />(这里提供的forward选项是用于action具体执行请求的页面中可转向的页面,比如登录成功转向成功页面,利用的是ActionLogin(Action的子类)的execute() 方法,里面的ActionMapping可实现此功能) </action>,这里ActionServlet根据struts-config.xml的配置实例化ActionForm(这里是UserForm,实际上是通过反射机制实例化的,这里要插一句,AcntionForm的setXXX方法名字一定要和浏览器的请求名字对应,如请求为<input name='username' type='text'>,则set方法必须为setUsername();这是填充数据必需的,有一点不匹配就无法完成填充 ),并转向实际的执行页面ActionLogin,ActionLogin根据处理结果再去查询struts-config.xml,再次利用反射机制转向相应的页面。


struts-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans>
  	<form-bean name="userForm"  type="com.sh.forms.UserForm"></form-bean>
  </form-beans>
  
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  	<action path="/Login" name="userForm" type="com.sh.actions.LoginAction">
  		<forward name="ok" path="/WEB-INF/OK.jsp"/>
 		<forward name="err" path="/WEB-INF/err.jsp"/>	
  	</action> 	
  </action-mappings>
  <message-resources parameter="com.sh.struts.ApplicationResources" />
</struts-config>


表单提交页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    
	<!-- 简单的登陆界面 -->
	<form action="Login.do" method="post">
		用户名<input type="text" name="username"><br>
		密码<input type="password" name="password"><br>
		<input type="submit" name="submit" value="提交">
	</form>
	
  </body>
</html>

Action类

package com.sh.actions;

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;

import com.sh.forms.UserForm;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		UserForm userForm = (UserForm) form;
		if(userForm.getUsername().equals("joseph") && 
				userForm.getPassword().equals("snnhoo"))
			return mapping.findForward("ok");
		else
			return mapping.findForward("err");
	}
	
	
}

ActionForm类
package com.sh.forms;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

	private String username;
	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;
	}
	private String password;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值