博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
详细剖析Struts工作流程
阅读量:3979 次
发布时间:2019-05-24

本文共 3029 字,大约阅读时间需要 10 分钟。

         1.Web客户端提交数据到Tomcat,在form表单中需说明表单提交的action是*.do或*.action,mothod是post或get;

  2.Tomcat接收Web客户端提交的表单,将表单数据打包到HttpServletRequest和HttpServletResponse对象中,然后通过doPost或doGet方式把request、response提交到ActionServlet(ActionServlet是Struts内部封装好的);

  要使用Struts封装的ActionServlet,需要在web.xml中配置ActionServlet,配置信息如下:

action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
debug
2
detail
2
2
action
*.do

  3.ActionServlet是struts的中央控制器,它任务如下:

   (1)它负责截取Web客户端提交的URL。比如login.jsp的action是login.do,然后根据URL从struts-config.xml中获得配置信息。配置信息需要手动在struts-config.xml中配置,配置信息如下:

           
login.jsp中action的url名必须和配置信息中的path名一致,这样ActionServlet才能根据URL找到对应的Action,完成请求响应。

   (2)创建ActionForm类的对象,用于收集表单数据,ActionForm类代码如下:

package com.tgb.struts;import org.apache.struts.action.ActionForm;/** * 登录ActionForm,负责表单收集数据 * 表单的属性必须和ActionForm中的get、set属性一致 * @author quwenzhe *  */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-config.xml中配置ActionForm信息,这样struts才能检测到有ActionForm的存在,配置信息如下:
   
 通过action-mappings中的scope属性,把表单数据赋值给ActionForm。

   (3)创建Action,Action类代码如下:

package com.tgb.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;/** * 登录Action 负责获得表单数据,调用业务逻辑,返回转向视图 *  * @author quwenzhe *  */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");		}	}}
           
action的信息已经在strut-config.xml中的action-mappings中配置。并且在配置信息中我们已经说明,forward name为"success"的对应login_success.jsp页面,forward name为"error"的对应login_error.jsp页面。
   (4)调用action的execute方法,将ActionForm中的信息提交到业务层控制器Action中处理

  4.Action是struts的业务层控制器,它获得ActionServlet提交的ActionForm,对ActionForm中的信息进行处理,将处理结果返回到ActionServlet。这里返回的是forward name为"success"或"error"的ActionFoward对象。

  5.ActionServlet根据Action返回的ActionFoward,选择需要跳转的页面。

  6.将跳转页面渲染,显示在Web客户端。

  温馨提示:如果修改了strus-config.xml文件,重启Tomcat服务器后修改才能生效。我在这吃亏了,希望大家能引以为戒。

  最后把项目的源码下载地址奉上,,希望能帮助大家理解Struts的工作流程。

你可能感兴趣的文章
vi和vim区别
查看>>
程序员经典语录
查看>>
Django性能优化
查看>>
python模块学习 ---- smtplib 邮件发送
查看>>
Python模块学习 ---- subprocess 创建子进程
查看>>
python求时间差
查看>>
Python网页抓取urllib,urllib2,httplib[2]
查看>>
Python urllib2递归抓取某个网站下图片
查看>>
JS清空多文本框|文本域
查看>>
linux常用命令(操作命令)
查看>>
Linux一些经典书籍
查看>>
apache启动报错(98)Address already in use: make_sock: could not bind to address [::]:80
查看>>
linux kill用法、killall、pkill、xkill
查看>>
Python笔记——排序算法的实现
查看>>
jQuery数据显示插件整合实现代码
查看>>
python时区设置——pytz模块
查看>>
用datetime和pytz来转换时区
查看>>
python解决导出excel文件时中文文件名乱码
查看>>
Django操作NOSQL(MongoDB)数据库
查看>>
Failed to load JavaHL Library
查看>>