struts2框架基本操作总结
struts技术说明
一:第一配置开发环境
1.struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
2.web.xml文件
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.导入包
(xwork,struts2-core,commons-fileupdate,commons-io,commons-logging,ognl,freework)
第二基本使用
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message">/index.jsp</result>
</action>
</package>
重定向到action(指定其他包名)
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message" type="redirectAction">
<param name="actionName">demo1</param>
<param name="namespace">/demo</param>
</result>
</action>
</package>
(同一个包中)
<result name="message" type="redirectAction">demo1</result>
第四格式化输出HTML
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<result name="message" type="plainText">
<param name="location">/index.jsp</param>
<param name="charSet">UTF-8</param>
</result>
</action>
</package>
第五action 指定参数
<package name="defalut" namespace="/demo" extends="struts-default">
<action name="demo1">
<result>/index.jsp</result>
</action>
<action name="Student_*" class="my.struts2.test.Child" method="{1}">
<param name="nameString">啊silt就会ihg</param>
<result name="message">/index.jsp</result>
</action>
</package>
struts2常用的常量
struts2可以通过修改常量值来改变action的访问后缀。
<constant name="struts.action.extension" value="do,action"/>
struts.properties中的配置如下:
struts.action.extension=do
此外因为常量可以在下面多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值。
常用常量介绍:
·<constant name="struts.i18n.encoding" value="UTF-8" />
指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输出
·<constant name="struts.action.extension" value="do" /> 改变action的后缀
·<constant name="struts.configuration.xml.reload" value="true" />
当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段最好打开,这样不用重启服务器。
·<constant name="struts.devMode" value="true" />
开发模式下使用,这样可以答应出更详细的错误信息
·<constant name="struts.multipart.maxSize" value="10701096" />
上传文件的大小限制,value值表示总大小,不是单个文件。
·<constant name="struts.ui.theme" value="simple" />
默认的视图主题
·<constant name="struts.serve.static.browserCache" value="false" />
设置浏览器是否缓存静态内容,默认为true,开发阶段最好关闭
·<constant name="struts.objectFactory" value="spring"/>
与spring集成时,指定由spring负责action对象的创建
·<constant name="struts.enable.DynamicMethodInvocation" value="true" />
该属性设置struts2师傅支持动态方法调用,该属性的默认值是true。如需关闭,则设置value="false"。
为多个项目指定配置文件
<include file=""></include>
类型转换器
在转换器类中继承DefaultTypeConverter 实现方法
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
}
是双向转换,value为要转换的值 toType 为转换成的类型
配置属性文件
位action配置转换文件
filename=ActionClassName-conversion.properties
其中里面内容是:属性名=转换器class类名)
全局类型转换器
filename= xwork-conversion.properties
其中里面的内容
要转换属性的类全名=转换器类全名
访问添加属性
ActionContext对象
example: ActionContext.getContext.getSession.put("name""liu");
ServletActionContext对象
example:HttpServletRequest request= ServletActionContext.getRequest();
文件的上传
用到工具类fileUtils.copy(from,to);
1.在form中指定:enctype="multipart/form-data"
2.可以通过file的属性名+FileName得到文件名,+ContentType文件类型
3.多文件用属性名为数组input name 相同
自定义拦截器
自定义拦截器实现接口Interceptor
通过拦截器return arg0.invoke();'
未通过返回视图名
在Struts.xml中配置拦截
<interceptors>
<interceptor name="permission" class="my.demo1.PermissionInterceptor"></interceptor>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"></interceptor-ref>(提供的)
<interceptor-ref name="permission"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="Load_*" class="my.demo1.Load" method="{1}">
<interceptor-ref name="permission"></interceptor-ref>
<result name="success" type="redirect">/index.jsp</result>
</action>
输入校验
在Action中继承ActionSupport
重写validate方法
校验没通过返回"input"视图名
对指定方法校验校验
validate+方法名(首字母大写)
xml实现表单校验
首先需要继承ActionSupport;
没通过返回"input"视图
对所有的action方法进行校验
xml 文件名 actionClassName-validation.xml
对指定的action方法进行校验 actionCalssName-actionName-validation.xml
详细的校验配置看文档
全局视图定义
<global-results>
<result name="message">/WEB-INF/jsp/LoadState.jsp</result>
</global-results>
方法可以直接返回视图名
对于使用其他包的全局视图可以继承他包
sdtruts2 的国际化
1.在struts.xml配置文件中添加
<constant name="struts.custom.i18n.resources" value="Resource" />
Resource_zh_CN.properties(Resource和value对应)
2配置action范围的资源文件
actionClassName_zh_CN.properties
3配置包范围的资源文件package_zh_CN.properties(package固定写法)
显示资源用
<s:text name="user">(占位符)
<s:param>ling</s:param>
<s:param>ssd</s:param>
</s:text>
<s:text name="habit"></s:text>{无占位符}
在表单中通过<s:textfield key="user"></s:textfield>
在action方法中继承Actionsupport 通过this.getText(key,string[]{占位符内容});
struts2框架基本操作总结的更多相关文章
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。
1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...
- Struts2框架基础
Struts2框架基础 1.Java的框架 1.1.框架简介 在大型项目开发过程中,经常会使用到一些框架,这样做好的好处是能够提高工作效率,在java中最常用的的框架就是SSH,这其实是三个框架的简称 ...
- Struts2框架简介和示例
struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...
- struts2 框架处理流程
struts2 框架处理流程 流程图如下: 注意:StrutsPrepareAndExecuteFilter替代了2.1.3以前的FilterDispatcher过滤器,使得在执行Action之前可以 ...
- 搭建一个简单struts2框架的登陆
第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Struts2 框架的快速搭建
方便myEclipse 手动配置Struts2框架,写下此文,需要的朋友拿走不谢~ 一.引入JAR包 WEB工程->WebRoot->WEB-INF->lib引入Struts2对应版 ...
- struts2框架 初始别
struts2 是webwork和struts合并而来. 1.下载struts2 说明: Full Distribution: 为完整版下载,建议下载它 Example Applications:st ...
随机推荐
- TFS客户端登录用户修改
1.操作环境: Windows 10 Pro Visual Studio 2013 TFS客户端 2.操作背景 安装完成TFS系统后,使用administrator在客户端进行登陆测试(已勾选保存用户 ...
- C++调用WebService服务问题总结
近期接手一部分C++研发工作,同时与.NET开发小组进行软件功能协作,于是需要调用.NET WebService服务,已完成数据的网络通讯业务功能:结束之余,将手头碰到的问题总结罗列如下: C++如何 ...
- EXTJS 密码确认与验证
extjs 框架是一个非常优秀的前端框架,提供了丰富的功能与炫丽的界面展示,在用 extjs 创建表单时,特别是在注册或修改密码的时候,要对密码进行确认,这里就密码确认一致性验证和大家分享自己的心得与 ...
- 洛谷P1215 [USACO1.4]母亲的牛奶 Mother's Milk
P1215 [USACO1.4]母亲的牛奶 Mother's Milk 217通过 348提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
- 关于Oracle的rac集群和mysql Galera Cluster的想法
到了新公司,公司用的是rac,我比较熟悉mysql第三方的集群方案Galera Cluster这类多主集群, 下面是我参考了他人对rac的介绍,然后和mysql方案进行的臆测级别的分析对比. rac和 ...
- Facebook React完全解析
2004年,对于前端社区来说,是里程碑式的一年.Gmail横空出世,它带来基于前端渲染的原生应用级别的体验,相对于之前的服务端渲染网页可谓提升了一个时代,触动了用户的G点.自此,前端渲染的网站成为无数 ...
- Android IOS WebRTC 音视频开发总结(二九)-- 安卓噪声消除交流
Android上的音质一直被大家所困扰和诟病,这里面有很多原因, 下面是最近一位前UC同行发邮件跟我交流的一些记录,供参考,支持原创,文章来自博客园RTC.Blacker,转载请说明出处. 以下文字来 ...
- FTP操作
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- MySQL的20条基本优化 加参考资料
mysql的性能优化包罗甚广:索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整理 ...
- TortoiseGit和Git操作git@osc简要说明
之前搞过一次git@osc的操作,重装系统后今天准备再次提交代码,悲剧了,我忘了之前怎么干得了,再次百度以后,终于搞定了,准备写下来,省得下次再忘了. 1.首先生成key,使用下面的工具,在Torto ...