Struts1.x复习
来源:互联网 发布:英雄联盟动漫 淘宝网 编辑:程序博客网 时间:2024/06/05 21:09
是Apache下的一个框架http://struts.apache.org/。
MVC是Model-View-Controller 的缩写;Model为业务数据,或者为业务处理;View为用来在屏幕上显示数据对象的当前状态,是应用程序的外观;Controller为根据用户的输入,控制用户界面数据显示和更新model状态。
Struts结构图:
Struts1.x:
html
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!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=UTF-8"><title>logning test</title></head><body><form action="login.do" method="post">username :<input name="username" id="username" type="text" /> <br>password: <input name="password" id="password" type="text" /> <br><input value="登录" type="submit" /> <br></form><a href="opertor.do?method=save">添加</a> <br> <a href="opertor.do?method=delete">删除</a> <br></body></html>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>test3</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>struts</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class></servlet><servlet-mapping><servlet-name>struts</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
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="loginForm" type="com.dqh.LoginForm.LoginForm" /></form-beans><action-mappings><action path="/login" type="com.dqh.Action.LoginAction" name="loginForm"><forward name="success" path="/welcome.jsp" /><forward name="failure" redirect="true" path="/err.jsp" /></action></action-mappings> <action path="/opertor" type="com.dqh.Action.OpertorAction" parameter="method" ></action><message-resources parameter="resources.application" /></struts-config>
<form-beans><global-forwords> <action-mappings〉是最常用的三个元素
<action>中name与 <form-bean>中name需相应
<forward>中redirect=“true” 表示为客户端跳转
Form:
package com.dqh.LoginForm;import org.apache.struts.action.ActionForm;public class LoginForm 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;}}
Action:
login
package com.dqh.Action;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.dqh.LoginForm.LoginForm;public class LoginAction extends Action{@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {LoginForm loginform=(LoginForm)form;String username=loginform.getUsername();String password=loginform.getPassword();if(username.equals("zhangsan") && password.equals("123456")){return new ActionForward("/welcome.jsp");}else{return mapping.findForward("failure");}}}
opertorAction:
package com.dqh.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.actions.DispatchAction;public class OpertorAction extends DispatchAction {public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {System.out.println("do save .........");return null;}public ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {System.out.println("do delete .........");return null;}}
继承Action调用execute()方法,而继承DispatchAction 可根据request中method 的值调用相应的方法,struts-config.xml 中需设置parameter为method 与 request传过来的变量名相同。
struts1.x是基于servlet
web.xml 中ActionServlet收到一个request时,执行以下步骤:
1.doPost()或者doGet()收到请求,调用proess()方法
2.process()方法的道当前的RequestProcssor对象
3.RequestProcessor.process()是实际负责处理request的方法,它根据request的uri从struts-config.xml中得到<action>元素。
4.当RequestProcessor.process()有了匹配的<action>元素之后。他将在<form-beans>元素中寻找与<action>元素中相同的“name”属性值的<form-bean>。
5.当RequestProcessor.process()找到对应<form-bean> ,根据在<form-bean>中的“type”属性中指出的词formbean的定义,创建或者更新一个定义的实例,利用request中提交的数据初始化这个实例。
6.至此,RequestProcessor.process()已获取一切所需的,它利用<action>元素总的“type”得值创建一个action类的子类实例,调用Action.execute()方法,执行真正的业务逻辑
7.这个Action从execute()方法中返回一个ActionForward对象,决定返回目标,RequestProcessor.proess()方法重新获得控制权,这个request按照action中返回的ActionForward返回。
8.至此,该ActionServlet实例完成了对这个请求的处理,准备处理下一个请求。
- Struts1.x复习-配置文件
- Struts1.x复习
- struts1.x
- Struts1.x
- Struts1.x
- struts1.2复习完毕
- struts1.2简单范例复习
- struts1的整体复习讲解
- struts1.x实现
- Struts1.x(一)
- Struts1.x(二)
- Struts1.x(三)
- spring和struts1.x
- Struts1.x 国际化
- Struts2与Struts1.X
- Struts1.x 架构
- struts1.X初步
- 关于struts1.X
- asp.net获取当前网址url
- div鼠标经过变手指形状
- CSS纯相册效果
- wince程序不能联机调试问题解决!!
- CSS+DIV布局之道
- Struts1.x复习
- 浅谈设计模式
- 归并排序
- 【集合】HashSet类
- USB入门系列之一----基本知识
- c# 获取相对路径
- ios sqlite中写入图片二进制数据及读取
- ExecutorService中submit和execute的区别
- 古希腊神话故事:狄俄尼索斯