1.简单应用示例

导入struts1的jar包,然后配置xml,写java和jsp

/struts/WebRoot/Login.jsp

<%@ 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 'index.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">
</head> <body>
<form action ="<%=request.getContextPath()%>/login.do">
username : <input type='text' name ='username' />
password : <input type='password' name ='password' />
<input type="submit" value="login"/>
</form>
</body>
</html>

/struts/WebRoot/LoginSuccess.jsp

<%@ 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 'index.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>
登陆成功
</body>
</html>

/struts/WebRoot/LoginFailure.jsp

<%@ 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 'index.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>
登陆失败
</body>
</html>

/struts/WebRoot/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml
</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

/struts/WebRoot/WEB-INF/struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="cn.itcast.LoginForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login" type="cn.itcast.LoginAction" name="loginForm">
<forward name="success" path="/LoginSuccess.jsp"></forward>
<forward name="failure" path="/LoginFailure.jsp"></forward>
</action>
</action-mappings>
</struts-config>

/struts/src/cn/itcast/LoginAction.java

package cn.itcast;

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 { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
LoginForm loginForm= (LoginForm)form ;
String username = loginForm.getUsername();
if("itcast".equals(username)){
return mapping.findForward("success");
}else{
return mapping.findForward("failure");
}
} }

/struts/src/cn/itcast/LoginForm.java

package cn.itcast;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {
private String username = null;
private String password = null; 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;
} }

2.Action什么时候初始化?发出该Action请求,不是在读取配置文件时初始化。

3.每个Action只会初始化一次

4. Action是线程不安全的,因为所有的请求都共享一个action实例

5.怎样实现action的安全性编程?

1)注意不要用实例变量或者类变量共享只是针对某个请求的数据

2)注意资源操作的同步性

应用:统计一个action的访问次数

在调用时加1,注意用Sychronzied同步变量

6.ActionMapping类,可以获取ActionMapping里配置的信息

7.ActionForward

 redirect:false ---------requestDispather:forward(重定向)默认值

    true-----------httpServletResponse:sendRedirect(重新发出请求)

8.ActionForm

1)检查action的映射,确定action中已经配置了对ActionForm的映射

2)根据name属性,查找formbean的配置信息

3)检查action的formbean的使用范围,确定在此范围下(request、session),是否已经有此formbean的实例

4)假如已经存在就重用

5)否则就重新构造,并保存在一定的范围

9.Action

1)attribute:用来存取form的关键字,缺省值与name一样

2)validate:用来控制是否校验表单(校验开关),缺省ture--校验

3)input如果表单校验不通过则跳转到该值所代表的目标模块,一般结合validate:true来使用

10.全局跳转

<global-forwards>

<forward name="error" path="/error.jsp"></forward>

</global-forwards>

11.ForwardAction不直接访问jsp,每次都访问action.do

<action path="/action" forward="/action.jsp"></action>

12.DispatchAction用一个action来分发请求,xml配置parameter参数

调用时加参数/risk.do?method=action中的方法名

<action path="/risk" type="org.springframework.web.struts.DelegatingActionProxy" parameter="method"></action>

struts1的更多相关文章

  1. struts1和struts2的区别

    1. 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类:Struts 1的一个具体问题是使用抽象类编程而不是接口.Struts 2 Action类可以实现一个Acti ...

  2. struts1四:常用标签

    struts1支持的5种标签: HTML 标签: 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 Bean 标签: 在访问JavaBeans 及其属性,以及定义一个 ...

  3. struts1二:基本环境搭建

    首先建立一个web项目 引入需要的jar包 建立包com.bjpowernode.struts创建LoginAction package com.bjpowernode.struts; import ...

  4. Struts2与Struts1的区别

    Struts2是基于WebWork的一个全新框架.不过有了Struts1的基础,学Struts2更方便.Struts2主要改进是取代了Struts1的Servlet和Action.Struts2的核心 ...

  5. Struts1.x 中的 Validate 框架

    转载于http://www.blogjava.net/nokiaguy/archive/2009/02/12/254421.html 一.Validator框架的优势       Validator框 ...

  6. Struts1.x有两个execute方法,不要重写错哦HttpServletRequest才是对的(转)

    Struts1.x 的 Action 有两个 execute 哦,小心搞错! by agate - Published: 2008-05-01 [9:42 下午] - Category: 程序编码 不 ...

  7. struts1拦截器

    Struts2已经发布一段时间了,这个版本较struts1.x版本有了很大变化,其中一个就是增加了拦截器功能.这是个非常有用的功能,可是struts1.x却没有.     其实,struts1.x可以 ...

  8. struts1的一些基本用法和操作

    入职两周了,项目是用struts1+ibatis框架搭建的,数据库是oracle,其他还行,关键是struts1之前没用用过,所以只好在网上狂查文档,最后大致整理了一些struts1的基本使用方法. ...

  9. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

随机推荐

  1. URL编码:不同的操作系统、不同的浏览器、不同的网页字符集,将导致完全不同的编码结果。

    URL编码:不同的操作系统.不同的浏览器.不同的网页字符集,将导致完全不同的编码结果. 因此如果Url中有中文或特殊字符,一定要自己调用函数编码解码,不要让浏览器帮你编码,否则出现了问题会浪费你很多时 ...

  2. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  3. PLSQL Developer注册码

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca

  4. FK JavaScript之:ArcGIS JavaScript添加Graphic,地图界面却不显示

    使用ArcGIS JavaScript,往地图中添加几个Graphic,基本是与官网示例代码一致.绘制的图形一闪而过之后,就没了 核心代码如下: iniToolBar: function () { t ...

  5. 阶乘之和--nyoj91

    描述 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9=1!+2!+3!,如果是,则输出Yes,否则输出No: 输入 第一行有一个整数0<m<10 ...

  6. Linux 搭建svn服务器

    一.安装svn服务器端yum install subversion      从镜像下载安装svn服务器端 如果后面执行“svnadmin create /usr/local/svn/sunny”提示 ...

  7. IOS第二天

    第二天 *******图片的放大,和缩小 (去掉自动的布局) -(IBAction ) zoomFrame:(UIbutton *) button{ CGRect frame= self.iconBu ...

  8. Windows内核 基本数据结构

    驱动对象: 每个驱动程序都会有唯一的驱动对象与之对应,并且这个驱动对象是在驱动加载时被内核中的对象管理程序所创建的.驱动对象用DRIVER_OBJECT数据结构表示,它作为驱动的一个实例被内核加载,并 ...

  9. ThinkPHP 3.2.3 简单后台模块开发(一)常用配置

    一.项目分组 下载解压 ThinkPHP 3.2.3,在默认的应用 Application(./Application) 中,包含一个默认的模块 Home(./Application/Home). 需 ...

  10. IE8兼容H5语义标签

    //IE浏览器定义的特殊属性,通过hack方式判断IE版本来执行不同的代码,IE8以下浏览器自动创建html5语义标签,从而实现兼容<!--[if lte IE 8] <script sr ...