struts开发文档.docx

上传人:b****6 文档编号:5809122 上传时间:2023-01-01 格式:DOCX 页数:12 大小:115.48KB
下载 相关 举报
struts开发文档.docx_第1页
第1页 / 共12页
struts开发文档.docx_第2页
第2页 / 共12页
struts开发文档.docx_第3页
第3页 / 共12页
struts开发文档.docx_第4页
第4页 / 共12页
struts开发文档.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

struts开发文档.docx

《struts开发文档.docx》由会员分享,可在线阅读,更多相关《struts开发文档.docx(12页珍藏版)》请在冰豆网上搜索。

struts开发文档.docx

struts开发文档

一.struts2原理

struts1和webwork结合产生

struts2需要的jar包

xwork-core-2.3.15.jar

struts2-core-2.3.15.jar

commons-lang3-3.1.jar

commons-logging-1.1.3.jar

ognl-3.0.6.jar

javassist-3.11.0.GA.jar

commons-fileupload-1.3.jar

freemarker-2.3.19.jar

commons-io-2.0.1.jar

struts2开发步骤

第一步:

web.xml文件配置

struts2.0使用的过滤器

org.apache.struts2.dispatcher.FilterDispatcher

struts2.1使用的过滤器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

   struts2

   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

 

 

 

 

    struts2

    /*

 

第二步编写action类

action类必须有一个execute()方法该方法返回一个字符串

第三步编写struts.xml文件

在src目录下新一个struts.xml文件

   

      

--action配置name代表你请求的url-->

      

      

        

--返回的结果页面-->

         /success.jsp

        

   

 

strust2接收客户端的数据(参数)

属性驱动

在action中有页面对应的属性设置set方法

对象模型驱动

mod模型驱动

模型驱动的Action中定义javaBean并实例化,并且需要提供该JavaBean的getter和setter方法。

Action需要实现ModelDriven接口。

publicUserInfogetModel(){

        if(user==null){

        user=newUserInfo();

}

   returnuser;

}

struts2工作原理或工作流程

1客户端发送请求,这个请求经过一系列的filter最后经过StrutsPrepareAndExecuteFilter

2StrutsPrepareAndExecuteFilter被调用,会询问ActionMapper(你的请求是否是action)来决定这个请求是否是需要调用某个action

3如果actionMapper决定需要调用action过滤器把这个请求交给actionProxy

5actionProxy通过configurationManager读取struts.xml文件,找到需要调用的acction类

6actionProxy创建actionInvocation实例

7actionInvocation通过命名模式来调用action,调用action之前会调用一系列的拉截器

8action执行完毕actionInvocation根据struts.xml文件找到对应的返回结果

实现action的几种方法

1实现接口action接口

2继承actionsupport

动态方法调用

用通配符*

在method属性中通过下标序号来访问方法

自动类型转换

result返回结果

name 

type类型

dispatcher

chain

redirect

redirectAction

freemarker

velocity

json

 

常用量的配置

修改struts2中默认配置的常用值

第一种方式

修改struts.xml

第二方式

在src目标下新建一个struts.properties

struts.i18n.encoding=ISO-8859-1

命名空间

namespace

搜索顺序

localhost:

8080/Struts2_07_02/test/test/test/a/listuser.do

localhost:

8080/Struts2_07_02/test/test/test/listuser.do

localhost:

8080/Struts2_07_02/test/test/listuser.do

localhost:

8080/Struts2_07_02/test/listuser.do

 

模块化开发

struts.xml

配置一些常用量

二.struts2类型转换

自动转换

类型自动转换出错的错误信息提示 

默认的使用xwork-messages.properties中的提示信息

如果要修改

局部

在action所在的包下新建一个名称跟action相同的一个properties文件

然后使用

invalid.fieldvalue.属性名=XXX来处理

全局

在src目录xwork-messages.properties

自定义转换

自定义转换器

编写自定义转换类

继承DefaultTypeConverter

重写convertValue

继承StrutsTypeConverter

实现convertFromString方法

convertToString方法

注册转换器

局部类型注册

三struts2拦截器

filter

Interceptor拦截器

自定义拦截器

拦截器类实现接口Interceptor

实其中最重要的一个方法

ActionInvocation调度者

intercept(ActionInvocationinvocation)

实现一个自定义拦截器步骤

编写拦截器类

注册拦截器

1定义拦截器

2使用拦截器

局部拦截针对某个Action

引用自定义拦截器后£¬struts2默认拦截器就不会调用£¬我们需要手动去调用拦截器栈

拦截器栈(包含多个拦截器)

定义

--全局类型的result-->

/login.jsp

使用全局拦截器

--引用拦截器-->

--全局类型的拦截器-->

拦截器只拦截action

拦截器可以拦截到action中的某个方法

拦截器类须要继承MethodFilterInterceptor类

--不需要拦截的方法-->

listuser,updateuser

--需要拦截的方法-->

adduser,deleteuser

四struts2文件上传下载

上传文件的三个参数

privateFileupload;//上传的文件

privateStringuploadFileName;//文件名(

privateStringuploadContentType;//上传文件的类型

定义上传文件的类型,文件大小

--限制文件上传的类型-->

image/gif,image/jpeg

--限制上传文件的大小-->

1024

文件下载

在actiion中一个方法

publicInputStreamgetInputStream()throwsException{

System.out.println(filename);

Stringfile=newString(filename.getBytes("iso-8859-1"),"utf-8");

InputStreamin=ServletActionContext.getServletContext().getResourceAsStream("/upload/"+file);

returnin;

}

置配

--下载的文件类型-->

image/jpeg

--指定action去调用action中的某个方法该方法返回的是一个inputstream-->

2048

--inline默认的表示在浏览器中打开,attachment会弹出下载的对话框-->

attachment;fileName=${newfilename}

返回json

struts.xml文件中必须继承json-default

必须引入下面的包

struts2-json-plugin-2.3.15.jar

五struts2国际化

在struts2中需要做国际化

jsp页面

textname="login">

text>

校验错误信息提示

this.getText("username.invalid")

类型转换错误信息提示

Action错误信息

以上四种都需要国际化

action类

actionName_en_US.properties

全局所

在src目录下

message_zh_CN.properties

message_en_US.properties

六struts2ognl

pojo简单java对象

struts2action跟servletapi容器解耦(没有依赖关系)

struts2与servlet耦合

三种方式

1ActionContext与servlet耦合不能获得httpservletResponse

ActionContext.getContext().put("hello","helloworld")类似于setAttribute();

${requestScope.hello}

2Aware方式(servletRequestAware,ServletResponseAware)实现接口

 

3ServletActionContext方法

session=ServletActionContext.getContext().getSession();

request=ServletActionContext.getRequest();

 

ognl(对象图导航语言)ObjectGraphNavigationLanguage

通过

propertyvalue="hello"/>标签访问valueStack和ActionContext中的值直接使用属性名或对象名去访问

访问其它5个域的值需要在前加上#

过滤和投影集合

?

ui标签主要用生成HTML元素的标签

非UI标签:

主要用来访问数据库,逻辑控制

Ajax标签

 

set>用于将某个值放到指定的范围scope属性applicationsessionreqeustpage不写默认是actionContext

value属性不写,默认将valueStack顶的值赋给变量

控制标签

if

elseif

else

append

iterator将迭代输出

status属性每一次环循都会有一个IteratorStatus实例这个实例有很多方法

booleanisEven()当前索引是否是偶数

getIndex()返回当前元素的索引号

isOdd()

getCount()

first()

last()

sort对集合进行排序

merge多个集合拼接成一个新集合

调用action中的方法

propertyvalue="abc()"/>

调用action中某个对象的普通方法

propertyvalue="user.getUsername()"/>

访问静态属性

propertyvalue="@java.lang.Math@PI"/>

访问静态方法

表达式的格式为@[类全名(包括包路径)]@[方法名|值名],例如:

@java.lang.String@format('foo%s','bar')。

页面的输出结果就是:

foobar

原来是在2.1.2中,如果要通过ognl访问静态方法,必须在struts.properties或者struts.xml中将选项struts.ognl.allowStaticMethodAccess设置为true.

日期控件

head/>

formname="test"theme="simple">

datetimepickerdisplayFormat="yyyy-MM-dd"type="date"value="today">

datetimepicker>

form>

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1