struts1常用组件

一,ActionServlet(中心控制器)

定义:他继承了HttpServlet类,是中心控制器(总控制器),它提供一个中心位置来处理全部的终端请求

作用:接受请求,读取配置文件,派发请求,响应用户

配置:

 

<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>

 

二,Action

|--Action实例化是在接受到ActionServlet的派发请求的时候,而不是在读取配置文件的时候

|-- 每个action只会被实例化一次

(第一种情况:后面的action会覆盖前面的action

第二种情况:内存中也只有一个LoginAction

·第一种情况

<action path="/login"type="hwt.action.LoginAction"name="loginForm">

            <forwardname="loginSuccess"path="/loginSuccess.jsp"></forward>

            <forwardname="loginFailure"path="/loginFailure.jsp"></forward>

  </action>

 

<action path="/login"type="hwt.action.LoginAction"name="loginForm">

            <forwardname="loginSuccess"path="/loginSuccess.jsp"></forward>

            <forwardname="loginFailure"path="/loginFailure.jsp"></forward>

  </action>

 

·第二种情况

<action path="/login"type="hwt.action.LoginAction"name="loginForm">

            <forwardname="loginSuccess"path="/loginSuccess.jsp"></forward>

            <forwardname="loginFailure"path="/loginFailure.jsp"></forward>

  </action>

  <action path="/login1"type="hwt.action.LoginAction"name="loginForm">

            <forwardname="loginSuccess"path="/loginSuccess.jsp"></forward>

            <forwardname="loginFailure"path="/loginFailure.jsp"></forward>

  </action>

|--Struts1中的Action是线程不安全的

  ·不要使用实例变量和类变量共享只针对某个特定请求的数据

  ·注意资源共享的同步性

 

action里面的几个属性:

path:

name:

type:

scope: actionForm的存放范围 默认为session

validate:  默认为true,为验证

input:  这里是确定验证错误的时候的去向页面,一般和validate联合使用

attribute: 存放form的真正关键字,缺省的时候默认为name属性的值

 

三,ActionMapping

在配置文件中的每一个<action></action>都与一个ActionMapping实例对应

 

mapping.getPath(); 得到action的path属性的值

mapping.getType(); 得到action的type

mapping.getName(); 得到action的name;

mapping.findForwards(); 得到action中的Forward的一个数组

mapping.findForward(String name); 返回一个ActionForward

 

四,ActionForward(导航器)

·        contextRelative- Should the pathvalue be interpreted as context-relative (instead of module-relative, if itstarts with a '/' character? [false]

·        name- Logical name bywhich this instance may be looked up in relationship to a particularActionMapping.

·        path- Module-relativeor context-relative URI to which control should be forwarded, or an absolute orrelative URI to which control should be redirected.

·        redirect- Set to true ifthe controller servlet should call HttpServletResponse.sendRedirect() on theassociated path; otherwise false. [false]

 

如果要用重定向访问站外的网站地址,需要用绝对路径:http:// .....

 
path的路径是默认为相对路径的,要以“/” 开头

 

重定向:

<forwardname="loginFailure"path="http://www.baidu.com"redirect="true"></forward>

 

redirect == false --> 内部调用的是RequestDispatcher.forward

redirect == true --> 内部调用的是HttpServletResponse.sendRedirect();

 

 

五,actionForm

 

actionForm的填充是有ActionServlet填充的,工作原理:

1,ActionServlet读取配置文件

2,actionServlet接受请求的时候,通过配置文件判断该请求需不需要form

3,如果需要,就根据<action ....name="" scop="">name里面的参数和scope里面的参数来找Form,scope里面的参数缺省值为session,scope.getParameter("name"); 如果scope里面没有form这个实例,那么实例化这个对象

    4,通过实例化的对象带入到action中

 

实例化ActionForm时候内部方法顺序:

构造函数

|

reset()方法

|

setter()方法 --》 页面的填充是根据setter方法中的名字,而不是字段名字

|

validate()方法来验证

 

 

 

========== 动态form-bean---------DynaActionForm

动态formbean就是不要写form-bean的java实现类,只需要在配置中配置一下,在配置文件中这样写

<form-beans>

        <form-beanname="loginForm"type="org.apache.struts.action.DynaActionForm">

            <form-propertyname="username"type="java.lang.String"/>

            <form-propertyname="password"type="java.lang.String"/>

        </form-bean>

    </form-beans>

 

在实现类中要用这里的面的属性的话:

public ActionForward loginMethod(ActionMapping mapping, ActionForm form,

            HttpServletRequestrequest, HttpServletResponse response) throws Exception {

        DynaActionForm dynaActionForm = (DynaActionForm)form;

        String username = dynaActionForm.get("username").toString();

        String password = dynaActionForm.get("password").toString();

        response.getWriter().print(username+"--"+password);

        return null;

    }

 

=======对于validate验证方法通过不了的,要在页面显示错误信息的:

 

1,要想做国际化一样,首先要建立资源文件ApplicationResources.properties,在这里面写错误信息

2,对于资源文件,要在struts-config.xml里面注册

<message-resourcesparameter=" " />

而要注意的是在<action .... validate="true" input=""/>

3,对于form 里面的validate方法

public ActionErrors validate(ActionMapping mapping,

            HttpServletRequestrequest) {

        ActionErrors errors = newActionErrors();

        ActionMessage message = new ActionMessage("这是写资源文件中的键",true);

        errors.add("这里是写页面的property内容", message);

        System.out.println(errors);

        return errors;

    }

4,在页面<html:errors  property=""/>----记得要导入想要的tld文件

==========

 

Session和request中的属性的监听器的实现:

 

1,在web.xml中配置

<listener>

<listener-class></listener-class>

</listener>

 

2,实现类

 实现两个接口:HttpSessionAttributeListener,ServletRequestAttributeListener

然后重写里面的方法

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值