Struts框架指导

来源:互联网 发布:申请淘宝账号注册官网 编辑:程序博客网 时间:2024/05/16 07:36

Struts框架指导》 2010-10-13 author:heguikun

Struts是最早Java开源框架之一,Struts是MVC 设计模式一个优秀的实现。
struts定义了通用的Controller,通过Struts-config.xml配置文件将视图
与模型分开。以action的概念对用户做了封装,自动将请求的数据封装到
对象。


Hibernate是一个优秀的持久化框架,负责从数据库保存.读取数

Spring是一个轻量级框架,他主要作为依赖注入容器和AOP实现存在。

DWR是ajax框架,它通过javascript代码异步请求服务器资源的技术。

M模型:由接口,javaBean充当(定义和实现逻辑的接口和类)
V视图:Jsp,自定义的ActionForm,struts标签
C控制器:ActionServlet,自定义Action类
===下面是实现MVC的自定义框架
在web.xml文件中*.do 把所以.do结尾的拦截下来交给controller类处理。
================Action接口================
Action接口中方法 String execute(httpServlerrequest request,HttpSrevlerResponse response);
此方法返回的是要转发到的页面。
====================Controller类================
Controller类中 Action determinActionBypath()方法
返回Action 的对应实现类(子类)
然后可以在dopost()中用Action 调用execute()方法得到跳转的页面
-----写在Servlet中-------------
--下面是登录的简单例子
loginActon 实现了接口Action的execute方法
public String execute(httpServlerrequest request,HttpSrevlerResponse response)
{
//1得到参数
//2判断用户是否存在
if(存在){
return index.jsp;//首页
}
else{
return login.jsp;//继续到登录页面
}
}

Controller类中 Action determinActionBypath()方法
public Action determinActionBypath(httpServlerrequest request,HttpSrevlerResponse response)
{
Action  ret=null;
//1.得到请求路径 String path=request.getservletPath();
//2.actionName=截取path的关键字
if(actionName.eqals("add")){
ret=new loginActon ();
}
else if(actionName.eqals("save")){
ret=new saveActon ();
}
else if(actionName.eqals("***")){
ret=new ***Acton ();
}
return ret;
}

Controller类中 dopost()方法
public oid dopost(httpServlerrequest request,HttpSrevlerResponse response)
{
Action  action =determinActionBypath(request,response);
String path=action.execute(request,response);//得到要转发的页面
//跳转到path页面
}
==控制器的工作基本这样了

===========================接下来学习struts===============================

          前期准备:1.把Struts库文件和自定义标签添加

                    2.web-INF下建立Struts-config.xml配置文件

                    3.web.xml中配置ActionServlet

 


=====================struts 研究==================

将struts解压后,web-inf/lib的文件 其中stuts.jar 复制到项目的lib下 其他文件复制到web-inf下
并创建struts-config.xml配置文件。
8:58 2010-10-8
最后在web.xml中配置Actionservlet。其中<param-value>web-inf/struts-config.xml </param-value>
中指定的配置文件可以指定多个,但名字不能相同。


配置访问路径时一般是*.do 也可以是:/action/*  或者 *.action 根据自己的习惯所定。

上面提到过 视图组件中 除了jsp 还有自定义的ActionFrom 它继承org.apache.struts.action.ActoinFrom
它用来封装页面输入 的数据类(称为form bean)
注意:ActionFrom 中的属性名要和页面组件的名(页面输入空间的名)一致
方便struts自动封装。
下面比如实现一个加法器
ActionFrom 就命名为AddFrom
创建AddAction 继承org.apache.struts.action.Actoin
并在其中实现execute()方法
public ActionFoward execute(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response)
{
//form 封装了页面传入 的数据
ActionFoward  af=null;
AddFrom addFrom =(AddFrom )form ;
//调用业务方法
Calculator biz =new Calculator();
double resultData = biz .add(addFrom .getNum1,addFrom .getNum2);
//存放到请求中
request.setAttribute("resultData ",resultData );
//mapping 封装了页面转发的配置信息,下面将请求转发到逻辑名为result的页面
//mapping 保存了页面和逻辑名对应关系的配置信
af=mapping.findForward("result"); //所以根据逻辑名可以找到对应的ActionFoward 
//返回执行后要转发到的jsp页面。
return af;
}
在编写execute()之前在struts-config.xml中配置
AddFrom  和ActionFrom
具体见下次补冲&&&&&&&&&&&&&&&&&&&&&&

===========struts原理1===============
客户端提交请求——ActionServlet ——在Struts配置信息中查找Path属性为/Add(根据提交
路径决定)的Action 于是找到AddAction 并得知Action 的名为AddForm 于是根据名字(AddForm )
找到AddForm 的实例(如果不存在则创建)
在确定调用Action Bean 和Form Bean 后,Struts自动将页面的数据填充到Form Bean(比如AddForm)
中。并在调用 execute(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response)方法之前 ActionServlet还有ActionMapping  其中ActionMapping
里面包含了页面和逻辑名对应关系的配置信。
执行execute方法之后返回要跳转的页面,控制权重新交给ActionServlert.
但ActionServlert.转到页面要用转发的方法,因为还要从请求中取到属性值(根据实际情况)
===========struts深度涉险============
===========struts原理2===============
-------------------使用DispatchAction-------------------
之前学的自定义的xxxxAction类是继承于org.apache.struts.action.Actoin
它必须要实现execute方法
现在我们开始用继承于org.apache.struts.actions.DispatchActoin 再也不必要实现execute方法
并且里面可以写多个业务需要的方法 比如:加法,减法...等方法
但是这些方法的参数必须跟之前的
execute(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response)
一样,
比如:加法的方法add(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response),减法的方法等;

现在比如 public calss CalcAction extends DispatchAction(
                                      //加法
                                    //减法
                                   //乘法---
                         }
但是怎么告诉xxxAction要到底什么时候要调用哪个方法呢?就要用到请求页面的隐藏的表单
元素如:<input type="hidden" name="operate" value="doAdopt">
但是struts中的配置文件中要比之前多些一个parameter属性 ,它的值是隐藏表单的名 operate
    <!-- 转到领养宠物 页面-->
    <action path="/adopt" scope="request" type="com.aptech.jb.epet.action.AdoptAction" parameter="operate">
    <forward name="adoptwrite" path="/WEB-INF/jsp/adopt.jsp"></forward>
 </action>
之后com.aptech.jb.epet.action.AdoptAction  会根据隐藏表单operate 的value值 找到对应的方法执行
这样减少了Action 的数量
其中Action中的方法的前缀要遵循这样的规则:Toxxx 表示转到xxx页面, doxxx表示执行xxx操作,
 这样的可以是程序逻辑更清晰,减少出错。
======struts国际化的错误信息处理机制==========

1.添加struts支持时, 它会自动生成一个ApplicationResource.properties 属性文件
2.并在config配置文件生成了一段代码:
    <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
    这表示对ApplicationResource.properties 属性文件的引用.
3. 在ApplicationResource.properties 属性文件配置错误信息如下:
   errors.header=<span style="color:red">
     errors.footer=</span>
  error.validate.number=Diviso cannot be 0.
  这样错位信息就会按照这样在样式显示
 <span style="color:red"> Diviso cannot be 0. </span>

原创粉丝点击